I'd like to change zoom level in outlines using iText7
. Note that this has probably changed compared to the way it was done in iText 5.
By trial and error I've come up with the code:
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));
}
}
EDIT
The problem is that the above code doesn't seem to work as the resulting pdf is saved but there are no changes in zoom level.
UPDATE I've come up with a different solution (inspired by 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);
}
}