Search code examples
pdftabstabstop

PDF sourcecode of text with tab/tabstop or fixed width trick


I have this s variable string: ID#9NAME#9VALUE

How this string look like in PDF? (ID) Tj (NAME) Tj (VALUE) Tj

I have to convert the s variable to PDF string. How can I change the #9 character to a working tabstop character? I can change the #9 character to 7 pieces of #20, but it is not good for me, because I and W are different widths.

Is there a trick? Like horizontal spacing in percent?

(ID) Tj

some code that spacing 100 horizontal pixels

(NAME) Tj

some code that spacing 100 horizontal pixels

(VALUE) Tj


Solution

  • Your #9 seems to be ^09 i.e. (HT)

    That should be x09 in (Base 16 / hex.) or \011 (base 8) or \t in literal string

    IF defined like that in a base font, then you should be able to insert that.

    (ID\t\tNAME\t\tVALUE) TJ

    enter image description here

    or

    (ID\011\011NAME\011\011VALUE) Tj

    However as pointed out by @mkl those were traditional mechanical printer carriage stops that could be set ON at 4 or 8 characters from line left or anything the printer operator chose to place indents or columns. Thus in a Word Processor are highly variable in number and position. But in a PDF are usually ignored.

    In PDF it is more conventional to set each block of characters at a new x,y position, where y is constant for each text block at that elevation.

    So for a tab stop approach with tabs at one inch (based on default 1 unit =1/72") try this

    stream
    q
    
    BT
    /F1 12 Tf
    1 0 0 1 144 720 Tm
    (ID) Tj
    ET
    
    BT
    /F1 12 Tf
    1 0 0 1 216 720 Tm
    (NAME) Tj
    ET
    
    BT
    /F1 12 Tf
    1 0 0 1 288 720 Tm
    (VALUE) Tj
    ET
    Q
    
    endstream
    

    Remember in PDF all whitespace is equal, but some is more so than others.

    so here find id name value accepts the non existent tabs as a single white space:-

    enter image description here

    Finally to answer your query you can set fixed space from the start of a text to the start of another just like tab stops using Td. Note I had intentionally used TD and Td mixed to show it does not matter in such cases :-), however the Human readable convention is to use CAPS for the Object (Noun) and lowercase the "action" (Verb) so Td is better for debugging by eye.

    enter image description here

    This can be written as suggested by @mkl (with me adding a start point)

    50 800 Td (ID) Tj 100 0 Td (NAME) Tj 100 0 Td (VALUE) Tj
    

    In comments you asked about adding lines and the simplest, for line by line loop programming, is to use something like this. (In this case skipping 780) and contra my above comment BT and ET are usually both CAPS.

    BT 50 800 Td (ID) Tj 100 0 Td (NAME) Tj 100 0 Td (VALUE) Tj ET
    BT 50 760 Td (A1) Tj 100 0 Td (Example) Tj 100 0 Td (2000) Tj ET
    BT 50 740 Td (B2) Tj 100 0 Td (Another) Tj 100 0 Td (1000) Tj ET
    

    enter image description here