Q: Is there a way to resize a PDF in order to scale non-letter-size PDFs to 8.5 x 11 PDFs using Qoppa’s PDF library jPDFProcess?

A: Yes, there is.

Version v2017R1 and later

jPDFProcess now has a direct API to resize pages in a PDF.

Version v2016R1 and earlier

In previous versions, it was possible to resize pages in a PDF document by creating a new document with new pages of the targeted paper size and then copying and re-scaling the content of your original pages. This process will preserve text, images and all vector commands present in the original PDF.

The method used is called PDFPage.appendPageContent in the PDFPage class:

PDFPage.appendPageContent(page, x, y, scalex, scaley, null);

The arguments are the x, y positions on the target page and the x and y scales.

Here is sample code to resize PDF Pages using Qoppa’s library jPDFProcess that will open a PDF and copy the pages to a new PDF with re-scaled pages of 8.5 x 11.

private static final int PDF_TOTAL_WIDTH = 612; // 8.5 * 72
private static final int PDF_TOTAL_HEIGHT = 792; // 11 * 72
    public static void main (String [] args)
    {
        try
        {
            // load original PDF
            PDFDocument originalDoc = new PDFDocument ("input.pdf", null);
 
            // create a new PDF Document 
            PDFDocument scaledDoc = new PDFDocument();
 
            // Loop through all pages
            for (int i = 0; i < originalDoc.getPageCount(); i++) 
            {
            	// create a new page of the expected size in the new PDF 
                PDFPage newPage = scaledDoc.appendNewPage(PDF_TOTAL_WIDTH, PDF_TOTAL_HEIGHT);
                // get original page in the original PDF 
                PDFPage originalPage = originalDoc.getPage(i);
                // compute the new scale
                double scale = calculatePDFPageScale(originalPage);
                // append the content of the original page to the new PDF Page
                // using the computed scale so it is resized as expected
                newPage.appendPageContent(originalPage, 0, 0, scale, scale, null);
            }
 
            // save the scaled document
            scaledDoc.saveDocument ("output.pdf");
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
    }

where the method calculatePDFPageScale to calculate the page scale is as follow:

    private static double calculatePDFPageScale(PDFPage page) 
    {
        double originalWidth = page.getDisplayWidth();
        double originalHeight = page.getDisplayHeight();
        double targetWidth = PDF_TOTAL_WIDTH;
        double targetHeight = PDF_TOTAL_HEIGHT;
 
        // No need to scale if original is smaller than target
        if (targetWidth - originalWidth >= 0 && targetHeight - originalHeight >= 0) {
            return 1;
        }
 
        // Skip scaling and just crop if either dimension is 0.
        if (originalWidth == 0 || originalHeight == 0) {
            return 1;
        }
 
        // Only scale as much as necessary. Maintain aspect ratio.
        double widthScale = targetWidth / originalWidth;
        double heightScale = targetHeight / originalHeight;
        return (widthScale <= heightScale) ? widthScale : heightScale;
    }