PDF Low Level: Text Rendering

I am trying to render text to PDF. I can draw vector graphics, but I would also like to accompany it with text.

In the below test code, the file (when saved as a .pdf) will display "Test" in the upper left corner, drawing it with vector graphics.

I would like to write 'Test' using the following commands text

BT
/F5 16 Tf
100 100 Td
(Test) Tj
ET

      

or

BT
9.6 0 0 9.6 200 200 Tm
/f-0-0 1 Tf
[<0001>169<000200030004>]TJ
ET

      

However, the required text does not appear to be generated. Is there anything extra I need to do with the command Tf

and where can I include it in my .pdf source file?

You can download the file (change extension to .pdf) using this pastebin source:

%PDF-1.7
    %ยฟรทยขรพ
    %QDF-1.0
    
    %% Original object ID: 6 0
    1 0 obj
    <<
      /Pages 3 0 R
      /Type /Catalog
    >>
    endobj
    
    %% Original object ID: 5 0
    2 0 obj
    <<
      /Author (None)
      /CreationDate (D:20150109100433)
      /Creator (None)
      /Keywords (None)
      /ModDate (D:20150109100433)
      /Producer (None)
      /Subject (None)
      /Title (test.pdf)
    >>
    endobj
    
    %% Original object ID: 1 0
    3 0 obj
    <<
      /Count 1
      /Kids [
        4 0 R
      ]
      /Type /Pages
    >>
    endobj
    
    %% Page 1
    %% Original object ID: 3 0
    4 0 obj
    <<
      /Annots [
      ]
      /Contents 5 0 R
      /MediaBox [
        0
        0
        612
        792
      ]
      /Parent 3 0 R
      /Resources 7 0 R
      /Type /Page
    >>
    endobj
    
    %% Contents for page 1
    %% Original object ID: 4 0
    5 0 obj
    <<
      /Length 6 0 R
    >>
    stream
    BT
    /F5 16 Tf
    100 100 Td
    (Test 1) Tj
    ET
    BT
    9.6 0 0 9.6 200 200 Tm
    /f-0-0 1 Tf
    [<0001>169<000200030004>]TJ
    ET
    1 0 0 -1 0 792 cm
    0.45 0 0 0.45 0 0 cm
    1 0 0 1 0 0 cm
    0 0 m 1360 0 l 1360 1760 l 0 1760 l h
    q
    1 1 1 rg f
    Q
    q
    1 0 0 1 0 0 cm
    q
    q
    q
    q
    q
    1 0 0 1 96 113.6 cm
    0.05 0 0 0.05 0 0 cm
    1 0 0 1 0 -792 cm
    q
    1 0 0 1 0 0 cm
    1 0 0 1 0 792 cm
    1 0 0 1 0 0 cm
    q
    127 -220 m 127 0 l 93 0 l 93 -220 l 8 -220 l 8 -248 l 212 -248 l 212 -220 l 127 -220 l
    q
    0 0 0 rg f
    Q
    Q
    Q
    q
    1 0 0 1 179 0 cm
    1 0 0 1 0 792 cm
    1 0 0 1 0 0 cm
    q
    100 -194 m 163 -194 186 -152 184 -88 c 49 -88 l 49 -48 63 -21 102 -20 c 128 -19 145 -32 151 -49 c 179 -41 l 168 -13 142 4 102 4 c 44 4 14 -33 15 -96 c 16 -157 41 -194 100 -194 c h 152 -113 m 158 -173 76 -190 55 -141 c 52 -134 49 -124 49 -113 c 152 -113 l
    q
    0 0 0 rg f
    Q
    Q
    Q
    q
    1 0 0 1 379 0 cm
    1 0 0 1 0 792 cm
    1 0 0 1 0 0 cm
    q
    135 -143 m 132 -177 49 -181 48 -143 c 63 -90 163 -131 167 -53 c 171 25 17 21 10 -45 c 38 -50 l 42 -14 135 -5 136 -50 c 126 -106 23 -65 18 -140 c 14 -197 100 -203 140 -182 c 152 -175 161 -163 164 -147 c
    q
    0 0 0 rg f
    Q
    Q
    Q
    q
    1 0 0 1 559 0 cm
    1 0 0 1 0 792 cm
    1 0 0 1 0 0 cm
    q
    59 -47 m 57 -23 77 -18 97 -25 c 97 -1 l 64 9 27 4 27 -40 c 27 -167 l 5 -167 l 5 -190 l 29 -190 l 38 -233 l 59 -233 l 59 -190 l 94 -190 l 94 -167 l 59 -167 l 59 -47 l
    q
    0 0 0 rg f
    Q
    Q
    Q
    q
    1 0 0 1 659 0 cm
    1 0 0 1 0 792 cm
    1 0 0 1 0 0 cm
    q
    
    q
    0 0 0 rg f
    Q
    Q
    Q
    Q
    Q
    endstream
    endobj
    
    6 0 obj
    1772
    endobj
    
    %% Original object ID: 2 0
    7 0 obj
    <<
      /ExtGState <<
      >>
      /Font <<
      >>
      /ProcSet [
        /PDF
        /Text
        /ImageB
        /ImageC
        /ImageI
      ]
      /Shading <<
      >>
      /XObject <<
      >>
    >>
    endobj
    
    xref
    0 8
    0000000000 65535 f 
    0000000052 00000 n 
    0000000133 00000 n 
    0000000391 00000 n 
    0000000500 00000 n 
    0000000701 00000 n 
    0000002528 00000 n 
    0000002576 00000 n 
    trailer <<
      /Info 2 0 R
      /Root 1 0 R
      /Size 8
      /ID [<78c80e1b6299c4ee49d43acfeb2655cf><78c80e1b6299c4ee49d43acfeb2655cf>]
    >>
    startxref
    2745
    %%EOF
      

