Creating and adding annotations

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

31st May 2016
iText PDF

Switch code for this example

AddLinkImages.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/29388313/itext-how-to-associate-actions-with-graphical-object
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.action.PdfAction;
  18. import com.itextpdf.kernel.pdf.canvas.wmf.WmfImageData;
  19. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Image;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.property.Property;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30. import java.net.MalformedURLException;
  31.  
  32. @Category(SampleTest.class)
  33. public class AddLinkImages extends GenericTest {
  34.     public static final String sourceFolder = "./src/test/resources/img/";
  35.     public static final String BUTTERFLY = sourceFolder + "butterfly.wmf";
  36.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_images.pdf";
  37.     public static final String DOG = sourceFolder + "dog.bmp";
  38.     public static final String FOX = sourceFolder + "fox.bmp";
  39.     public static final String INFO = sourceFolder + "info.png";
  40.  
  41.     public static void main(String[] args) throws Exception {
  42.         File file = new File(DEST);
  43.         file.getParentFile().mkdirs();
  44.         new AddLinkImages().manipulatePdf(DEST);
  45.     }
  46.  
  47.     @Override
  48.     protected void manipulatePdf(String dest) throws Exception {
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  50.         Document doc = new Document(pdfDoc);
  51.         Paragraph p = new Paragraph("Objects with links");
  52.         p.add(createImage(INFO, "http://itextpdf.com/"));
  53.         p.add(createImage(DOG, "http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
  54.         p.add(createImage(FOX, "http://stackoverflow.com/q/29388313/1622493"));
  55.         p.add(new Image(new PdfFormXObject(new WmfImageData(BUTTERFLY), pdfDoc)).
  56.                 setAction(PdfAction.createURI("http://stackoverflow.com/questions/tagged/itext*")));
  57.         doc.add(p);
  58.         doc.close();
  59.     }
  60.  
  61.     public Image createImage(String src, String url) throws MalformedURLException {
  62.         Image img = new Image(ImageDataFactory.create(src));
  63.         img.setProperty(Property.ACTION, PdfAction.createURI(url));
  64.         return img;
  65.     }
  66. }
AddPointerAnnotation.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/26752663/adding-maps-at-itext-java
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.geom.PageSize;
  17. import com.itextpdf.kernel.geom.Rectangle;
  18. import com.itextpdf.kernel.pdf.*;
  19. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  20. import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Image;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class AddPointerAnnotation extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_pointer_annotation.pdf";
  33.     public static final String IMG = "./src/test/resources/img/map_cic.png";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddPointerAnnotation().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         Image img = new Image(ImageDataFactory.create(IMG));
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  45.         Document doc = new Document(pdfDoc, new PageSize(img.getImageWidth(), img.getImageHeight()));
  46.         img.setFixedPosition(0, 0);
  47.         doc.add(img);
  48.         Rectangle rect = new Rectangle(220, 350, 255, 245);
  49.         PdfLineAnnotation lineAnnotation = new PdfLineAnnotation(rect, new float[]{225, 355, 470, 590});
  50.         lineAnnotation.setTitle(new PdfString("You are here:"));
  51.         lineAnnotation.setContents("Cambridge Innovation Center");
  52.         lineAnnotation.setColor(Color.RED);
  53.         lineAnnotation.setFlag(PdfAnnotation.PRINT);
  54.  
  55.         PdfDictionary borderStyle = new PdfDictionary();
  56.         borderStyle.put(PdfName.S, PdfName.S);
  57.         borderStyle.put(PdfName.W, new PdfNumber(5));
  58.         lineAnnotation.setBorderStyle(borderStyle);
  59.  
  60.         PdfArray le = new PdfArray();
  61.         le.add(new PdfName("OpenArrow"));
  62.         le.add(new PdfName("None"));
  63.         lineAnnotation.put(new PdfName("LE"), le);
  64.         lineAnnotation.put(new PdfName("IT"), new PdfName("LineArrow"));
  65.  
  66.         pdfDoc.getFirstPage().addAnnotation(lineAnnotation);
  67.         doc.close();
  68.     }
  69. }
