Adding links to existing documents

25th October 2015
admin-marketing

Switch code for this example

AddImageLink.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26983703/itext-how-to-stamp-image-on-existing-pdf-and-create-an-anchor
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Image;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfAnnotation;
  11. import com.itextpdf.text.pdf.PdfBorderArray;
  12. import com.itextpdf.text.pdf.PdfDestination;
  13. import com.itextpdf.text.pdf.PdfReader;
  14. import com.itextpdf.text.pdf.PdfStamper;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class AddImageLink {
  23.  
  24.     public static final String SRC = "resources/pdfs/primes.pdf";
  25.     public static final String IMG = "resources/images/info.png";
  26.     public static final String DEST = "results/annotations/link_image.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddImageLink().manipulatePdf(SRC, DEST);
  32.     }
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         Image img = Image.getInstance(IMG);
  37.         float x = 10;
  38.         float y = 650;
  39.         float w = img.getScaledWidth();
  40.         float h = img.getScaledHeight();
  41.         img.setAbsolutePosition(x, y);
  42.         stamper.getOverContent(1).addImage(img);
  43.         Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
  44.         PdfDestination destination = new PdfDestination(PdfDestination.FIT);
  45.         PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
  46.                 linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
  47.                 reader.getNumberOfPages(), destination);
  48.         link.setBorder(new PdfBorderArray(0, 0, 0));
  49.         stamper.addAnnotation(link, 1);
  50.         stamper.close();
  51.     }
  52. }
AddPolygonLink.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27083206/itextshape-clickable-polygon-or-path
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfContentByte;
  12. import com.itextpdf.text.pdf.PdfDestination;
  13. import com.itextpdf.text.pdf.PdfName;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class AddPolygonLink {
  24.  
  25.     public static final String SRC = "resources/pdfs/hello.pdf";
  26.     public static final String DEST = "results/annotations/link_polygon.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddPolygonLink().manipulatePdf(SRC, DEST);;
  32.     }
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         PdfContentByte canvas = stamper.getOverContent(1);
  37.         canvas.moveTo(36, 700);
  38.         canvas.lineTo(72, 760);
  39.         canvas.lineTo(144, 720);
  40.         canvas.lineTo(72, 730);
  41.         canvas.closePathStroke();
  42.         Rectangle linkLocation = new Rectangle(36, 700, 144, 760);
  43.         PdfArray array = new PdfArray(new int[]{72, 730, 144, 720, 72, 760, 36, 700});
  44.         PdfDestination destination = new PdfDestination(PdfDestination.FIT);
  45.         PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
  46.                 linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
  47.                 1, destination);
  48.         link.put(PdfName.QUADPOINTS, array);
  49.         stamper.addAnnotation(link, 1);
  50.         stamper.close();
  51.     }
  52. }
AddLinkAnnotation.java
  1. package sandbox.annotations;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Rectangle;
  5. import com.itextpdf.text.pdf.PdfAnnotation;
  6. import com.itextpdf.text.pdf.PdfDestination;
  7. import com.itextpdf.text.pdf.PdfReader;
  8. import com.itextpdf.text.pdf.PdfStamper;
  9.  
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13.  
  14. import sandbox.WrapToTest;
  15.  
  16. @WrapToTest
  17. public class AddLinkAnnotation {
  18.  
  19.     public static final String SRC = "resources/pdfs/primes.pdf";
  20.     public static final String DEST = "results/annotations/link_annotation.pdf";
  21.  
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new AddLinkAnnotation().manipulatePdf(SRC, DEST);
  26.     }
  27.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  28.         PdfReader reader = new PdfReader(src);
  29.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  30.         Rectangle linkLocation = new Rectangle(523, 770, 559, 806);
  31.         PdfDestination destination = new PdfDestination(PdfDestination.FIT);
  32.         PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
  33.                 linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
  34.                 3, destination);
  35.         stamper.addAnnotation(link, 1);
  36.         stamper.close();
  37.         reader.close();
  38.     }
  39. }
AddLinkAnnotation2.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Font.FontFamily;
  11. import com.itextpdf.text.Phrase;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfContentByte;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class AddLinkAnnotation2 {
  24.  
  25.     public static final String SRC = "resources/pdfs/hello.pdf";
  26.     public static final String DEST = "results/annotations/link_annotation2.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddLinkAnnotation2().manipulatePdf(SRC, DEST);
  32.     }
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         PdfContentByte canvas = stamper.getOverContent(1);
  37.         Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  38.         Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
  39.         chunk.setAnchor("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
  40.         Phrase p = new Phrase("Download ");
  41.         p.add(chunk);
  42.         p.add(" and discover more than 200 questions and answers.");
  43.         ColumnText ct = new ColumnText(canvas);
  44.         ct.setSimpleColumn(36, 700, 559, 750);
  45.         ct.addText(p);
  46.         ct.go();
  47.         stamper.close();
  48.         reader.close();
  49.     }
  50. }
AddLinkAnnotation3.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.awt.geom.AffineTransform;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Font.FontFamily;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.ColumnText;
  16. import com.itextpdf.text.pdf.PdfAction;
  17. import com.itextpdf.text.pdf.PdfAnnotation;
  18. import com.itextpdf.text.pdf.PdfContentByte;
  19. import com.itextpdf.text.pdf.PdfPageEventHelper;
  20. import com.itextpdf.text.pdf.PdfReader;
  21. import com.itextpdf.text.pdf.PdfStamper;
  22. import com.itextpdf.text.pdf.PdfWriter;
  23.  
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import sandbox.WrapToTest;
  28.  
  29. @WrapToTest
  30. public class AddLinkAnnotation3 {
  31.  
  32.     public class AddAnnotation extends PdfPageEventHelper {
  33.         protected PdfStamper stamper;
  34.         protected AffineTransform transform;
  35.        
  36.         public AddAnnotation(PdfStamper stamper, AffineTransform transform) {
  37.             this.stamper = stamper;
  38.             this.transform = transform;
  39.         }
  40.        
  41.         @Override
  42.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  43.             float[] pts = {rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()};
  44.             transform.transform(pts, 0, pts, 0, 2);
  45.             float[] dstPts = {pts[0], pts[1], pts[2], pts[3]};
  46.             rect = new Rectangle(dstPts[0], dstPts[1], dstPts[2], dstPts[3]);
  47.             PdfAnnotation annot = PdfAnnotation.createLink(writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction(text));
  48.             stamper.addAnnotation(annot, 1);
  49.         }
  50.        
  51.     }
  52.    
  53.     public static final String SRC = "resources/pdfs/hello.pdf";
  54.     public static final String DEST = "results/annotations/link_annotation3.pdf";
  55.  
  56.     public static void main(String[] args) throws IOException, DocumentException {
  57.         File file = new File(DEST);
  58.         file.getParentFile().mkdirs();
  59.         new AddLinkAnnotation3().manipulatePdf(SRC, DEST);
  60.     }
  61.    
  62.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  63.         PdfReader reader = new PdfReader(src);
  64.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  65.         AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 6);
  66.         stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform));
  67.         PdfContentByte canvas = stamper.getOverContent(1);
  68.         Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  69.         Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
  70.         chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
  71.         Phrase p = new Phrase("Download ");
  72.         p.add(chunk);
  73.         p.add(" and discover more than 200 questions and answers.");
  74.         canvas.saveState();
  75.         canvas.concatCTM(transform);
  76.         ColumnText ct = new ColumnText(canvas);
  77.         ct.setSimpleColumn(300, 0, 800, 400);
  78.         ct.addText(p);
  79.         ct.go();
  80.         canvas.restoreState();
  81.         stamper.close();
  82.         reader.close();
  83.     }
  84. }
AddLinkAnnotation4.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/28554413/how-to-add-overlay-text-with-link-annotations-to-existing-pdf
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.awt.geom.AffineTransform;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Font;
  12. import com.itextpdf.text.Font.FontFamily;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.Rectangle;
  15. import com.itextpdf.text.pdf.ColumnText;
  16. import com.itextpdf.text.pdf.PdfAction;
  17. import com.itextpdf.text.pdf.PdfAnnotation;
  18. import com.itextpdf.text.pdf.PdfBorderArray;
  19. import com.itextpdf.text.pdf.PdfContentByte;
  20. import com.itextpdf.text.pdf.PdfPageEventHelper;
  21. import com.itextpdf.text.pdf.PdfReader;
  22. import com.itextpdf.text.pdf.PdfStamper;
  23. import com.itextpdf.text.pdf.PdfWriter;
  24.  
  25. import java.io.File;
  26. import java.io.FileOutputStream;
  27. import java.io.IOException;
  28. import sandbox.WrapToTest;
  29.  
  30. @WrapToTest
  31. public class AddLinkAnnotation4 {
  32.  
  33.     public class AddAnnotation extends PdfPageEventHelper {
  34.         protected PdfStamper stamper;
  35.         protected AffineTransform transform;
  36.        
  37.         public AddAnnotation(PdfStamper stamper, AffineTransform transform) {
  38.             this.stamper = stamper;
  39.             this.transform = transform;
  40.         }
  41.        
  42.         @Override
  43.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  44.             float[] pts = {rect.getLeft(), rect.getBottom(), rect.getRight(), rect.getTop()};
  45.             transform.transform(pts, 0, pts, 0, 2);
  46.             float[] dstPts = {pts[0], pts[1], pts[2], pts[3]};
  47.             rect = new Rectangle(dstPts[0], dstPts[1], dstPts[2], dstPts[3]);
  48.             PdfAnnotation annot = PdfAnnotation.createLink(writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction(text));
  49.             annot.setBorder(new PdfBorderArray(0, 0, 0));
  50.             stamper.addAnnotation(annot, 1);
  51.         }
  52.        
  53.     }
  54.    
  55.     public static final String SRC = "resources/pdfs/hello.pdf";
  56.     public static final String DEST = "results/annotations/link_annotation4.pdf";
  57.  
  58.     public static void main(String[] args) throws IOException, DocumentException {
  59.         File file = new File(DEST);
  60.         file.getParentFile().mkdirs();
  61.         new AddLinkAnnotation4().manipulatePdf(SRC, DEST);
  62.     }
  63.    
  64.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  65.         PdfReader reader = new PdfReader(src);
  66.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  67.         AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 2);
  68.         stamper.getWriter().setPageEvent(new AddAnnotation(stamper, transform));
  69.         PdfContentByte canvas = stamper.getOverContent(1);
  70.         Font bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
  71.         Chunk chunk = new Chunk("The Best iText Questions on StackOverflow", bold);
  72.         chunk.setGenericTag("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
  73.         Phrase p = new Phrase("Download ");
  74.         p.add(chunk);
  75.         p.add(" and discover more than 200 questions and answers.");
  76.         canvas.saveState();
  77.         canvas.concatCTM(transform);
  78.         ColumnText ct = new ColumnText(canvas);
  79.         ct.setSimpleColumn(36, -559, 806, -36);
  80.         ct.addText(p);
  81.         ct.go();
  82.         canvas.restoreState();
  83.         stamper.close();
  84.         reader.close();
  85.     }
  86. }
AddNavigation.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/31240828/how-navigate-one-page-to-another-page-in-pdf-file-by-using-itextsharp-c
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfDestination;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13.  
  14. import java.io.File;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import sandbox.WrapToTest;
  18.  
  19. /**
  20.  * @author iText
  21.  */
  22. @WrapToTest
  23. public class AddNavigation {
  24.    
  25.     public static final String SRC = "resources/pdfs/primes.pdf";
  26.     public static final String DEST = "results/stamper/primes_links.pdf";
  27.  
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddNavigation().manipulatePdf(SRC, DEST);
  32.     }
  33.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  34.         PdfReader reader = new PdfReader(src);
  35.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  36.         PdfDestination d1 = new PdfDestination(PdfDestination.FIT);
  37.         Rectangle rect = new Rectangle(0, 806, 595, 842);
  38.         PdfAnnotation a10 = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_INVERT, 10, d1);
  39.         stamper.addAnnotation(a10, 1);
  40.         PdfDestination d2 = new PdfDestination(PdfDestination.FIT);
  41.         PdfAnnotation a1 = PdfAnnotation.createLink(stamper.getWriter(), rect, PdfAnnotation.HIGHLIGHT_PUSH, 1, d2);
  42.         stamper.addAnnotation(a1, 10);
  43.         stamper.close();
  44.     }
  45. }
AddLinkAnnotation5.java
  1. package sandbox.annotations;
  2.  
  3. import com.itextpdf.text.DocumentException;
  4. import com.itextpdf.text.Paragraph;
  5. import com.itextpdf.text.Rectangle;
  6. import com.itextpdf.text.pdf.ColumnText;
  7. import com.itextpdf.text.pdf.PdfAnnotation;
  8. import com.itextpdf.text.pdf.PdfBorderArray;
  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.  
  17. import sandbox.WrapToTest;
  18.  
  19. @WrapToTest
  20. public class AddLinkAnnotation5 {
  21.  
  22.     public static final String SRC = "resources/pdfs/primes.pdf";
  23.     public static final String DEST = "results/annotations/link_annotation5.pdf";
  24.  
  25.     public static void main(String[] args) throws IOException, DocumentException {
  26.         File file = new File(DEST);
  27.         file.getParentFile().mkdirs();
  28.         new AddLinkAnnotation5().manipulatePdf(SRC, DEST);
  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.         // Here we define the location:
  34.         Rectangle linkLocation = new Rectangle(320, 695, 560, 741);
  35.         // here we add the actual content at this location:
  36.         ColumnText ct = new ColumnText(stamper.getOverContent(1));
  37.         ct.setSimpleColumn(linkLocation);
  38.         ct.addElement(new Paragraph("This is a link. Click it and you'll be forwarded to another page in this document."));
  39.         ct.go();
  40.         // now we create the link that will jump to a specific destination:
  41.         PdfDestination destination = new PdfDestination(PdfDestination.FIT);
  42.         PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
  43.                 linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
  44.                 3, destination);
  45.         // If you don't want a border, here's where you remove it:
  46.         link.setBorder(new PdfBorderArray(0, 0, 0));
  47.         // We add the link (that is the clickable area, not the text!)
  48.         stamper.addAnnotation(link, 1);
  49.         stamper.close();
  50.         reader.close();
  51.     }
  52. }
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