Changing the zoom factor in a link destination

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

ChangeZoomXYZDestination.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written by Bruno Lowagie in answer to the following question:
  10.  * http://stackoverflow.com/questions/22828782/all-links-of-existing-pdf-change-the-action-property-to-inherit-zoom-itext-lib
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.pdf.*;
  15. import com.itextpdf.samples.GenericTest;
  16. import com.itextpdf.test.annotations.type.SampleTest;
  17.  
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.File;
  21.  
  22. @Category(SampleTest.class)
  23. public class ChangeZoomXYZDestination extends GenericTest {
  24.     public static final String DEST = "./target/test/resources/sandbox/annotations/change_zoom_xyz_destination.pdf";
  25.     public static final String SRC = "./src/test/resources/pdfs/xyz_destination.pdf";
  26.  
  27.     public static void main(String[] args) throws Exception {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new ChangeZoomXYZDestination().manipulatePdf(DEST);
  31.     }
  32.  
  33.     @Override
  34.     protected void manipulatePdf(String dest) throws Exception {
  35.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  36.  
  37.         PdfDictionary pageDict = pdfDoc.getPage(11).getPdfObject();
  38.         PdfArray annots = pageDict.getAsArray(PdfName.Annots);
  39.         for (int i = 0; i < annots.size(); i++) {
  40.             PdfDictionary annotation = annots.getAsDictionary(i);
  41.             if (PdfName.Link.equals(annotation.getAsName(PdfName.Subtype))) {
  42.                 PdfArray d = annotation.getAsArray(PdfName.Dest);
  43.                 if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1)))
  44.                     d.set(4, new PdfNumber(0));
  45.             }
  46.         }
  47.  
  48.         pdfDoc.close();
  49.     }
  50. }
XYZDestination.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * This example was written to create a sample file for use in code that answers the following question:
  10.  * http://stackoverflow.com/questions/22828782/all-links-of-existing-pdf-change-the-action-property-to-inherit-zoom-itext-lib
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfWriter;
  16. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  17. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  18. import com.itextpdf.layout.Document;
  19. import com.itextpdf.layout.element.AreaBreak;
  20. import com.itextpdf.layout.element.Link;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. @Category(SampleTest.class)
  30. public class XYZDestination extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/annotations/xyz_destination.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new XYZDestination().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  42.         Document doc = new Document(pdfDoc);
  43.         for (int i = 0; i < 10; i++) {
  44.             doc.add(new Paragraph("Test"));
  45.             doc.add(new AreaBreak());
  46.         }
  47.         PdfDestination d;
  48.         Paragraph c;
  49.         for (int i = 0; i < 10; ) {
  50.             i++;
  51.             d = PdfExplicitDestination.createXYZ(i, 36, 806, 0);
  52.             c = new Paragraph(new Link("Goto page " + i, d));
  53.             doc.add(c);
  54.         }
  55.         doc.close();
  56.     }
  57. }
AddOpenAction.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24087786/how-to-set-zoom-level-to-pdf-using-itextsharp-5-5-0-0
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import java.io.File;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. @Category(SampleTest.class)
  26. public class AddOpenAction extends GenericTest {
  27.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  28.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_open_action.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new AddOpenAction().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  39.         pdfDoc.getCatalog()
  40.                 .setOpenAction(PdfExplicitDestination.createXYZ(1, 0, pdfDoc.getPage(1).getPageSize().getHeight(), 0.75f));
  41.         pdfDoc.close();
  42.     }
  43. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now