Starting with v2017R1, Qoppa Java PDF library jPDFProcess offers an API to easily resize or change page sizes in a PDF document.
The resize options are as follow:
- Media Box – new media box for the PDF Page.
- Center – option to center the page contents in the new media box (off by default)
- Auto Scale – option to scale the page contents to fit inside the new media box (off by default)
- XOffset, YOffset – x and y offset of the page contents. If center or auto scale is on, then this value is not used.
- Scale – scale to be applied to the page content. Not used if auto scale is set to true.
- Rotation – amount to rotate the page contents in radians. A positive value rotates the page counter clockwise.
- Fit To Media – option to set the page’s crop box equal to the page’s media box, otherwise the crop box will have the same margins as before resizing. The default is off.
Here is a Java sample program that will open a PDF and resize its pages to A3 format. Media Box and Crop Box will be resized to A3. The page content is re-scaled and centered to fit the new format.
// Load document PDFDocument pdfDoc = new PDFDocument("C:/qoppa/test/in.pdf", null); // Get the first page of the PDF PDFPage page = pdfDoc.getPage(0); // Create ResizePageOptions object ResizePageOptions options = new ResizePageOptions(); // Get the page size A3 (297mm x 420mm) in points double width = 297 / 25.4 * 72.0; double height = 420 / 25.4 * 72.0; // Set the new Media Box options.setMediaBox(new Rectangle2D.Double(0, 0, width, height)); // Scale the page contents to fit the new page size options.setAutoScale(true); // Center the page contents on the page options.setCenter(true); // Set the crop box to the same size as the media box options.setFitToMedia(true); // Resize the page page.resizePage(options); // Save the output document pdfDoc.saveDocument("C:/qoppa/test/out.pdf"); |
Download the full Resize PDF Pages Java Sample.
You can try the functionality in our end-user application PDF Studio, which makes it simple to visualize the different resize properties available. You can download PDF Studio free demo. Look under Pages -> Advanced -> Resize Pages.