How to calculate the string width in iText?

MemoryLeak picture MemoryLeak · Oct 6, 2009 · Viewed 31.2k times · Source

I am using iText to write a PDF. In some cases, I need to sign the PDF with the SetVisibleSignature function. With this function, we need to designate the rectangle that we will write the content into.

But it's hard for me to calculate how wide the string will be, so that I can set the rectangle before setting a signature on the PDF.

How can I calculate the string width in iText?

Answer

Jan Soltis picture Jan Soltis · Oct 26, 2009

You can use BaseFont.getWidthPoint(String text, float fontSize) to get the width of the string in pt.

Or put the string in a Chunk and do chunk.getWidthPoint()