使用 PDFBox 更改现有 pdf 的页面缩放

Changing page zoom of an existing pdf with PDFBox

我有一个 pdf,我正在使用 PDFBox 进行迭代,如下所示:

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

我想设置页面的默认放大倍率,以便在通过 pdf reader 打开时,默认以 75% 缩放比例打开。这可能吗?我看过一些使用 PDPageXYZDestination 设置缩放的帖子,但我不确定这是否适用于我的情况。

谢谢。

这样做,它仅适用于第一个看到的页面,即打开时:

PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDPage page = doc.getPage(0); // zero-based; you can also put another number to jump to a specific existing page
PDPageXYZDestination dest = new PDPageXYZDestination();
dest.setPage(page);
dest.setZoom(0.75f);
dest.setLeft((int) page.getCropBox().getLowerLeftX());
dest.setTop((int) page.getCropBox().getUpperRightY());
PDActionGoTo action = new PDActionGoTo();
action.setDestination(dest);
catalog.setActions(null);
catalog.setOpenAction(action);
doc.save(...);