AddRotatedAnnotation.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/27083206/itextshape-clickable-polygon-or-path
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.*;
  17. import com.itextpdf.kernel.pdf.action.PdfAction;
  18. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  19. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  20. import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
  21. import com.itextpdf.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class AddRotatedAnnotation extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_rotated_annotation.pdf";
  31.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new AddRotatedAnnotation().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  42.  
  43.         PdfAction action = PdfAction.createURI("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
  44.         Rectangle linkLocation1 = new Rectangle(30, 770, 90, 30);
  45.         PdfAnnotation link1 = new PdfLinkAnnotation(linkLocation1)
  46.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  47.                 .setAction(action)
  48.                 .setColor(Color.RED.getColorValue());
  49.         pdfDoc.getFirstPage().addAnnotation(link1);
  50.  
  51.         Rectangle linkLocation2 = new Rectangle(30, 670, 30, 90);
  52.         PdfAnnotation link2 = new PdfLinkAnnotation(linkLocation2)
  53.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  54.                 .setAction(action)
  55.                 .setColor(Color.GREEN.getColorValue());
  56.         pdfDoc.getFirstPage().addAnnotation(link2);
  57.  
  58.         Rectangle linkLocation3 = new Rectangle(150, 770, 90, 30);
  59.         PdfAnnotation stamp1 = new PdfStampAnnotation(linkLocation3)
  60.                 .setStampName(new PdfName("Confidential"))
  61.                 .setContents("Landscape");
  62.         pdfDoc.getFirstPage().addAnnotation(stamp1);
  63.  
  64.         Rectangle linkLocation4 = new Rectangle(150, 670, 90, 90);
  65.         PdfAnnotation stamp2 = new PdfStampAnnotation(linkLocation4)
  66.                 .setStampName(new PdfName("Confidential"))
  67.                 .setContents("Portrait")
  68.                 .put(PdfName.Rotate, new PdfNumber(90));
  69.         pdfDoc.getFirstPage().addAnnotation(stamp2);
  70.  
  71.         Rectangle linkLocation5 = new Rectangle(250, 670, 90, 90);
  72.         PdfAnnotation stamp3 = new PdfStampAnnotation(linkLocation5)
  73.                 .setStampName(new PdfName("Confidential"))
  74.                 .setContents("Portrait")
  75.                 .put(PdfName.Rotate, new PdfNumber(45));
  76.         pdfDoc.getFirstPage().addAnnotation(stamp3);
  77.  
  78.         pdfDoc.close();
  79.     }
  80. }
AddStamp.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/29229629/how-to-add-a-printable-or-non-printable-bitmap-stamp-to-a-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfName;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  22. import com.itextpdf.kernel.pdf.annot.PdfStampAnnotation;
  23. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  24. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  25. import com.itextpdf.samples.GenericTest;
  26. import com.itextpdf.test.annotations.type.SampleTest;
  27.  
  28. import org.junit.experimental.categories.Category;
  29.  
  30. import java.io.File;
  31.  
  32. @Category(SampleTest.class)
  33. public class AddStamp extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_stamp.pdf";
  35.     public static final String IMG = "./src/test/resources/img/itext.png";
  36.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new AddStamp().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  47.  
  48.         ImageData img = ImageDataFactory.create(IMG);
  49.         float w = img.getWidth();
  50.         float h = img.getHeight();
  51.         Rectangle location = new Rectangle(36, 770 - h, w, h);
  52.         PdfStampAnnotation stamp = new PdfStampAnnotation(location)
  53.                 .setStampName(new PdfName("ITEXT"));
  54.         PdfFormXObject xObj = new PdfFormXObject(new Rectangle(w, h));
  55.         PdfCanvas canvas = new PdfCanvas(xObj, pdfDoc);
  56.         canvas.addImage(img, 0, 0, false);
  57.         stamp.setNormalAppearance(xObj.getPdfObject());
  58.         stamp.setFlags(PdfAnnotation.PRINT);
  59.  
  60.         pdfDoc.getFirstPage().addAnnotation(stamp);
  61.         pdfDoc.close();
  62.     }
  63. }
