Creating PDF Document File in Java using Apache PDFBox

Introduction

In this tutorial we will learn how to create a PDF document file in Java application using Apache PDFBox library.

Apache PDFBox Overview

The Apache PDFBox is an open source library for working with PDF documents in Java. You can get more information about the project at pdfbox.apache.org

Adding Apache PDFBox Dependencies

Adding below dependencies to build.gradle file if you are using Gradle build tool.

compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.18'

Adding below XML to pom.xml file if you are using Maven build tool.

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.18</version>
</dependency>

Or you can download the pdfbox-2.0.18.jar file from pdfbox.apache.org/download.cgi

Creating empty PDF document

To create a new empty document we need create new instance of PDDocument.

PDDocument document = new PDDocument();

Adding new empty page to PDF document

To add a new blank page to the document we create new instance of PDPage.

PDPage page = new PDPage();
document.addPage(page);

Adding text to PDF document page

To add text to the document page we use PDPageContentStream object to adding text as below.

PDPageContentStream pageContentStream = new PDPageContentStream(document, page);
pageContentStream.beginText();
pageContentStream.setFont( PDType1Font.TIMES_ROMAN , 12 );
pageContentStream.newLineAtOffset(100,  700);
pageContentStream.showText("Welcome to simplesolution.dev");
pageContentStream.endText();
pageContentStream.close();

Save PDF document

Finally we need to save PDF document to the disk, for example we save the new PDF file to D:\SimpleSolution\Document.pdf

document.save("D:\\SimpleSolution\\Document.pdf");

Complete Application Source Code

package dev.simplesolution;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class CreatingPdfDocument {

	public static void main(String[] args) {
		try(PDDocument document = new PDDocument()) {
			PDPage page = new PDPage();
			document.addPage(page);
			
			PDPageContentStream pageContentStream = new PDPageContentStream(document, page);
			pageContentStream.beginText();
			pageContentStream.setFont( PDType1Font.TIMES_ROMAN , 12 );
			pageContentStream.newLineAtOffset(100,  700);
			pageContentStream.showText("Welcome to simplesolution.dev");
			pageContentStream.endText();
			pageContentStream.close();
			
			document.save("D:\\SimpleSolution\\Document.pdf");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Execute the application above you will get the Document.pdf file as below.

Creating PDF Document File in Java using Apache PDFBox

The source code in this article can be found at: github.com/simplesolutiondev/ApachePDFBoxCreatingPdfDocument

or download at:

Download Source Code

Happy Coding 😊

Insert Image to PDF Document in Java using Apache PDFBox

Apache PDFBox Adding Metadata to PDF Document in Java

Creating Bookmarks for PDF Document in Java with Apache PDFBox

Creating PDF Document Page Labels in Java with Apache PDFBox