Apache PDFBox Create PDF document in Java
This tutorial demonstrates how to create a PDF document using Apache PdfBox. In the first example we show how to create an empty PDF document. The second example adds some simple text to the PDF document.
Apache PdfBox
The Apache PDFBox® library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. Apache PDFBox also includes several command-line utilities. Apache PDFBox is published under the Apache License v2.0.
Maven Dependencies
We use Apache Maven to manage our project dependencies. Make sure the following dependencies reside on the class-path.
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.8</version>
</dependency>
Apache PDFBox Create Empty PDF Document
We create a new PDF Document using the new PDDocument
. We create a new PDPage
and add it to the document using doc.addPage(page)
. To save the file we simply use doc.save(new File("/location"))
. The try-with-resources automatically closes the appropriate streams.
package com.memorynotfound.pdf.pdfbox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import java.io.File;
import java.io.IOException;
public class EmptyPdf {
public static void main(String... args) {
try (final PDDocument doc = new PDDocument()){
PDPage page = new PDPage();
doc.addPage(page);
doc.save(new File("/tmp/empty.pdf"));
} catch (IOException e){
System.err.println("Exception while trying to create pdf document - " + e);
}
}
}
Apache PDFBox Create PDF Document with Text.
In the following example we add some simple text to the PDF document. We create a PDPageContentStream
which is responsible to add content in form of a stream. To start writing text we use the beginText();
method. Next we have to set the font and the start position. Now we are able to write some text to the PDF document using showText("text")
. Finally we close the PDPageContentStream
and save the document.
package com.memorynotfound.pdf.pdfbox;
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;
import java.io.File;
import java.io.IOException;
public class SimplePdf {
public static void main(String... args) {
try (final PDDocument doc = new PDDocument()){
PDPage page = new PDPage();
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setFont(PDType1Font.HELVETICA, 12);
content.newLineAtOffset(100, 700);
content.showText("Apache PDFBox Create PDF Document");
content.endText();
content.close();
doc.save(new File("/tmp/simple.pdf"));
} catch (IOException e){
System.err.println("Exception while trying to create pdf document - " + e);
}
}
}
Demo
The previous example generates the following PDF document.
References
- Apache PdfBox Official Website
- Apache PdfBox API Javadoc
- PDDocument JavaDoc
- PDPage JavaDoc
- PDPageContentStream JavaDoc