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.


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

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 {

dependencies {

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

<project xmlns="" xmlns:xsi=""
		<java.version>at least 1.8</java.version>

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

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.");

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);

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


Thanks for reading.


Leave a Reply

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