Java Create PDF
In the previous sections, we have learned about Apache POI Library. It is an API that deals with Microsoft Office Documents. Using the POI Library, we can easily create DOC, DOCX, XLS, XLSX, PPT, and PPTX files. What if we want to create a PDF file in Java using the Java program? In this section, we are going to learn how to create PDF using a Java program.
There are various libraries that can be used to create a PDF file using a Java program. Some of them are:
The first two libraries are more popular and widely used by developers. So, in this section, we will discuss only Apache PDFBox, iText, and PDF Clown library.
It is an open-source library that can be used while dealing with PDF documents. It includes several command-line utilities. It allows us to create PDFs, read, and manipulate PDFs. It provides a variety of fonts. The latest release of the library is Apache PDFBox 3.0.0-RC1. The library has the following features:
How to Use Apache PDFBox?
First, we have to install the library. In order to install the library, follow the steps given below:
Step 1: Go to the PDFBox official site and download the PDFBox library. It will land you on the home page.
Step 2: Under the Current releases section, download all the JAR files for PDFBox 3.0.0-RC1 release.
Download all the JAR files one by one.
Step 3: Once the required JARs are download, open eclipse IDE and create a Java project.
Step 4: Add all the downloaded JRAs in the Path. Right-click on the project -> Build Path -> Configure Build Path -> Add External JARs -> Select the all the downloaded JARs -> Open -> Apply -> Apply and Close. After performing all the steps, the project directory looks like the following:
Step 5: Create a class file with the name CreatePdf and write the following code into it.
Let's access the location that we have provided in the program. We get a blank PDF file named Sample, at the specified location.
If you do not want to perform the above steps, just create a maven project and add the following dependency in the pom.xml file.
After that continue from step 5.
iText is also an open-source Java library that deals with PDFs. It is written in Java and .NET. Using iText, we can create PDF files, dynamically. It is a powerful API that supports the generation of HTML, RTF, and XML documents in addition to generation of PDFs. It provides features like a watermark, generates tables in PDF, fonts, and so on. Although it is free if you want to use it commercially then you will have to pay a nominal fee.
Features of iText
The library contains the number of classes that can be used for different purposes. The most important class of the library is the Document class that belongs to com.itextpdf.text package. It represents the PDF document instance. There is another important class named PdfWriter that writes text into the PDF. It also belongs to the same package.
Before running the program, download itextpdf-184.108.40.206.jar file and add it the path (follow step 4) of your project. If you have created a maven project for the same, add the following dependency in the pom.xml file.
Let's create a Java program and generate a PDF using iText library.
Let's access the location that we have provided in the program.
We get a PDF file named Motivation.pdf inside the Java Folder. Now open the PDF file and see what is written inside it.
We can also set the attributes (author, date, title, subject, etc.) to the PDF file.
Once, the attributes are settled up, we can see it in the Document Properties.
By using the iText library, we can also generate a password-protected PDF file.
Generating a Password Protected PDF
For generating a password-protected file, we need to use an encryption library named BoucnyCastle.
The BouncyCastle is an encryption library that contains the Java implementation of cryptographic algorithms. It is used to generate password-protected PDF files. We can download the bcprov-jdk15on-1.69.jar file from the maven repository. After downloading add it to the project.
We can also add the dependency to the pom.xml file.
The PdfWriter class provides a method setEncryption() that is used to set password to generate PDF.
Let's create a Java program that creates a password protected PDF file.
Let's access the location that we have provided in the program. We get a file named Account Details.pdf. When we try to open this file, it asks for providing the password.
In order to open the password-protected file, we can provide any of the passwords i.e. USER_PASSWORD = "1234567" or OWNER_PASSWORD = "javatpoint". When we provide any of the passwords, the PDF file shows the following details:
The library also allows us to create PDFs with limited permissions.
PDF Clown is another open-source and popular Java and .NET library that also deals with PDFs. Apart from the normal operations (create, read, and manipulate), there are some other major functionalities offered by PDF Clown are as follows.
Similarly, we can create a Java program to generate PDF by using the PDF Clown library. Before running the program ensure that the library is properly added to the project.