How to change the zoom factor in link annotations?

Tags: annotationslink annotationzoom factordestinationiText 5

I want to change only zoom property of links. This is what I currently have:

int n = reader.getNumberOfPages();
PdfDestination d = new PdfDestination(PdfDestination.XYZ,-1,-1,0.0F) ;
PdfAction act = PdfAction.gotoLocalPage(1, d, pdfCopy);
for (int i=1; i <= n ;i++) {
    PdfDictionary pageDic = reader.getPageN(i);
    PdfArray arrayann = pageDic.getAsArray(PdfName.ANNOTS);  
    if (arrayann != null) {
        PdfArray annot=(PdfArray)PdfReader.getPdfObject(pageDic.get(PdfName.ANNOTS)); 
        ArrayList<PdfObject> arrAnnot = new ArrayList<PdfObject>();
        arrAnnot = annot.getArrayList();
        for (int j = 0; j < arrAnnot.size(); j++) {
            PdfDictionary annots = (PdfDictionary)PdfReader.getPdfObject(arrAnnot.get(j));
            if (PdfName.LINK.equals(annots.get(PdfName.SUBTYPE))) {
                annots.remove(PdfName.DEST);
                annots.put(PdfName.DEST,act);
            }
        }
    }
    pdfCopy.addPage(pdfCopy.getImportedPage(reader, i));
    pdfCopy.freeReader(reader);
}
reader.close();

Now I'm stuck, because this doesn't change the zoom factor.

Posted on StackOverflow on Apr 3, 2014 by paresh3489227

Please take a look at the ChangeZoomXYZDestination example. You shouldn't use PdfCopy; use PdfStamper instead. Also, you shouldn't replace the destination, you should replace the zoom factor.

Take for instance the file xyz_destination.pdf on page 11, there are 10 links to the 10 previous pages, each with an /XYZ destination pointing at a specific page with a specific zoom factor. You can see this in the following screen shot:

Internal structure of link annotations and their destinations
Internal structure of link annotations and their destinations

In the first annotation, the zoom factor is 1, in the second it's 2, and so on.

If you want to change the zoom factor of these links to 0, then you need to loop over the annotations (you already do this), but instead of replacing the /DEST incorrectly with an action, you need to change the value of the zoom-factor in the /DEST array:

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));
    }
}

Now you will have a file such as xyz_zoom.pdf where the zoom factor of all links of type /XYZ will be zero.