Open action

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

7th November 2015
admin-marketing

Switch code for this example

OpenAt100pct.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/31314993/how-to-set-a-default-zoom-of-100-using-itext-java
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfAction;
  13. import com.itextpdf.text.pdf.PdfDestination;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. /**
  23.  *
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class OpenAt100pct {
  28.    
  29.     public static final String DEST = "results/annotations/open100pct.pdf";
  30.  
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new OpenAt100pct().createPdf(DEST);
  35.     }
  36.     public void createPdf(String dest) throws FileNotFoundException, DocumentException {
  37.         Rectangle pagesize = PageSize.LETTER;
  38.         Document document = new Document(pagesize);
  39.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, 842, 1f);
  42.         writer.setOpenAction(PdfAction.gotoLocalPage(1, pdfDest, writer));
  43.         document.add(new Paragraph("Hello World"));
  44.         document.close();
  45.     }
  46. }
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