FileAttachmentAnnot.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/31006683/itext-clickable-image-should-open-ms-word-attachment
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.image.ImageData;
  15. import com.itextpdf.io.image.ImageDataFactory;
  16. import com.itextpdf.kernel.geom.Rectangle;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  20. import com.itextpdf.kernel.pdf.annot.PdfFileAttachmentAnnotation;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.kernel.pdf.filespec.PdfFileSpec;
  23. import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.io.File;
  30.  
  31. @Category(SampleTest.class)
  32. public class FileAttachmentAnnot extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/annotations/file_attachment_annot.pdf";
  34.     public static final String IMG = "./src/test/resources/img/info.png";
  35.     public static final String PATH = "./src/test/resources/txt/test.docx";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new FileAttachmentAnnot().manipulatePdf(DEST);
  41.     }
  42.  
  43.     @Override
  44.     protected void manipulatePdf(String dest) throws Exception {
  45.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  46.         Rectangle rect = new Rectangle(36, 700, 100, 100);
  47.         PdfFileSpec fs = PdfFileSpec.createEmbeddedFileSpec(pdfDoc, PATH, null, "test.docx", null, null, false);
  48.         PdfAnnotation attachment = new PdfFileAttachmentAnnotation(rect, fs)
  49.                 .setContents("Click me");
  50.  
  51.         PdfFormXObject xObject = new PdfFormXObject(rect);
  52.         ImageData imageData = ImageDataFactory.create(IMG);
  53.         PdfCanvas canvas = new PdfCanvas(xObject, pdfDoc);
  54.         canvas.addImage(imageData, rect, true);
  55.         attachment.setNormalAppearance(xObject.getPdfObject());
  56.  
  57.         pdfDoc.addNewPage().addAnnotation(attachment);
  58.         pdfDoc.close();
  59.     }
  60. }
ImagesLinksTable.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/32839816/export-hyperlink-to-pdf-file-from-itextsharp-library-in-c-sharp
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.image.ImageDataFactory;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.kernel.pdf.action.PdfAction;
  18. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  19. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.Image;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.property.Property;
  25. import com.itextpdf.layout.renderer.DrawContext;
  26. import com.itextpdf.layout.renderer.ImageRenderer;
  27. import com.itextpdf.samples.GenericTest;
  28. import com.itextpdf.test.annotations.type.SampleTest;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. import java.io.File;
  33.  
  34. @Category(SampleTest.class)
  35. public class ImagesLinksTable extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/annotations/images_links_table.pdf";
  37.     public static final String IMG = "./src/test/resources/img/info.png";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new ImagesLinksTable().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  48.         Document doc = new Document(pdfDoc);
  49.  
  50.         Image img = new Image(ImageDataFactory.create(IMG));
  51.         Paragraph anchor = new Paragraph().add(img);
  52.         anchor.setProperty(Property.ACTION, PdfAction.createURI("http://lowagie.com/"));
  53.  
  54.         Table table = new Table(3);
  55.         table.addCell(anchor);
  56.         table.addCell("A");
  57.         table.addCell("B");
  58.         table.addCell("C");
  59.  
  60.         Image img2 = new Image(ImageDataFactory.create(IMG));
  61.         img2.setNextRenderer(new LinkImageRenderer(img2));
  62.         table.addCell(img2);
  63.  
  64.         doc.add(table);
  65.         doc.close();
  66.     }
  67.  
  68.  
  69.     protected class LinkImageRenderer extends ImageRenderer {
  70.         public LinkImageRenderer(Image image) {
  71.             super(image);
  72.         }
  73.  
  74.         @Override
  75.         public void draw(DrawContext drawContext) {
  76.             super.draw(drawContext);
  77.             PdfAnnotation a = new PdfLinkAnnotation(getOccupiedAreaBBox())
  78.                     .setAction(PdfAction.createURI("http://lowagie.com/bio"));
  79.             drawContext.getDocument().getLastPage().addAnnotation(a);
  80.         }
  81.     }
  82. }
RelativeLink.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/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation
  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.action.PdfAction;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Link;
  19. import com.itextpdf.layout.element.Paragraph;
  20. import com.itextpdf.samples.GenericTest;
  21. import com.itextpdf.test.annotations.type.SampleTest;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. import java.io.File;
  26.  
  27. @Category(SampleTest.class)
  28. public class RelativeLink extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/annotations/relative_link.pdf";
  30.     public static final String XML = "./src/test/resources/xml/data.xml";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new RelativeLink().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  41.         Document doc = new Document(pdfDoc);
  42.  
  43.         Paragraph chunk = new Paragraph(new Link("Click me", PdfAction.createURI("../../../../." + XML)));
  44.         doc.add(chunk);
  45.  
  46.         doc.close();
  47.     }
  48. }
