This quickstart walks you through adding PDFBox to a Java project, creating a PDF that contains a line of text, and extracting text from an existing PDF. By the end you will have a working setup and two runnable Java programs to build on.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/apache/pdfbox/llms.txt
Use this file to discover all available pages before exploring further.
PDFBox 3.x requires Java 11 or higher. Make sure your
JAVA_HOME points to a compatible JDK before proceeding.Add the dependency
Add the If you use encryption or digital signatures, also add the Bouncy Castle provider. PDFBox 3.x uses Bouncy Castle 1.7x or later.
pdfbox artifact to your build. The groupId is org.apache.pdfbox and the latest stable release is 3.0.0.Create a blank PDF
The simplest possible PDFBox program creates a one-page document and saves it to disk. A valid PDF must contain at least one page.
CreateBlankPDF.java
PDDocument implements AutoCloseable, so wrapping it in a try-with-resources statement ensures the document and any underlying scratch files are closed correctly.Write text to a PDF
To add content to a page, open a Coordinates in PDFBox follow the PDF convention: the origin
PDPageContentStream and use the text operators. The example below uses the built-in Helvetica Bold font from the PDF standard-14 set, which requires no font embedding.HelloWorld.java
(0, 0) is at the bottom-left corner of the page, and Y increases upward. An A4 page is 595 × 842 points; a US Letter page is 612 × 792 points.Extract text from a PDF
Use
Loader.loadPDF() to open an existing PDF, then PDFTextStripper to extract the text. The example below iterates page-by-page and prints each page’s content to standard output.ExtractTextSimple.java
Build and run
Compile and run with Maven:Or build a fat JAR and run directly:You should see each page’s text printed to the console. If a page prints empty or garbled characters, the PDF may use a non-standard font encoding. Consult the text extraction guide for troubleshooting steps.