Creating some dynamic text in my PDFs


I have to create some dynamic text in my PDFs. Essentially I have a box and would like to fill it with aligned or centered text.

My problem is that I have to create the box after the text because it has to be a little larger than the text.

So I can create a box before the text but I don’t know the exact size because I cannot use the gettextlenght on the element because I have to create the text after the box.

How can I achieve it? Is there a method which I can use to know the length of a text before to write it on the pdf?


The following method should work:

  1. create the text element (don’t write it) get the text length using GetTextLength

  2. create the box and write it

  3. create the text element again and write it

This should work as long as you aren’t modifying the transformation matrix during the process.

For example see: ElementBuilder sample project (the part that writes a paragraph).