How to get the landing page of a link in a PDF file?

Using iText , we can easily change the zoom level for links. There is even a code snippet that does this for a GoTo destination type. To participate in the conference you will find below.

 PdfReader reader = new PdfReader(src); PdfDictionary page = reader.getPageN(11); PdfArray annots = page.getAsArray(PdfName.ANNOTS); for (int i = 0; i < annots.size(); i++) { PdfDictionary annotation = annots.getAsDict(i); if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) { PdfArray d = annotation.getAsArray(PdfName.DEST); if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1))) d.set(4, new PdfNumber(0)); } } 

The code deals only with one of the destination types found in the PDF files. I am interested in zooming in on other types of destinations (they are listed in 32000-1 if anyone was wondering). In particular, I would like to change each destination to GoTo and indicate my own coordinates. I want the left coordinate to be the same as the page height of the page to navigate. To do this, I obviously need a page number. How to get it?

What have i done so far? The PdfArray d = annotation.getAsArray(PdfName.DEST) command PdfArray d = annotation.getAsArray(PdfName.DEST) gives a su array, where its first (0-based) element is a page link, not a page number, as Bruno Loudji explains in his iText in Action, 2nd edition, p. 202). The array looks like this: iText in Action, 2nd edition, p. 202). The array looks like this: iText in Action, 2nd edition, p. 202). The array looks like this: [1931 0 R, / XYZ, 0, 677, 0] `. I can not find the correct command to get the page number myself, so this is a message.

+5
source share
2 answers

I want the left coordinate to be the same as the page height of the page to navigate. To do this, I obviously need a page number. How to get it?

It is assumed that you need a page number. Yes, PdfReader methods mainly work based on page numbers, but this is not enough in these methods. If everything is fine with low-level data access, you don’t need a page number.

Below is the code with additional code for harvesting (which determines the coordinates of the left, bottom, right and top pages), once directly from the link to the object that you have, and once by page number.

 PdfReader reader = new PdfReader(src); PdfDictionary page = reader.getPageN(11); PdfArray annots = page.getAsArray(PdfName.ANNOTS); for (int i = 0; i < annots.size(); i++) { PdfDictionary annotation = annots.getAsDict(i); if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) { PdfArray d = annotation.getAsArray(PdfName.DEST); if (d == null) { // in case the link has not a Dest but instead a GoTo action PdfDictionary action = annotation.getAsDict(PdfName.A); if (action != null) d = action.getAsArray(PdfName.D); } if (d != null && d.size() > 0) { System.out.println("Next destination -"); PdfIndirectReference pageReference = d.getAsIndirectObject(0); // Work with target dictionary directly PdfDictionary pageDict = d.getAsDict(0); PdfArray boxArray = pageDict.getAsArray(PdfName.CROPBOX); if (boxArray == null) { boxArray = pageDict.getAsArray(PdfName.MEDIABOX); } Rectangle box = PdfReader.getNormalizedRectangle(boxArray); System.out.printf("* Target page object %s has cropbox %s\n", pageReference, box); // Work via page number for (int pageNr = 1; pageNr <= reader.getNumberOfPages(); pageNr++) { PRIndirectReference pp = reader.getPageOrigRef(pageNr); if (pp.getGeneration() == pageReference.getGeneration() && pp.getNumber() == pageReference.getNumber()) { System.out.printf("* Target page %s has cropbox %s\n", pageNr, reader.getCropBox(pageNr)); break; } } } } } 

( ProcessLink test testDetermineTargetPage )


By the way, a destination can also be a named destination. Thus, if the Dest value for some PDFs is not an array, but a string, you just need to look in the Dests name tree .

+2
source

According to this: https://developers.itextpdf.com/fr/node/1750

The first example is an array with two elements 8 0 R and / Fit. the second example is an array with four elements 6 0 R, / XYZ, 0, 806 and 0. You need the first element. It does not give you the page number (because there are no such things as page numbers), but it gives you a link to the / Page object. Based on this link, you can display the page number by going through the page tree and comparing the object with the specific page number with the object number at the destination.

And than you can go recursively to extract the page number, for example: Extract the page number from a PDF file

I hope you find this helpful. Good luck

+3
source

Source: https://habr.com/ru/post/1275959/


All Articles