Apache PDFBox Documentation
repository·trunk·Indexed 25 days ago
https://github.com/apache/pdfboxAn open-source Java library for creating new PDF documents, manipulating existing ones, and extracting content. The project includes subprojects such as Apache FontBox and Apache XmpBox, which implements Adobe's XMP specification. It also provides command-line utilities for PDF tasks and a PDF Debugger App.
What's inside Apache PDFBox
- Apache XmpBox is an open source Java library that implements Adobe's XMP (Extensible Metadata Platform) specification. It is a subproject of Apache PDFBox.
Build Apache PDFBox from source
trunkTo build PDFBox, you must have Java 11 (or higher) and Maven 3 installed. The default build process compiles the Java sources and packages the binary classes into jar packages.
mvn clean installUse async-profiler with JMH benchmarks
trunkYou can use
async-profilerto profile benchmarks. You must provide the path to thelibasyncProfiler.solibrary.- Basic profiling: Use
-prof async:libPath=<path>. - Flame graph output: Use
-prof async:libPath=<path>\;output=flamegraph(note the escaped semicolon). - Help: Use
-prof async:helpto see available options.
- Basic profiling: Use
Build Apache XmpBox from source
trunkTo build XmpBox, you need Java 6 or higher and Maven 2. The default build compiles Java sources and packages the binary classes into a JAR package.
mvn clean installDownload Apache PDFBox binaries
trunkBinary versions for current development and older releases can be downloaded from the official Apache PDFBox download page.Build Apache FontBox
trunkTo build FontBox from source, you must have Java 6 or higher and Maven installed. The default build process compiles the Java source code and packages the binary classes into a JAR file.
mvn clean installPrepare Ghent PDF Output Suite V50 for Rendering benchmarks
trunkTo run the
Rendering.renderGhentCMYKbenchmark, you must manually prepare the test PDF files because the Ghent PDF Output Suite V50 requires a license agreement.- Download the suite from the Ghent PDF Output Suite V50 download page.
- Unpack the ZIP file into the
target/pdfsdirectory so that theGhent_PDF_Output_Suite_V50_Fulldirectory is at the root of that path. - Retain only the following file:
Ghent_PDF_Output_Suite_V50_Full/Categories/1-CMYK/Test pages/Ghent_PDF-Output-Test-V50_CMYK_X4.pdf.
Use GC profiler to measure allocation rates
trunkTo measure memory allocation rates, use the
-prof gcprofiler. When profiling, focus on thenormmetric, which measures allocations per operation rather than allocations per second, providing a more stable measurement regardless of code execution speed.java -jar target/benchmarks.jar -prof gcRun PDFBox benchmarks using JMH
trunkThe benchmark module uses JMH (Java Microbenchmark Harness). After building the project, you can run benchmarks using the
target/benchmarks.jarfile.- Run all benchmarks:
java -jar target/benchmarks.jar - List all available benchmarks:
java -jar target/benchmarks.jar -l - Select specific benchmarks by pattern:
java -jar target/benchmarks.jar <PatternOrName>(e.g.,LoadAndSave.loadMediumFile) - List benchmarks matching a pattern:
java -jar target/benchmarks.jar -l <Pattern> - Run a specific test with custom configuration: Use
-f(forks),-i(iterations), and-wi(warm-up iterations).
- Run all benchmarks:
Package the PDF Debugger App for Windows, Linux, and macOS
trunkThe Apache PDFBox Debugger App can be packaged for distribution on Windows, Linux, and macOS using packr.
Note: The Apache PDFBox project does not provide official support for creating these packages; this information is provided as a guide for interested developers.
Troubleshoot common PDFBox issues
trunkText Extraction Issues
- Meaningless character strings (e.g., "G38G43G36G51G5"): This occurs when characters use a meaningless internal encoding for embedded glyphs. In these cases, text extraction via standard methods may fail, and OCR (Optical Character Recognition) may be required.
- Incorrect character order: PDF text is stored in chunks that may not follow visual order. If text appears in the wrong order, ensure you have enabled sorting in your extraction process.
Classpath and Font Errors
java.io.IOException: Can't handle font width: This error often indicates that theorg/apache/pdfbox/resourcesdirectory is missing from your classpath. Ensure you have included theapache-pdfbox-x.x.x.jarin your classpath to resolve this.
Reference: JMH Benchmark CLI Flags and Profilers
trunkCommonly used flags and profiler commands for the PDFBox benchmark JAR:
CLI Flags
-l: List available benchmarks.-lprof: List available profilers.-f <int>: Number of forks.-i <int>: Number of iterations.-wi <int>: Number of warm-up iterations.-prof <profiler>: Specify a profiler (e.g.,gcorasync).