Skip to content
Advertisement

itext 7 pdf how to prevent text overflow on right side of the page

I am using itextpdf 7 (7.2.0) to create a pdf file. However even though the TOC part is rendered very well, in the content part the text overflows. Here is my code that generates the pdf:

JavaScript

Here are the screen shots of TOC part and content part :

TOC :

TOC

Content :

Content part

What am I missing? Thank you all for your help.

UPDATE

When I add the line below it renders with no overflow but the page margins of TOC and content part differ (the TOC margin is way more than the content margin). See the picture attached please :

JavaScript

Right Margin difference between TOC and content:  Right Margin difference between TOC and content

UPDATE 2 :

When I comment the line

JavaScript

and set the font size on line :

JavaScript

to 12 then it renders fine. What difference should possibly the font size cause for the page content and margins? Are not there standard page margins and page setups? Am I unknowingly (I am newbie to itextpdf) messing some standard implementations?

Advertisement

Answer

TL; DR: either remove setFontSize in

JavaScript

or change setFontSize(10) -> setFontSize(12) in

JavaScript

Explanation: You are setting the Document to not immediately flush elements added to that document with the following line:

JavaScript

Then you add an paragraph element with font size equal to 10 to the document with the following line:

JavaScript

What happens is that the element is being laid out (split in lines etc), but now drawn on the page. Then you do .setFontSize(12) and this new font size is applied for draw only, so iText calculated that X characters would fit into one line assuming the font size is 10 while in reality the font size is 12 and obviously fewer characters can fit into one line.

There is no sense in setting the font size two times to different values – just pick one value you want to see in the resultant document and set it once.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement