Alasdair
Alasdair

Reputation: 14151

PDF Low Level - Writing text in absolute location

I'm writing a PDF file directly from code.

I'm now at the stage where I'm writing text onto the page, and have all the words and coordinates, but I can't get it to put them in exactly the right place. Acrobat thinks that each word should be on a new line and it ends up looking like this:

word
     word
          word
               word

Instead of:

word word word word

This is what I'm using:

q
Tr 3
 BT
  /F1 8 Tf
  10 1000 Td
  (word)Tj
 ET
 BT
  /F1 8 Tf
  50 1000 Td
  (word)Tj
 ET
Q

I know that Td is likely wrong, but I can't find what the correct command is to do this. I have the PDF specification open, but it's not very clear - doesn't explain how to position each word absolutely.

Upvotes: 1

Views: 511

Answers (1)

Andrew Cash
Andrew Cash

Reputation: 2394

This code seems to work

q 
  0 Tr 
  /Helv 12 Tf 
  BT 
    1 0 0 1 10 10 Tm 
    (Hello)Tj
  ET
  BT 
    1 0 0 1 90 10 Tm 
   (World)Tj
  ET
Q

'1 0 0 1 10 10 Tm' sets the text matrix with a scaling of 1,1 and x,y at 10, 10.

There are ways to combine text strings into a string of commands to make it more efficient.

Upvotes: 1

Related Questions