Run codeHide result


Despite the addition of text commands, I still only see vector art. http://i.stack.imgur.com/BMvhK.png

+3


source to share


2 answers


Your PDF does not include the actual font objects associated with the names /F5

and /f-0-0

therefore the font is not displayed to display the text. The dictionary /Font

on the page is /Resources

empty.



In this situation, some PDF viewers will use the default font to display the text, while others will display nothing.

+5


source


As already pointed out in its response @MihaiIancu (which you have to move, as well as to take!), The code in your PDF files are used /F5

and /f-0-0

as a font resource file but nowhere defines what these resources are meant.

Here is my "patched" PDF source code from your Pastebin. It should provide a correct "minimum viable example" PDF using two different fonts (not embedded) and not providing an (optional) dictionary /Info

:

%PDF-1.7

1 0 obj
<<
  /Pages    3 0 R
  /Type     /Catalog
>>
endobj

2 0 obj
<<
  /BaseFont /Helvetica
  /Subtype  /Type1
  /Type     /Font
>>
endobj

3 0 obj
<<
  /Count    1
  /Kids     [ 4 0 R ]
  /Type     /Pages
>>
endobj

4 0 obj
<<
  /Contents     5 0 R
  /MediaBox     [ 0 0 612 792 ]
  /Parent       3 0 R
  /Resources    7 0 R
  /Type /Page
>>
endobj

5 0 obj
<<
  /Length       6 0 R
>>
stream
BT
/F5 16 Tf
100 100 Td
(Test) Tj
ET
BT
9.6 0 0 9.6 200 200 Tm
/f-0-0 10  Tf
[<0054>169<006500730074>]TJ
ET
endstream
endobj

6 0 obj
 108
endobj

7 0 obj
<<
  /Font     8 0 R
>>
endobj

8 0 obj
<<
  /F5       2 0 R
  /f-0-0    9 0 R
>>
endobj

9 0 obj
<<
  /BaseFont /Helvetica-Bold
  /Subtype  /Type1
  /Type     /Font
>>
endobj

xref
0 10
0000000000 65535 f 
0000000010 00000 n 
0000000071 00000 n 
0000000153 00000 n 
0000000230 00000 n 
0000000364 00000 n 
0000000533 00000 n 
0000000554 00000 n 
0000000594 00000 n 
0000000652 00000 n 
trailer <<
  /Root    1 0 R
  /Size    10
  /ID      [<78c80e1b6299c4ee49d43acfeb2655cf><78c80e1b6299c4ee49d43acfeb2655cf>]
>>
startxref
739
%%EOF

      



And here's another version of it: stripped of most newlines and spaces, equally valid as the first, but less readable and slightly shorter.

%PDF-1.7

1 0 obj
<</Pages 3 0 R/Type/Catalog>>
endobj

2 0 obj
<</BaseFont/Helvetica/Subtype/Type1/Type/Font>>
endobj

3 0 obj
<</Count 1/Kids[ 4 0 R ]/Type/Pages>>
endobj

4 0 obj
<</Contents 5 0 R/MediaBox[0 0 612 792]/Parent 3 0 R/Resources 7 0 R/Type/Page>>
endobj

5 0 obj
<</Length 6 0 R>>
stream
BT
/F5 16 Tf 100 100 Td(Test)Tj
ET
BT
9.6 0 0 9.6 200 200 Tm/f-0-0 10 Tf[<0054>169<006500730074>]TJ
ET
endstream
endobj

6 0 obj
103
endobj

7 0 obj
<</Font 8 0 R>>
endobj

8 0 obj
<</F5 2 0 R/f-0-0 9 0 R>>
endobj

9 0 obj
<</BaseFont/Helvetica-Bold/Subtype/Type1/Type/Font>>
endobj

xref
0 10
0000000000 65535 f 
0000000010 00000 n 
0000000056 00000 n 
0000000120 00000 n 
0000000174 00000 n 
0000000271 00000 n 
0000000425 00000 n 
0000000445 00000 n 
0000000477 00000 n 
0000000519 00000 n 
trailer <<
  /Root 1 0 R
  /Size 10
  /ID [<78c80e1b6299c4ee49d43acfeb2655cf><78c80e1b6299c4ee49d43acfeb2655cf>]
>>
startxref
588
%%EOF

      

+2


source







All Articles