drunkenfist drunkenfist - 2 months ago 25
Java Question

Changing page zoom of an existing pdf with PDFBox

I have a pdf which I'm iterating through using

PDFBox
as below:

PDDocument doc = PDDocument.load(new ByteArrayInputStream(bytearray));
PDDocumentCatalog catalog = doc.getDocumentCatalog();
for(PDPage page : catalog.getPages()){
...
}


I want to set the default magnification for the pages so that when it is opened through a pdf reader, it opens at
75%
zoom by default. Is this possible? I've seen few posts where the zoom is set using
PDPageXYZDestination
, but I'm not sure whether that is applicable in my case.

Thanks.

Answer

Do this, it applies to the first page only, i.e. when opening:

PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDPage page1 = doc.getPage(0);
PDPageXYZDestination dest = new PDPageXYZDestination();
dest.setPage(page1);
dest.setZoom(0.75f);
dest.setLeft((int) page1.getCropBox().getLowerLeftX());
dest.setTop((int) page1.getCropBox().getUpperRightY());
PDActionGoTo action = new PDActionGoTo();
action.setDestination(dest);
catalog.setActions(null);
catalog.setOpenAction(action);
doc.save(...);
Comments