How to Work with PDF File Operations in Java
Concatenating PDF Files
Use PdfFileEditor.concatenate() to merge multiple PDF files into one output file.
Pass an array of input paths and the target output path:
PdfFileEditor editor = new PdfFileEditor();
String[] inputs = {"part1.pdf", "part2.pdf", "part3.pdf"};
editor.concatenate(inputs, "merged.pdf");Extracting a Page Range
Extract a contiguous range of pages with start and end page numbers (1-indexed). The extracted pages are written to a new file:
PdfFileEditor editor = new PdfFileEditor();
// Extract pages 2 through 5 (1-indexed)
editor.extract("source.pdf", 2, 5, "extracted.pdf");Splitting into Individual Pages
Split a document so each page becomes a separate file. The output directory must already exist before calling this method:
PdfFileEditor editor = new PdfFileEditor();
editor.splitToPages("document.pdf", "output_dir/");Resizing Page Content
Scale content within existing page boundaries using ContentsResizeParameters.
Specify the left margin, content width, right margin, top margin, content height,
and bottom margin as percentages — they must sum to 100 in each dimension:
try (Document doc = new Document("input.pdf")) {
PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
ContentsResizeValue.percents(10), // left margin
ContentsResizeValue.percents(80), // content width
ContentsResizeValue.percents(10), // right margin
ContentsResizeValue.percents(10), // top margin
ContentsResizeValue.percents(80), // content height
ContentsResizeValue.percents(10) // bottom margin
);
editor.resizeContents(doc, params);
doc.save("resized.pdf");
}Making a Booklet
Impose pages in booklet print order so the document prints correctly when folded. This reorders and pairs pages appropriately for duplex printing:
PdfFileEditor editor = new PdfFileEditor();
editor.makeBooklet("input.pdf", "booklet.pdf");