Add text to Pdf using Apache PDFBox

In our last post we had seen How to Create Simple PDF using Apache PDFBox.
Now in this post we will see steps to add text to PDF using Apache PDFBox.

The PDPageContentStream is the class from the PDFBox library that is used to write text.
Below are few of the methods from this class that will be used

1. beginText() – Begins the text operation

2. newLineAtOffset(x,y) – Moves to the new lines and determines the X and Y positions for the text.

3. setFont(name,size) – Sets the desired font and its size.

4. showText(text) – Shows the text at the given position

5. endText() – Ends the text operation

Lets see an examples for this. We have used 2 different fonts for testing text in various fonts in the PDF.

Add text to Pdf using Apache PDFBox

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