Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a destination page of a link in PDF file?

Tags:

java

itext

Using iText we can easily change zoom level for links. There is even a piece of code that does this for GoTo destination type. For convienence, please find it 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 destination types found in PDF files. I'm interested in changing zoom in other types of destinations (they are listed in 32000-1 if anyone wondered). Specifically, I'd like to change each destination to GoTo type and specify my own coordinates. I want left coordinate to be the same as the page height of the page to jump. To do this, I obviously I need the page number. How do I get it?

What have I done so far? The instruction PdfArray d = annotation.getAsArray(PdfName.DEST) gives su an array where its first (0 based) element is page reference and not page number as Bruno Lowagie explains in his iText in Action, 2nd edition, p. 202). The array looks like this:[1931 0 R, /XYZ, 0, 677, 0]`. I cannot find correct command to get page number on my own hence this post.

like image 354
menteith Avatar asked Mar 19 '18 18:03

menteith


1 Answers

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 doesn't give you the page number (because there is no such thing as page numbers), but it gives you a reference to the /Page object. Based on that reference, you can deduce the page number by looping over the page tree and comparing the object number of a specific page with the object number in the destination.

And than you can go recursively to extract page number, like this: Extract page number from PDF file

Hope, you find it's helpful. Good luck!

like image 127
Anton Kot Avatar answered Nov 13 '22 05:11

Anton Kot