0

This Code is perfect. But the only problem is that I want to copy links and I want to change the property of links to inherit zoom.

public class links {
public static void main(String[] args) throws DocumentException, IOException,FileNotFoundException {

    String src = "E:/bookmark.pdf";
    String destination = "E:/links.pdf";

    PdfReader reader=new PdfReader(src);
    reader.consolidateNamedDestinations();

    Document doc=new Document();

    PdfCopy pdfCopy = new PdfCopy(doc,new FileOutputStream(destination));

    doc.open();

     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)
        {
                //reader.addPdfObject(pageDic.get(PdfName.ANNOTS));
            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();
    pdfCopy.close(); 
    doc.close();
    System.out.println("The Pdf is Created..");
}

}
connectedsoftware
  • 6,987
  • 3
  • 28
  • 43
Paresh3489227
  • 845
  • 11
  • 22
  • Why are you using `PdfCopy` instead of `PdfStamper` if you want to change annotations. Why are you replacing all links with a specific goto that always jumps to page 1 instead of replacing the zoom factor of the existing links? – Bruno Lowagie Apr 03 '14 at 12:43
  • i want to change only zoom property of links i don't have any method. instead of local. remaining all contents i want as it is. – Paresh3489227 Apr 04 '14 at 04:17
  • zoom property is only set by pdfDestination. Do u know any another way to set zoom property to set links – Paresh3489227 Apr 04 '14 at 04:18
  • You crawl until you find an annotation of type `Link`, then you do something very strange: you replace the complete `/Dest` instead of examining the existing destination: why don't you do `annots.get(PdfName.DEST);` and examine what you get? – Bruno Lowagie Apr 04 '14 at 07:11
  • Can you paste the code. i tried with pdfCopy object . addNamedDestination there i put pdfDestination property to zoom then also i didnt got my ans. – Paresh3489227 Apr 04 '14 at 08:34

1 Answers1

1

Please take a look at the ChangeZoomXYZDestination example. You'll soon discover that your allegation "This Code is perfect" is wrong. As I already indicated in my comments, you shouldn't use PdfCopy, you should use PdfStamper. 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:

enter image description here

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • As you said i changed all pdfCopy to pdfStamper class. and applied this code. but didn't got answer yet. this code says that if the pdfname is xyz then set the 4 to zero. But i need all properties to set xyz, if any property is applied like fit width,fit page. i want to change the property of all links to xyz and last 0 to set the zoom level. – Paresh3489227 Apr 07 '14 at 04:59
  • and what is d.size()==5. here what 5 says? didn't got – Paresh3489227 Apr 07 '14 at 05:07
  • Again it is hard to understand why you ask these questions. If the second element in the array isn't /XYZ, why don't you change it to /XYZ? As for the number of elements in the array: that depends on the value of the second parameter, doesn't it? Please read th documentation (E.g. The ABC of PDF) to find out how many parameters correspond with each value of the second parameter. – Bruno Lowagie Apr 07 '14 at 05:38
  • Thanks Mr.Bruno for help. I got my answer. – Paresh3489227 Apr 08 '14 at 04:13