Search code examples
coordinatespdfbox

How to translate x, y origin of a PDPage?


I'm new to Apache's PDFBox. I'm using version 2.0.0, taken from the SVN repository.

Actually I try to increase the cropbox of a PDPage object. OK, no challenge. But the page content remains in the lower left corner of the cropbox. I want it centered in the new cropbox space.

I understood that all content is positioned absolutely in PDF. So my question: Is there a way using PDFBox to translate the origin (x, y) of my PDPage or the content elements?

Regards
Hans


Solution

  • The first approach would be to simply change the crop box like this:

        PDDocument document = PDDocument.load(new File("data/test.pdf"));
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        @SuppressWarnings("unchecked")
        List<PDPage> pages = catalog.getAllPages();
        float expand = 72;
    
        for (PDPage page : pages)
        {
            PDRectangle cropBox = page.findCropBox();
            PDRectangle newCropBox = new PDRectangle();
            newCropBox.setLowerLeftX(cropBox.getLowerLeftX() - expand);
            newCropBox.setLowerLeftY(cropBox.getLowerLeftY() - expand);
            newCropBox.setUpperRightX(cropBox.getUpperRightX() + expand);
            newCropBox.setUpperRightY(cropBox.getUpperRightY() + expand);
            page.setCropBox(newCropBox);
        }
    
        document.save("data/out/test-expand-crop-simple.pdf");
    

    This only works sometimes, though, because according to the specification ISO 32000-1, section 14.11.2 Page Boundaries

    The crop, bleed, trim, and art boxes shall not ordinarily extend beyond the boundaries of the media box. If they do, they are effectively reduced to their intersection with the media box.

    (also see this answer)

    Thus, we have to make sure that the crop box even after enlarging still fits into the media box, e.g. like this:

        PDDocument document = PDDocument.load(new File("data/test.pdf"));
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        @SuppressWarnings("unchecked")
        List<PDPage> pages = catalog.getAllPages();
        float expand = 72;
    
        for (PDPage page : pages)
        {
            PDRectangle cropBox = page.findCropBox();
            PDRectangle newCropBox = new PDRectangle();
            newCropBox.setLowerLeftX(cropBox.getLowerLeftX() - expand);
            newCropBox.setLowerLeftY(cropBox.getLowerLeftY() - expand);
            newCropBox.setUpperRightX(cropBox.getUpperRightX() + expand);
            newCropBox.setUpperRightY(cropBox.getUpperRightY() + expand);
            page.setCropBox(newCropBox);
    
            PDRectangle mediaBox = page.findMediaBox();
            PDRectangle newMediaBox = new PDRectangle();
            newMediaBox.setLowerLeftX(mediaBox.getLowerLeftX() - expand);
            newMediaBox.setLowerLeftY(mediaBox.getLowerLeftY() - expand);
            newMediaBox.setUpperRightX(mediaBox.getUpperRightX() + expand);
            newMediaBox.setUpperRightY(mediaBox.getUpperRightY() + expand);
            page.setMediaBox(newMediaBox);
        }
    
        document.save("data/out/test-expand-crop-and-media.pdf");