Search code examples
pdfscalepdfboxpdflib

Java- stretch pdf pages content


How can I make the page larger in the way that the text/images will stretch according to the new size? I only found ways to scale down but not scale up... any idea?

Thanks in advance!!!


Solution

  • There are two conceptional options:

    1. For each page enlarge MediaBox and CropBox, prepend current transformation matrix scaling operation to the page content stream or array of streams, and update annotation positions and sizes.

    2. For each page set the property UserUnit to a value > 1.

    The second option can e.g. be implemented like this using PDFBox and a stretch factor of 1.7:

    PDDocument document = PDDocument.load(SOURCE);
    
    for (PDPage page : document.getPages()) {
        page.getCOSObject().setFloat("UserUnit", 1.7f);
    }
    
    document.save(TARGET);
    

    (The second option obviously is much easier to implement than the first one. Feature incomplete PDF viewers might ignore this value, though. If you need to support such incomplete viewers, you probably have to go for the first option.)