PDF Manipulation
Merging and Page Selection
Combine PDF files and pick which pages to include.
Merging files
Start with an empty document and chain .merge() calls to append pages from other files:
try (PdfDocument op = PdfDocument.create()) {
byte[] result = op
.merge(new File("cover.pdf"))
.merge(new File("report.pdf"))
.merge(new File("appendix.pdf"))
.saveBytes();
}Selecting pages from a merged file
Every .merge() call accepts an optional page spec that controls which pages get appended:
op.merge(new File("report.pdf"), "1-3") // pages 1 to 3
op.merge(new File("report.pdf"), "1,4,7") // specific pages
op.merge(new File("report.pdf"), "2-4,8") // a range plus individual pagesPage spec syntax
| Syntax | What it selects |
|---|---|
"1-5" | Pages 1 through 5, inclusive |
"1,3,5" | Specific pages |
"1-3,7" | A mix of ranges and individual pages |
"3,1" | Reordering: page 3 first, then page 1 |
Page numbers are 1-based.
Selecting pages from a loaded document
Use .pages() to filter or reorder the pages of the document you already loaded:
// Keep only pages 1, 3, 4, and 5
try (PdfDocument op = PdfDocument.load(new File("large.pdf"))) {
byte[] subset = op.pages("1,3-5").saveBytes();
}
// Reverse a 3-page document
op.pages("3,2,1")Password-protected sources
For encrypted source files, pass the password to .merge():
op.merge(new File("confidential.pdf"), "owner-password", "1-3")Or load it separately and merge the loaded operation:
try (PdfDocument source = PdfDocument.load(new File("confidential.pdf"), "password");
PdfDocument result = PdfDocument.create()) {
result.merge(source, "2-4").save(new File("output.pdf"));
}Using PageSelection directly
If you prefer not to use a string spec, you can use PageSelection instead:
import pdf.dev.ditsche.kawa.PageSelection;
op.merge(file, PageSelection.of(1, 3, 5))
op.merge(file, PageSelection.range(2, 8))
op.pages(PageSelection.all())