How to create PDF File Content using Java

Introduction

In this tutorial I am going to show you how to create PDF file content with different formats using Java programming language. In this tutorial I am going to use iText library to generate the PDF document.

Many applications require dynamic generation of PDF documents. The applications, where dynamically generated PDF is required, are banking system generating statements for customers, readers buying online specific chapters of a book, etc.

The iText library is freely available and can be used using maven or gradle build tool. We can also generate HTML, RTF, XML documents using iText library apart from PDF documents.

We will see how to use different fonts, colors on text. We will create paragraphs, anchor link, chapter, list items, table, etc. We will also insert an image into the PDF document. We will create another an anchor link which will take you to the top of the PDF document.

Prerequisites

Eclipse 2019-12, At least Java 8, Gradle 6.4.1, Maven 3.6.3, iText 5.5.13.1

Project Setup

Create a gradle or maven based project in Eclipse. The name of the project is java-create-pdf-file-content.

If you are creating a gradle based project then you can use below build.gradle script:

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
    implementation('com.itextpdf:itextpdf:5.5.13.1')
}

If you are creating a maven based project then you can use below pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>com.roytuts</groupId>
	<artifactId>java-create-pdf-file-content</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>at least 1.8</java.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13.1</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
                <configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Generate PDF Document

Let’s move on to generate PDF document. The first step is to create a document which is the container for containing all elements of a pdf document.

Document document = new Document(PageSize.A4, 25, 25, 25, 25);

The first argument is size of the page and next arguments are left, right, top and bottom margins, respectively.

We need to define the type of this document, for example, for PDF we need PdfWriter. So we are going to define a writer for the document.

PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(fileName));

The first argument is the reference to the document object, and the second argument is the absolute name of the file in which output will be written. Next, we open the document for writing using document.open();.

We create an anchor target. This target will be shown when we click on an anchor link from other page of the document.

Anchor anchorTarget = new Anchor("First page of the document.");
anchorTarget.setName("BackToTop");

Next we will create several paragraphs with different text formats and styles.

Then we will create chapter. A chapter is a special section that starts with a new page and has a number displayed by default. Setting number depth to 0 will not display the chapter number on page.

Paragraph title2 = new Paragraph("Chapter 2",
		FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new BaseColor(0, 255, 255)));
Chapter chapter2 = new Chapter(title2, 1);
chapter2.setNumberDepth(0);

Then we have created table, list items followed by inserted an image.

We have finally created back to top link, clicking on which will take to the top page of the PDF document.

That’s all about generating PDF document dynamically using Java programming language with the help of iText library.

The final output PDF file can be downloaded from the source code section below.

Source Code

Download

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *