Adding links to existing documents

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

31st May 2016
admin-marketing

Switch code for this example

AddImageLink.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/26983703/itext-how-to-stamp-image-on-existing-pdf-and-create-an-anchor
  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.PdfArray;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.action.PdfAction;
  22. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  23. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  24. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  25. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. import java.io.File;
  32.  
  33. @Category(SampleTest.class)
  34. public class AddImageLink extends GenericTest {
  35.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_image_link.pdf";
  36.     public static final String IMG = "./src/test/resources/img/info.png";
  37.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new AddImageLink().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  48.  
  49.         ImageData img = ImageDataFactory.create(IMG);
  50.         float x = 10;
  51.         float y = 650;
  52.         float w = img.getWidth();
  53.         float h = img.getHeight();
  54.  
  55.         new PdfCanvas(pdfDoc.getFirstPage().newContentStreamAfter(), pdfDoc.getFirstPage().getResources(), pdfDoc)
  56.                 .addImage(img, x, y, false);
  57.         Rectangle linkLocation = new Rectangle(x, y, w, h);
  58.  
  59.         PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation)
  60.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  61.                 .setAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(pdfDoc.getLastPage())))
  62.                 .setBorder(new PdfArray(new float[]{0, 0, 0}));
  63.         pdfDoc.getFirstPage().addAnnotation(annotation);
  64.  
  65.         pdfDoc.close();
  66.     }
  67. }
AddLinkAnnotation.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. package com.itextpdf.samples.sandbox.annotations;
  9.  
  10. import com.itextpdf.kernel.geom.Rectangle;
  11. import com.itextpdf.kernel.pdf.PdfArray;
  12. import com.itextpdf.kernel.pdf.PdfDocument;
  13. import com.itextpdf.kernel.pdf.PdfReader;
  14. import com.itextpdf.kernel.pdf.PdfWriter;
  15. import com.itextpdf.kernel.pdf.action.PdfAction;
  16. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  17. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  18. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class AddLinkAnnotation extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_annotation.pdf";
  29.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new AddLinkAnnotation().manipulatePdf(DEST);
  35.     }
  36.  
  37.     @Override
  38.     protected void manipulatePdf(String dest) throws Exception {
  39.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  40.         Rectangle linkLocation = new Rectangle(523, 770, 36, 36);
  41.         int[] borders = {0, 0, 1};
  42.         PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation)
  43.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  44.                 .setAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(3)))
  45.                 .setBorder(new PdfArray(borders));
  46.         pdfDoc.getFirstPage().addAnnotation(annotation);
  47.         pdfDoc.close();
  48.     }
  49. }
AddLinkAnnotation2.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/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.action.PdfAction;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Link;
  23. import com.itextpdf.layout.element.Paragraph;
  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 AddLinkAnnotation2 extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_annotation2.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new AddLinkAnnotation2().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.  
  47.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  48.  
  49.         Link link = new Link("The Best iText Questions on StackOverflow", PdfAction.createURI("http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
  50.         Paragraph p = new Paragraph("Download ").add(link.setFont(bold)).add(" and discover more than 200 questions and answers.");
  51.  
  52.         doc.add(p.setFixedPosition(36, 700, 500));
  53.  
  54.         doc.close();
  55.     }
  56. }
AddLinkAnnotation3.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/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.color.Color;
  16. import com.itextpdf.kernel.font.PdfFont;
  17. import com.itextpdf.kernel.font.PdfFontFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.action.PdfAction;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.layout.element.Link;
  24. import com.itextpdf.layout.element.Paragraph;
  25. import com.itextpdf.layout.property.TextAlignment;
  26. import com.itextpdf.layout.property.VerticalAlignment;
  27. import com.itextpdf.samples.GenericTest;
  28. import com.itextpdf.test.annotations.type.SampleTest;
  29.  
  30. import org.junit.Ignore;
  31. import org.junit.experimental.categories.Category;
  32.  
  33. import java.io.File;
  34.  
  35. @Category(SampleTest.class)
  36. public class AddLinkAnnotation3 extends GenericTest {
  37.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_annotation3.pdf";
  38.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  39.  
  40.     public static void main(String[] args) throws Exception {
  41.         File file = new File(DEST);
  42.         file.getParentFile().mkdirs();
  43.         new AddLinkAnnotation3().manipulatePdf(DEST);
  44.     }
  45.  
  46.     @Override
  47.     protected void manipulatePdf(String dest) throws Exception {
  48.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  49.         Document doc = new Document(pdfDoc);
  50.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  51.         Link link = new Link("The Best iText Questions on StackOverflow",
  52.                 PdfAction.createURI("http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
  53.         link.setBackgroundColor(Color.RED);
  54.         link.setFont(bold);
  55.         Paragraph p = new Paragraph("Download ");
  56.         p.add(link);
  57.         p.add(" and discover \nmore than 200 questions and answers.");
  58.         doc.showTextAligned(p, 30, 600, 1, TextAlignment.LEFT,
  59.                 VerticalAlignment.TOP, (float) Math.PI / 6);
  60.         doc.close();
  61.     }
  62. }
AddLinkAnnotation4.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/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.io.font.FontConstants;
  15. import com.itextpdf.kernel.font.PdfFont;
  16. import com.itextpdf.kernel.font.PdfFontFactory;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfReader;
  19. import com.itextpdf.kernel.pdf.PdfWriter;
  20. import com.itextpdf.kernel.pdf.action.PdfAction;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Link;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.property.TextAlignment;
  25. import com.itextpdf.layout.property.VerticalAlignment;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.Ignore;
  30. import org.junit.experimental.categories.Category;
  31.  
  32. import java.io.File;
  33.  
  34. @Category(SampleTest.class)
  35. public class AddLinkAnnotation4 extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_annotation4.pdf";
  37.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new AddLinkAnnotation4().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  48.         Document doc = new Document(pdfDoc);
  49.         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
  50.         Link link = new Link("The Best iText Questions on StackOverflow",
  51.                 PdfAction.createURI("http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
  52.         link.setFont(bold);
  53.         Paragraph p = new Paragraph("Download ");
  54.         p.add(link);
  55.         p.add(" and discover \nmore than 200 questions and answers.");
  56.         doc.showTextAligned(p, 30, 600, 1, TextAlignment.LEFT,
  57.                 VerticalAlignment.TOP, (float) Math.PI / 2);
  58.         doc.close();
  59.     }
  60. }
AddLinkAnnotation5.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/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  11.  */
  12. package com.itextpdf.samples.sandbox.annotations;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.pdf.PdfDocument;
  16. import com.itextpdf.kernel.pdf.PdfReader;
  17. import com.itextpdf.kernel.pdf.PdfWriter;
  18. import com.itextpdf.kernel.pdf.action.PdfAction;
  19. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  20. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.layout.element.Link;
  23. import com.itextpdf.layout.element.Paragraph;
  24. import com.itextpdf.layout.property.TextAlignment;
  25. import com.itextpdf.layout.property.VerticalAlignment;
  26. import com.itextpdf.samples.GenericTest;
  27. import com.itextpdf.test.annotations.type.SampleTest;
  28.  
  29. import org.junit.Ignore;
  30. import org.junit.experimental.categories.Category;
  31.  
  32. import java.io.File;
  33.  
  34. @Category(SampleTest.class)
  35. public class AddLinkAnnotation5 extends GenericTest {
  36.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  37.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_link_annotation5.pdf";
  38.  
  39.     public static void main(String[] args) throws Exception {
  40.         File file = new File(DEST);
  41.         file.getParentFile().mkdirs();
  42.         new AddLinkAnnotation5().manipulatePdf(DEST);
  43.     }
  44.  
  45.     @Override
  46.     protected void manipulatePdf(String dest) throws Exception {
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  48.         Document doc = new Document(pdfDoc);
  49.         Link link = new Link("This is a link. Click it and you'll be forwarded to another page in this document.",
  50.                 PdfAction.createGoTo(PdfExplicitDestination.createFit(3)));
  51.         link.setBackgroundColor(Color.RED);
  52.         link.getLinkAnnotation().setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
  53.  
  54.         Paragraph p = new Paragraph(link).setWidth(240);
  55.         doc.showTextAligned(p, 320, 695, 1, TextAlignment.LEFT,
  56.                 VerticalAlignment.TOP, 0);
  57.         doc.close();
  58.     }
  59. }
AddPolygonLink.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.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.action.PdfAction;
  17. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  18. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  19. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  20. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class AddPolygonLink extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/annotations/add_polygon_link.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new AddPolygonLink().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  43.         Document doc = new Document(pdfDoc);
  44.  
  45.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
  46.         canvas.moveTo(36, 700)
  47.                 .lineTo(72, 760)
  48.                 .lineTo(144, 720)
  49.                 .lineTo(72, 730)
  50.                 .closePathStroke();
  51.         Rectangle linkLocation = new Rectangle(36, 700, 144, 760);
  52.         PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(linkLocation)
  53.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  54.                 .setAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(1)));
  55.         PdfArray arrayOfQuadPoints = new PdfArray(new int[]{72, 730, 144, 720, 72, 760, 36, 700});
  56.         linkAnnotation.put(PdfName.QuadPoints, arrayOfQuadPoints);
  57.  
  58.         pdfDoc.getFirstPage().addAnnotation(linkAnnotation);
  59.  
  60.         doc.close();
  61.     }
  62. }
AddNavigation.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/31240828/how-navigate-one-page-to-another-page-in-pdf-file-by-using-itextsharp-c
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.kernel.geom.Rectangle;
  15. import com.itextpdf.kernel.pdf.*;
  16. import com.itextpdf.kernel.pdf.action.PdfAction;
  17. import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
  18. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  19. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  20. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  21. import com.itextpdf.layout.Document;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28.  
  29. @Category(SampleTest.class)
  30. public class AddNavigation extends GenericTest {
  31.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_navigation.pdf";
  32.     public static final String SRC = "./src/test/resources/pdfs/primes.pdf";
  33.  
  34.     public static void main(String[] args) throws Exception {
  35.         File file = new File(DEST);
  36.         file.getParentFile().mkdirs();
  37.         new AddNavigation().manipulatePdf(DEST);
  38.     }
  39.  
  40.     @Override
  41.     protected void manipulatePdf(String dest) throws Exception {
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  43.         Document doc = new Document(pdfDoc);
  44.         int[] borders = {0, 0, 1};
  45.         PdfDestination d1 = PdfExplicitDestination.createFit(10);
  46.         Rectangle rect = new Rectangle(0, 806, 595, 36);
  47.         PdfAnnotation a10 = new PdfLinkAnnotation(rect)
  48.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
  49.                 .setPage(pdfDoc.getPage(10))
  50.                 .setAction(PdfAction.createGoTo(d1))
  51.                 .setBorder(new PdfArray(borders))
  52.                 .setColor(new PdfArray(new float[]{0, 1, 0}));
  53.         pdfDoc.getPage(1).addAnnotation(a10);
  54.         PdfDestination d2 = PdfExplicitDestination.createFit(1);
  55.         PdfAnnotation a1 = new PdfLinkAnnotation(rect)
  56.                 .setHighlightMode(PdfAnnotation.HIGHLIGHT_PUSH)
  57.                 .setPage(pdfDoc.getPage(1))
  58.                 .setAction(PdfAction.createGoTo(d2))
  59.                 .setBorder(new PdfArray(borders))
  60.                 .setColor(new PdfArray(new float[]{0, 1, 0}));
  61.         pdfDoc.getPage(10).addAnnotation(a1);
  62.         doc.close();
  63.     }
  64. }
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