How to change the zoom factor in link annotations?
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)); ArrayListPdfObject> arrAnnot = new ArrayListPdfObject>(); 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 replace the destination, you should replace the zoom factor. Also note that there is no PdfCopy
class in iText 7, though its usage was redundant. You only need a PdfDocument
instance.
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
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 = pageDict.getAsArray(PdfName.Annots);
for (int i = 0; i < annots.size(); i++) {
PdfDictionary annotation = annots.getAsDictionary(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.
Click this link if you want to see how to answer this question in iText 5.