RemoteGoToPage.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/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
  11.  *
  12.  * Creating a link from one PDF to another
  13.  */
  14. package com.itextpdf.samples.sandbox.annotations;
  15.  
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.action.PdfAction;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.AreaBreak;
  21. import com.itextpdf.layout.element.Link;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class RemoteGoToPage extends GenericTest {
  31.     // !IMPORTANT We change the order of SRC and DEST because we want to check via CompareTool
  32.     // comprehensive file rather then simple
  33.     // So DEST file DOES NOT mean destination but DEST-file-which-we-will-check-from-GenericTest
  34.     public static final String SRC = "./target/test/resources/sandbox/annotations/subdir/xyz2.pdf";
  35.     public static final String DEST = "./target/test/resources/sandbox/annotations/remote_go_to_page.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         new RemoteGoToPage().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         File file = new File(SRC);
  44.         file.getParentFile().mkdirs();
  45.         RemoteGoToPage app = new RemoteGoToPage();
  46.         app.createPdf(SRC);
  47.         app.createPdf2(DEST);
  48.     }
  49.  
  50.  
  51.     private void createPdf(String src) throws Exception {
  52.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(src));
  53.         Document doc = new Document(pdfDoc);
  54.         doc.add(new Paragraph("page 1"));
  55.         doc.add(new AreaBreak());
  56.         doc.add(new Paragraph("page 2"));
  57.         doc.add(new AreaBreak());
  58.         doc.add(new Paragraph("page 3"));
  59.         doc.add(new AreaBreak());
  60.         doc.add(new Paragraph("page 4"));
  61.         doc.add(new AreaBreak());
  62.         doc.add(new Paragraph("page 5"));
  63.         doc.add(new AreaBreak());
  64.         doc.add(new Paragraph("page 6"));
  65.         doc.add(new AreaBreak());
  66.         doc.add(new Paragraph("page 7"));
  67.         doc.close();
  68.     }
  69.  
  70.     private void createPdf2(String dest) throws Exception {
  71.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  72.         Document doc = new Document(pdfDoc);
  73.         Paragraph chunk = new Paragraph(new Link("Link", PdfAction.createGoToR("subdir/xyz2.pdf", 6)));
  74.         doc.add(chunk);
  75.         doc.close();
  76.     }
  77. }
RemoteGoto.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/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
  11.  *
  12.  * Creating a link from one PDF to another
  13.  */
  14. package com.itextpdf.samples.sandbox.annotations;
  15.  
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.action.PdfAction;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Link;
  21. import com.itextpdf.layout.element.Paragraph;
  22. import com.itextpdf.layout.property.Property;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class RemoteGoto extends GenericTest {
  31.     // !IMPORTANT We change the order of SRC and DEST because we want to check via CompareTool
  32.     // comprehensive file rather then simple
  33.     // So DEST file DOES NOT mean destination but DEST-file-which-we-will-check-from-GenericTest
  34.     public static final String SRC = "./target/test/resources/sandbox/annotations/subdir/xyz.pdf";
  35.     public static final String DEST = "./target/test/resources/sandbox/annotations/remote_goto.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         new RemoteGoto().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         File file = new File(SRC);
  44.         file.getParentFile().mkdirs();
  45.         RemoteGoto app = new RemoteGoto();
  46.         app.createPdf(SRC);
  47.         app.createPdf2(DEST);
  48.     }
  49.  
  50.     private void createPdf(String src) throws Exception {
  51.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(src));
  52.         Document doc = new Document(pdfDoc);
  53.         Paragraph anchor = new Paragraph("This is a destination");
  54.         anchor.setProperty(Property.DESTINATION, "dest");
  55.         doc.add(anchor);
  56.         doc.close();
  57.     }
  58.  
  59.     private void createPdf2(String dest) throws Exception {
  60.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  61.         Document doc = new Document(pdfDoc);
  62.         Paragraph chunk = new Paragraph(new Link("Link", PdfAction.createGoToR("subdir/xyz.pdf", "dest")));
  63.         doc.add(chunk);
  64.         doc.close();
  65.     }
  66. }
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