itext change zoom level of all hyper link to inher

2019-03-06 11:22发布

问题:

I am using following code to change zoom level of all hyperlinks to inherit zoom, but unable to change.

may be i have made some mistake in PdfName.DEST and condition because there is no array of DEST in pdf for first page( check out screen shot).

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

I have created link in first page to second page, check out structure in image.

I have also try to use following code... i have debug and check value of d is null every time

for (int count = 0; count < reader.getNumberOfPages(); count++) {
            PdfDictionary page = reader.getPageN(count+1);
            PdfArray annots = page.getAsArray(PdfName.ANNOTS);
            if (annots != null) {
                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);
                        // d is null every time
                    }
                }
            }
        }

回答1:

You add a screen shot that allows you to look inside the PDF. In this screen shot, you clearly see that the destination is [5 0 R, /FitH, 795]. However, you only change the zoom factor of destinations that consist of 5 values and that are of type /XYZ.

 if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1)))
      d.set(4, new PdfNumber(0));

[5 0 R, /FitH, 795] has 3 elements (in other words d.size() == 5 is false) and the first entry in the array is PdfName.FITH (in other words PdfName.XYZ.equals(d.getAsName(1) is false).

Your question should be down-voted because you show one thing, then do something else, and then ask us why you don't get the expected result. It's as if somebody serves you soup and you see a spoon and a fork on the table. In spite of the fact that you know that soup should be eaten with a spoon, you take the fork and then complain that you can't eat soup with a fork. That's... odd.

Update In the comments, you complain that d is null, but looking at your screen shot, you already knew that d is null.

You loop over the pages and you get the page dictionary for page 1 (Object 4). You check if there's an /Annots entry in this page dictionary. There is (object 40). You loop over the annots and you encounter an annotation dictionary (object 30 or 39, the resolution of the screen shot is too bad for me to see). You check if the /Subtype is Link. It is. You check if the annotation has an /A entry. It has. Then you get the /Dest entry. There is no such entry. Hence you get null. The destination is to be found here:

PdfDictionary action = annotation.get(PdfName.A);
if (action != null) {
    PdfArray d = action.getAsArray(PdfName.D);
    // now examine and change D
}

Note that you are asking the SO community to write your application in your place. You are paid for this solution, but you expect that other people do your work in your place. Moreover: you probably don't have a commercial license for iText...



标签: java itext