Add multiple lines in PDF using Apache PDFBox

When you large content to be displayed in the PDF using the pdPageContentStream.showText(str) the content can sometimes go out of the PDF and get cut. See below example :

Add multiple lines in PDF using Apache PDFBox

To avoid this we need some processing to be done of the content, so that it is converted into an array that can be printed without cutting any data.

Steps to Add multiple lines in PDF using Apache PDFBox

1) Define all the constants like X, Y coordinates, font, font size and the pdf margin

2) Using the PDRectangle class get the allowed printable width inside the pdf

3) Inside a While loop, check the length of the content Vs the printable width. If content length is greater, then cut the length into the printable width allowed. Add that to the array. Process the remaining content again in loop.

4) IMPORTANT : Setting a leading to avoid the overlapping of text

5) Loop the array and print the lines one by one. Use pdPageContentStream.newLine() after each line.

Example

Output

Add multiple lines in PDF using Apache PDFBox

Reference
1)Apache PDFBox

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn