Create Simple PDF using Apache PDFBox

What is Apache PDFBox?

Apache PDFbox is an open source java library used for working with PDF files. We can create new files, modify existing files, read files and do many other manupulations using Apache Pdfbox.

In this post lets see the steps to create simple pdf using Apache Pdfbox library.

1. Setup the environment

For creating a simple PDF file you can use the following maven dependency

If you are working on a simple java project you can add the following jar into the classpath
pdfbox-2.0.10.jar
You can download it from Apache PDFBox site.Apache PDFBox Downloads
Please note: for writing into the pdf you will also need another jars. We will explain that in our next post.

2. Java Code

For creating a simple pdf we require 2 important classes from the PDFBox library
a) org.apache.pdfbox.pdmodel.PDDocument;
b) org.apache.pdfbox.pdmodel.PDPage;

PDDocument is the in-memory representation of the PDF document, while PDPage is representation of a Page in the pdf file.

You can only use PDDocument for creating a file without any pages, but if you try to open the file you may get an error. Hence adding a page to the document becomes a mandatory step.

Output : A pdf gets created at the specified location.
create simple pdf using apache pdfbox

In our next few posts we will see more examples on Apache PDFBox.

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