Alasdair
Alasdair

Reputation: 14151

PDF Low-Level: Adding text as an invisible layer with each letter in specific position

I'm writing a PDF file directly from code, it's all working nicely, but I don't know how to add text into the content object of a page with each letter at a specific position.

I have the coordinates of each letter, something like this:

    x0  y0  x1  y1
a = 345,200,350,210
n = 352,201,360,209
d = 365,200,371,212

I want to be able to put this onto the PDF page as an invisible layer so it can be searched or selected, but with each letter in the exact correct coordinates.

Alternatively I could do it with only the coordinates for each word, if this is better.

What is the format for writing this into the content object?

Thank you very much for your help!

Upvotes: 0

Views: 288

Answers (1)

Jimmy
Jimmy

Reputation: 6171

There are many ways of doing this. You'll need to use a text block:

BT 
%..you need to set a font... 
/f1 10 Tf
%..you need to set the text matrix to include Tx and Ty (if not already done)..
1 0 0 1 345 200 Tm
(a) Tj % or (and) Tj to display the word in one go (position of chars depends on font selected)
1 0 0 1 352 201 Tm
(n) Tj
% etc.
ET

You also mentioned that you wanted the text to be invisible. If you are in complete control of the page content you can set the text stroke and fill colour to be the same as the background colour (which will probably be white)

1 1 1 RG
1 1 1 rg

Otherwise you can paint over the text, it will still be selectable.

Upvotes: 2

Related Questions