Changing the zoom factor in a link destination

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

24th October 2015
admin-marketing

Switch code for this example

XYZDestination.java
  1. /**
  2.  * This example was written to create a sample file for use in code that answers the following question:
  3.  * http://stackoverflow.com/questions/22828782/all-links-of-existing-pdf-change-the-action-property-to-inherit-zoom-itext-lib
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfAnnotation;
  13. import com.itextpdf.text.pdf.PdfDestination;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19.  
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class XYZDestination {
  24.  
  25.     public static final String DEST = "results/annotations/xyz_destination.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new XYZDestination().createPdf(DEST);
  31.     }
  32.    
  33.     public void createPdf(String dest) throws IOException, DocumentException {
  34.         Document document = new Document();
  35.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  36.         document.open();
  37.         for (int i = 0; i < 10; i++) {
  38.             document.add(new Paragraph("Test"));
  39.             document.newPage();
  40.         }
  41.         PdfDestination d;
  42.         Chunk c;
  43.         for (int i = 0; i < 10; ) {
  44.             i++;
  45.             d = new PdfDestination(PdfDestination.XYZ, 36, 806, i);
  46.             c = new Chunk("Goto page " + i);
  47.             c.setAnnotation(PdfAnnotation.createLink(writer, new Rectangle(0, 0), PdfAnnotation.HIGHLIGHT_NONE, i, d));
  48.             document.add(new Paragraph(c));
  49.         }
  50.         document.close();
  51.     }
  52. }
ChangeZoomXYZDestination.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22828782/all-links-of-existing-pdf-change-the-action-property-to-inherit-zoom-itext-lib
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfArray;
  9. import com.itextpdf.text.pdf.PdfDictionary;
  10. import com.itextpdf.text.pdf.PdfName;
  11. import com.itextpdf.text.pdf.PdfNumber;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class ChangeZoomXYZDestination {
  22.  
  23.     public static final String SRC = "resources/pdfs/xyz_destination.pdf";
  24.     public static final String DEST = "results/annotations/xyz_zoom.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ChangeZoomXYZDestination().manipulatePdf(SRC, DEST);;
  30.     }
  31.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  32.         PdfReader reader = new PdfReader(src);
  33.         PdfDictionary page = reader.getPageN(11);
  34.         PdfArray annots = page.getAsArray(PdfName.ANNOTS);
  35.         for (int i = 0; i < annots.size(); i++) {
  36.             PdfDictionary annotation = annots.getAsDict(i);
  37.             if (PdfName.LINK.equals(annotation.getAsName(PdfName.SUBTYPE))) {
  38.                 PdfArray d = annotation.getAsArray(PdfName.DEST);
  39.                 if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1)))
  40.                     d.set(4, new PdfNumber(0));
  41.             }
  42.         }
  43.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  44.         stamper.close();
  45.     }
  46. }
AddOpenAction.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24087786/how-to-set-zoom-level-to-pdf-using-itextsharp-5-5-0-0
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.PdfAction;
  9. import com.itextpdf.text.pdf.PdfDestination;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class AddOpenAction {
  20.  
  21.     public static final String SRC = "resources/pdfs/hello.pdf";
  22.     public static final String DEST = "results/stamper/hello_open75p.pdf";
  23.  
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new AddOpenAction().manipulatePdf(SRC, DEST);
  28.     }
  29.  
  30.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  31.         PdfReader reader = new PdfReader(src);
  32.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  33.         PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 0.75f);
  34.         PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, stamper.getWriter());
  35.         stamper.getWriter().setOpenAction(action);
  36.         stamper.close();
  37.         reader.close();
  38.     }
  39. }
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