我想使用iText7
更改轮廓的缩放级别。请注意,与iText 5中的处理方式相比,这可能已经改变。
通过反复试验,我得出了以下代码:
List<PdfOutline> outlines = pdfDoc.getOutlines(true).getallChildren();
for (int i = 0; i < outlines.size(); i++) {
PdfOutline outline = outlines.get(i);
PdfDictionary content = outline.getcontent();
PdfDictionary pdfDictionary = (PdfDictionary) content.get(PdfName.A);
if (pdfDictionary != null) {
PdfArray arr = (PdfArray) pdfDictionary.get(PdfName.D);
if (arr.size() == 5) { // for XYZ zoom type
PdfName xyz = (PdfName) arr.get(1);
arr.set(3,new PdfNumber(2_000));
arr.set(4,new PdfNumber(2_000));
}
}
编辑
问题在于上面的代码似乎不起作用,因为保存了生成的pdf,但是缩放级别没有变化。
更新 我想出了一个不同的解决方案(受different question at SO的启发):
PdfNameTree destsTree = document.getcatalog().getNameTree(PdfName.Dests);
PdfOutline outline = document.getOutlines(false);
if (outline != null) {
walkOutlines(outline,destsTree.getNames(),document);
}
private static void walkOutlines(PdfOutline outline,Map<String,PdfObject> names,PdfDocument document) {
if (outline.getDestination() != null) {
int pageNumber = document.getPageNumber(
(PdfDictionary) outline.getDestination().getDestinationPage(names));
float height = document.getPage(pageNumber).getPageSize().getHeight();
outline.setOpen(false);
outline.addDestination(PdfExplicitRemoteGoToDestination.createXYZ(
pageNumber,0F,height,zoom_LEVEL));
}
for (PdfOutline child : outline.getallChildren()) {
walkOutlines(child,names,document);
}
}