PDF Manipulation
Loading and Inspecting PDFs
Load, inspect, and post-process existing PDF files with PdfDocument.
PdfDocument is a fluent pipeline for working with existing PDF files. You can merge documents, select pages, encrypt, and decrypt without going through the document composition API.
Loading a PDF
// From a file
PdfDocument.load(new File("input.pdf"))
// From a byte array
PdfDocument.load(pdfBytes)
// Password-protected
PdfDocument.load(new File("protected.pdf"), "password")
PdfDocument.load(pdfBytes, "password")
// Start with an empty document
PdfDocument.create()Saving the result
op.save(new File("output.pdf"));
op.save(outputStream);
byte[] bytes = op.saveBytes();You can also pass save options:
op.save(new File("output.pdf"), PdfSaveOptions.standard());
op.saveBytes(PdfSaveOptions.builder().compress(true).build());Resource management
PdfDocument implements Closeable, so use try-with-resources to make sure the underlying document gets closed:
try (PdfDocument op = PdfDocument.load(new File("input.pdf"))) {
byte[] result = op.pages("2-5").saveBytes();
}Inspecting a document
try (PdfDocument op = PdfDocument.load(file)) {
int pages = op.pageCount();
boolean enc = op.isEncrypted();
}