pdfpdf-generationitexttext-width

How to calculate the string width in iText?


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?


Solution

  • 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()