Resizing Pages using the PageResizer Class
Overview
PDFs generated from different sources often have inconsistent page sizes, orientations, and layout structures. When working with merged document sets, print workflows, or standardized output requirements, it becomes necessary to resize all pages to a single uniform format.
iText Core 9.4.0 introduces PageResizer (Java/.NET), a new API designed to resize PDF pages to a target PageSize while preserving visual correctness. The class updates page dimensions, scales and aligns the content, and adjusts annotations, forms, and resources so the resized pages remain functional and well structured.
Concepts
Page Resizing:
This class allows any page to be resized to a target PageSize ensuring that the final Target PDF is similar to the one requested in this method.
Resize Strategies:
ResizeType.DEFAULT or ResizeType.MAINTAIN_ASPECT_RATIO, which controls how content within those pages are scaled when the new size differs from the input PDFs.
Anchor Points:
Controls how content is aligned on the resized page. (LEFT, CENTER, RIGHT horizontally and TOP, CENTER, BOTTOM vertically).
Page Adjustments:
When resizing the page boxes, content streams, annotations and other resources are adjusted to match the new and final page size.
Example Explanation
In the below example we merge several PDFs, each created with different page dimensions or containing annotations and we resize all pages in the final document to A4.
PageResizer applies the new page size to each copied page and uses the configured resize strategy and anchor points to determine how the existing content fits within the new dimensions.
The final target PDF gets assigned a new
PageSizeofA4.Content within all the input PDFs get scaled proportionally when using MAINTAIN_ASPECT_RATIO.
Horizontal and Vertical Anchor points depict where the scaled content is placed.
Associated elements such as annotations and page resources are updated so they align correctly with the resized content.
Code Samples
Java
PdfDocument output = new PdfDocument(new PdfWriter(dest));
PageResizer resizer = new PageResizer(
PageSize.A4,
PageResizer.ResizeType.MAINTAIN_ASPECT_RATIO
);
resizer.setHorizontalAnchorPoint(PageResizer.HorizontalAnchorPoint.CENTER);
resizer.setVerticalAnchorPoint(PageResizer.VerticalAnchorPoint.CENTER);
for (String src : inputs) {
try (PdfDocument input = new PdfDocument(new PdfReader(src))) {
int start = output.getNumberOfPages() + 1;
input.copyPagesTo(1, input.getNumberOfPages(), output);
int end = output.getNumberOfPages();
for (int p = start; p <= end; p++) {
resizer.resize(output.getPage(p));
}
}
}
output.close();
.NET
using (PdfDocument output = new PdfDocument(new PdfWriter(dest)))
{
PageResizer resizer = new PageResizer(
PageSize.A4,
PageResizer.ResizeType.MAINTAIN_ASPECT_RATIO
);
resizer.SetHorizontalAnchorPoint(PageResizer.HorizontalAnchorPoint.CENTER);
resizer.SetVerticalAnchorPoint(PageResizer.VerticalAnchorPoint.CENTER);
foreach (string src in inputs)
{
using (PdfDocument input = new PdfDocument(new PdfReader(src)))
{
int start = output.GetNumberOfPages() + 1;
input.CopyPagesTo(1, input.GetNumberOfPages(), output);
int end = output.GetNumberOfPages();
for (int p = start; p <= end; p++)
{
resizer.Resize(output.GetPage(p));
}
}
}
}
Result
The final output PDF is a single merged PDF after being resized with the
PageResizerclassContent within those PDF pages is scaled proportionally and centered.
Annotations as well as other page resources remain aligned with their resized content.
Summary
PageResizer in iText Core 9.4.0 introduces a straightforward and reliable way to resize PDF pages to a target PageSize. By adjusting page boxes, content, forms, annotations, and associated resources, it makes it easy to normalize documents from different sources into a consistent layout.
The merging and resizing example above demonstrates a practical use case where multiple PDFs are brought together and standardized to A4 in a single step, providing a consistent output PDF where the content and the resources are scaled proportionally and maintained.
Sample Resources
Output PDF - resized_output.pdf
Code Samples - PageResizer_Java.zip | PageResizer_DotNet.zip