I am using the itext7 library to manipulate some existing PDFs. For some reason, I am not able to get the page number from the outline. I guess I somehow should get it from
Regarding the levels of the outline hierarchy, in order to traverse the whole hierarchy you will have to check for each PdfOutline
's children and traverse them recursively.
The names parameter that was confusing to you is the parameter that is responsible for resolving named destinations which is necessary to get the page numbers correctly in general case because your PDF document may contains explicit as well as named destinations. To get the names map you can use pdfDocument.getCatalog().getNameTree(PdfName.Dests).getNames()
;
To find the page number by a page object, you should use pdfDocument.getPageNumber(PdfDictionary)
.
Overall, the method walking through the outlines may look as following:
void walkOutlines(PdfOutline outline, Map names, PdfDocument pdfDocument) {
if (outline.getDestination() != null) {
System.out.println(outline.getTitle() + ": page " +
pdfDocument.getPageNumber((PdfDictionary) outline.getDestination().getDestinationPage(names)));
}
for (PdfOutline child : outline.getAllChildren()) {
walkOutlines(child, names, pdfDocument);
}
}
And the main entry point to call the method to traverse the outline root:
PdfNameTree destsTree = pdfDocument.getCatalog().getNameTree(PdfName.Dests);
PdfOutline root = pdfDocument.getOutlines(false);
walkOutlines(root, destsTree.getNames(), pdfDocument);
Please note that the code sample is for Java, but it should be similar in C# except some case changes and IDictionary
instead if Map
.