PDF Document Manipulation in Java
Aspose.PDF FOSS for Java provides a rich set of APIs for manipulating existing PDF
documents — splitting, concatenating, extracting pages, inserting page ranges, and
resizing content within pages. These capabilities are concentrated in PdfFileEditor
and its companion classes ContentsResizeParameters and ContentsResizeValue.
PdfFileEditor: Core Page Operations
The PdfFileEditor class is the central facility for high-level PDF file operations.
It provides methods for concatenating multiple PDF files, extracting page subsets,
splitting a document into individual pages, and inserting pages from one document
into another at a specific position.
PdfFileEditor editor = new PdfFileEditor();
// Concatenate two documents into one
editor.concatenate("first.pdf", "second.pdf", "combined.pdf");
// Extract pages 2 through 5 to a new file
editor.extract("source.pdf", 2, 5, "extracted.pdf");
Key methods include:
concatenate(inputFiles, outputFile)— merges multiple documents in orderextract(inputFile, startPage, endPage, outputFile)— exports a contiguous rangeextract(inputFile, pageNumbers, outputFile)— exports a non-contiguous setsplitToPages(inputFile)— splits into oneByteArrayOutputStreamper pageinsert(inputFile, insertPosition, portFile, startPage, endPage, outputFile)— inserts a rangedelete(inputFile, pageNumbers, outputFile)— removes specific pagesappend(inputFile, portFile, startPage, endPage, outputFile)— appends a page range
For large files, setUseDiskBuffer(true) switches internal storage from heap to disk.
Content Resizing with ContentsResizeParameters
PdfFileEditor.resizeContents() adjusts content within existing page boundaries.
Each margin — left, right, top, bottom — and the content width and height are
specified as ContentsResizeValue instances, constructed via the factory methods
ContentsResizeValue.percents(value) or ContentsResizeValue.units(value).
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");
}
The six-argument constructor for ContentsResizeParameters accepts values in the
order: leftMargin, contentsWidth, rightMargin, topMargin, contentsHeight, bottomMargin.
All six must sum to 100% when using percentage-based values, or match the page
dimensions when using absolute point values.
Resizing Specific Pages
When only a subset of pages requires resizing, the overload that accepts an integer array of page numbers targets those pages while leaving the rest unchanged:
PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
ContentsResizeValue.units(28), // left margin in points
ContentsResizeValue.units(540), // content width
ContentsResizeValue.units(28), // right margin
ContentsResizeValue.units(28), // top margin
ContentsResizeValue.units(786), // content height
ContentsResizeValue.units(28) // bottom margin
);
// Only resize pages 1 and 3
editor.resizeContents(doc, new int[]{1, 3}, params);
ContentsResizeValue.getValue() and isPercent() allow inspecting the stored
value and determining whether it represents a percentage or an absolute point count.
Booklet and N-Up Layout
PdfFileEditor includes makeBooklet() for imposing pages in two-sided booklet order
and makeNUp() for tiling multiple pages per sheet. Both methods accept file paths or
streams:
PdfFileEditor editor = new PdfFileEditor();
// Print as a folded booklet
editor.makeBooklet("input.pdf", "booklet.pdf");
// Tile 2 columns × 3 rows per sheet
editor.makeNUp("input.pdf", "nup.pdf", 2, 3);
PdfPageEditor: Per-Page Zoom Control
PdfPageEditor targets individual page properties such as zoom level. setZoom()
accepts a float multiplier that controls the initial magnification when the document
is opened in a viewer:
PdfPageEditor pageEditor = new PdfPageEditor();
pageEditor.bindPdf("input.pdf");
pageEditor.setZoom(1.5f);
pageEditor.save("zoomed.pdf");
Values of 0.0 or negative are rejected — getZoom() returns 1.0 in those cases.
This makes PdfPageEditor suitable for preparing documents that must open at a specific
magnification for accessibility or kiosk display use cases.
Getting Started
Add the Maven dependency and import the facades package:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>0.1.0-alpha</version>
</dependency>
import org.aspose.pdf.*;
import org.aspose.pdf.facades.*;
Aspose.PDF FOSS for Java is MIT-licensed and available at
https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java.