Pdf Report Generation in Java Example

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:

  • Apache PDFBox
  • iText
  • PDF Clown
  • BFO
  • PJX
  • PDFjet
  • jPOD
  • ICEpdf

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.

Java Create PDF

Apache PDFBox

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:

  • Extract Text: The library allows us to extract Unicode from a PDF file.
  • Split and Merge: We can also split a PDF file into many PDFs and vice-versa.
  • Fill Forms: Extract data from PDF forms or fill a PDF form.
  • Preflight: Validate PDF files against the PDF/A-1b standard.
  • Print: The feature allows us to print a PDF file using the standard Java printing API.
  • Save as Image: We can also save PDFs as image files like JPEG and PNG.
  • Create PDFs: It allows us to create a PDF with embedded font and image.
  • Signing: The feature allows us to digitally sign PDF files.

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.

Java Create PDF

Step 2: Under the Current releases section, download all the JAR files for PDFBox 3.0.0-RC1 release.

Java Create PDF

Download all the JAR files one by one.

  1. pdfbox-app-3.0.0-RC1.jar
  2. debugger-app-3.0.0-RC1.jar
  3. preflight-app-3.0.0-RC1.jar
  4. pdfbox-3.0.0-RC1.jar
  5. fontbox-3.0.0-RC1.jar
  6. preflight-3.0.0-RC1.jar
  7. xmpbox-3.0.0-RC1.jar
  8. pdfbox-tools-3.0.0-RC1.jar
  9. pdfbox-debugger-3.0.0-RC1.jar

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:

Java Create PDF

Step 5: Create a class file with the name CreatePdf and write the following code into it.

CreatePdf.java

Output:

Java Create PDF

Let's access the location that we have provided in the program. We get a blank PDF file named Sample, at the specified location.

Java Create PDF

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 Library

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

  • Generate dynamic documents from XML files or databases.
  • Add bookmarks, page numbers, watermarks, etc.
  • Split, concatenate and manipulate PDF pages.
  • Automate filling out PDF forms.
  • Add digital signatures to a PDF file.

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-5.5.13.2.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.

GeneratePDF.java

Output:

Java Create PDF

Let's access the location that we have provided in the program.

Java Create PDF

We get a PDF file named Motivation.pdf inside the Java Folder. Now open the PDF file and see what is written inside it.

Java Create PDF

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.

Java Create PDF

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.

GenerateProtectedPdf.java

Output:

Java Create PDF

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.

Java Create PDF

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:

Java Create PDF

The library also allows us to create PDFs with limited permissions.

PDF Clown

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.

  • Powerful content manipulations.
  • Enhance page rendering
  • Styled page composition
  • Extensive enhancements throughout the codebase.
  • PDF rasterizer helps in content rendering and printing.
  • Allows creating, reading and modifying bookmarks.
  • Allows to create, read, and modify Acroform fields.
  • Supports content grouping into switchable layers.
  • Advanced text extraction functionalities.

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.


aronsondinione.blogspot.com

Source: https://www.javatpoint.com/java-create-pdf

0 Response to "Pdf Report Generation in Java Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel