Creating and adding annotations

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

6th November 2015
admin-marketing

Switch code for this example

RemoteGoto.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
  4.  *
  5.  * Creating a link from one PDF to another
  6.  */
  7. package sandbox.annotations;
  8.  
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Anchor;
  14. import com.itextpdf.text.Chunk;
  15. import com.itextpdf.text.Document;
  16. import com.itextpdf.text.DocumentException;
  17. import com.itextpdf.text.pdf.PdfAction;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class RemoteGoto {
  21.     public static final String DEST = "results/annotations/subdir/abc.pdf";
  22.     public static final String SRC = "results/annotations/xyz.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         RemoteGoto app = new RemoteGoto();
  28.         app.createPdf(DEST);
  29.         app.createPdf2(SRC);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         // first document
  34.         Document document1 = new Document();
  35.         PdfWriter.getInstance(document1, new FileOutputStream(dest));
  36.         document1.open();
  37.         Anchor anchor = new Anchor("This is a destination");
  38.         anchor.setName("dest");
  39.         document1.add(anchor);
  40.         document1.close();
  41.     }
  42.    
  43.     public void createPdf2(String src) throws IOException, DocumentException {
  44.         // second document (with a link to the first one)
  45.         Document document2 = new Document();
  46.         PdfWriter.getInstance(document2, new FileOutputStream(src));
  47.         document2.open();
  48.         Chunk chunk = new Chunk("Link");
  49.         chunk.setAction(PdfAction.gotoRemotePage("subdir/abc.pdf", "dest", false,  true));
  50.         document2.add(chunk);
  51.         document2.close();
  52.     }
  53. }
RemoteGoToPage.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19999048/how-to-create-hyperlink-from-a-pdf-to-another-pdf-to-a-specified-page-using-itex
  4.  *
  5.  * Creating a link from one PDF to another
  6.  */
  7. package sandbox.annotations;
  8.  
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12.  
  13. import com.itextpdf.text.Chunk;
  14. import com.itextpdf.text.Document;
  15. import com.itextpdf.text.DocumentException;
  16. import com.itextpdf.text.Paragraph;
  17. import com.itextpdf.text.pdf.PdfAction;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. public class RemoteGoToPage {
  21.     public static final String DEST = "results/annotations/subdir/abc2.pdf";
  22.     public static final String SRC = "results/annotations/xyz2.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         RemoteGoToPage app = new RemoteGoToPage();
  28.         app.createPdf(DEST);
  29.         app.createPdf2(SRC);
  30.     }
  31.    
  32.     public void createPdf(String dest) throws IOException, DocumentException {
  33.         // first document
  34.         Document document1 = new Document();
  35.         PdfWriter.getInstance(document1, new FileOutputStream(dest));
  36.         document1.open();
  37.         document1.add(new Paragraph("page 1"));
  38.         document1.newPage();
  39.         document1.add(new Paragraph("page 2"));
  40.         document1.newPage();
  41.         document1.add(new Paragraph("page 3"));
  42.         document1.newPage();
  43.         document1.add(new Paragraph("page 4"));
  44.         document1.newPage();
  45.         document1.add(new Paragraph("page 5"));
  46.         document1.newPage();
  47.         document1.add(new Paragraph("page 6"));
  48.         document1.newPage();
  49.         document1.add(new Paragraph("page 7"));
  50.         document1.close();
  51.     }
  52.    
  53.     public void createPdf2(String src) throws IOException, DocumentException {
  54.         // second document (with a link to the first one)
  55.         Document document2 = new Document();
  56.         PdfWriter.getInstance(document2, new FileOutputStream(src));
  57.         document2.open();
  58.         Chunk chunk = new Chunk("Link");
  59.         chunk.setAction(new PdfAction("subdir/abc2.pdf", 6));
  60.         document2.add(chunk);
  61.         document2.close();
  62.     }
  63. }
AddLinkImages.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/29388313/itext-how-to-associate-actions-with-graphical-object
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.BadElementException;
  8. import com.itextpdf.text.Chunk;
  9. import com.itextpdf.text.Document;
  10. import com.itextpdf.text.DocumentException;
  11. import com.itextpdf.text.Image;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.pdf.PdfAction;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16.  
  17. import java.io.File;
  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 AddLinkImages {
  28.    
  29.     public static final String DEST = "results/annotations/link_images.pdf";
  30.    
  31.    
  32.     public static void main(String[] args) throws IOException, DocumentException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new AddLinkImages().createPdf(DEST);
  36.     }
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         PdfContentByte cb = writer.getDirectContent();
  42.         document.add(new Paragraph("Objects with links"));
  43.         Paragraph p = new Paragraph();
  44.         p.add(createImage("resources/images/info.png", "http://itextpdf.com/"));
  45.         p.add(createImage("resources/images/dog.bmp", "http://pages.itextpdf.com/ebook-stackoverflow-questions.html"));
  46.         p.add(createImage("resources/images/fox.bmp", "http://stackoverflow.com/q/29388313/1622493"));
  47.         p.add(createImage("resources/images/butterfly.wmf", "http://stackoverflow.com/questions/tagged/itext*"));
  48.         document.add(p);
  49.         document.close();
  50.     }
  51.    
  52.     public Chunk createImage(String src, String url) throws BadElementException, IOException {
  53.         Image img = Image.getInstance(src);
  54.         Chunk chunk = new Chunk(img, 0, 0, true);
  55.         chunk.setAction(new PdfAction(url));
  56.         return chunk;
  57.     }
  58. }
ImagesLinksTable.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/32839816/export-hyperlink-to-pdf-file-from-itextsharp-library-in-c-sharp
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.Anchor;
  8. import com.itextpdf.text.Annotation;
  9. import com.itextpdf.text.Chunk;
  10. import com.itextpdf.text.Document;
  11. import com.itextpdf.text.DocumentException;
  12. import com.itextpdf.text.Image;
  13. import com.itextpdf.text.Phrase;
  14. import com.itextpdf.text.pdf.PdfAction;
  15. import com.itextpdf.text.pdf.PdfPTable;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. /**
  24.  * @author Bruno Lowagie (iText Software)
  25.  */
  26. @WrapToTest
  27. public class ImagesLinksTable {
  28.     public static final String DEST = "results/annotations/images_links_table.pdf";
  29.     public static final String IMG = "resources/images/info.png";
  30.    
  31.     public static void main(String[] args) throws IOException, DocumentException {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new ImagesLinksTable().createPdf(DEST);
  35.     }
  36.    
  37.     public void createPdf(String dest) throws IOException, DocumentException {
  38.         Document document = new Document();
  39.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  40.         document.open();
  41.         PdfPTable table = new PdfPTable(3);
  42.         Image img = Image.getInstance(IMG);
  43.         Chunk c = new Chunk(img, 0, 0, true);
  44.         Anchor anchor = new Anchor(c);
  45.         anchor.setReference("http://lowagie.com/");
  46.         table.addCell(anchor);
  47.         table.addCell("A");
  48.         table.addCell("B");
  49.         table.addCell("C");
  50.         Chunk c2 = new Chunk(img, 0, 0, true);
  51.         c2.setAction(new PdfAction("http://itextpdf.com"));
  52.         table.addCell(new Phrase(c2));
  53.         table.addCell("D");
  54.         table.addCell("E");
  55.         table.addCell("F");
  56.         Image img2 = Image.getInstance(IMG);
  57.         Annotation a = new Annotation(0, 0, 0, 0, "http://lowagie.com/bio");
  58.         img2.setAnnotation(a);
  59.         table.addCell(img2);
  60.         document.add(table);
  61.         document.close();
  62.     }
  63. }
RelativeLink.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27063677/use-of-relative-path-for-anchor-method-using-itext-for-pdf-generation
  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.pdf.PdfWriter;
  11.  
  12. import java.io.File;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import sandbox.WrapToTest;
  16.  
  17. @WrapToTest
  18. public class RelativeLink {
  19.     public static final String DEST = "results/annotations/relative_link.pdf";
  20.     public static final String XML = "resources/xml/data.xml";
  21.    
  22.     public static void main(String[] args) throws IOException, DocumentException {
  23.         File file = new File(DEST);
  24.         file.getParentFile().mkdirs();
  25.         new RelativeLink().createPdf(DEST);
  26.     }
  27.    
  28.     public void createPdf(String dest) throws IOException, DocumentException {
  29.         // first document
  30.         Document document = new Document();
  31.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  32.         document.open();
  33.         Chunk chunk = new Chunk("Click me");
  34.         chunk.setAnchor("./" + new File(XML).getName());
  35.         document.add(chunk);
  36.         document.close();
  37.     }
  38. }
FileAttachmentAnnot.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/31006683/itext-clickable-image-should-open-ms-word-attachment
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.Document;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Image;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfAnnotation;
  12. import com.itextpdf.text.pdf.PdfAppearance;
  13. import com.itextpdf.text.pdf.PdfFileSpecification;
  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. /**
  21.  * @author Bruno Lowagie (iText Software)
  22.  */
  23. public class FileAttachmentAnnot {
  24.     public static final String DEST = "results/annotations/file_attachment.pdf";
  25.     public static final String PATH = "resources/misc/test.docx";
  26.     public static final String IMG = "resources/images/info.png";
  27.    
  28.     public static void main(String[] args) throws IOException, DocumentException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new FileAttachmentAnnot().createPdf(DEST);
  32.     }
  33.    
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Document document = new Document();
  36.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  37.         document.open();
  38.         Rectangle rect = new Rectangle(36, 700, 136, 800);
  39.         PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
  40.                 writer, PATH, "test.docx", null);
  41.         PdfAnnotation attachment =
  42.                 PdfAnnotation.createFileAttachment(writer, rect, "Click me" , fs);
  43.         PdfAppearance app = writer.getDirectContent().createAppearance(100, 100);
  44.         Image img = Image.getInstance(IMG);
  45.         img.scaleAbsolute(100, 100);
  46.         img.setAbsolutePosition(0, 0);
  47.         app.addImage(img);
  48.         attachment.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, app);
  49.         writer.addAnnotation(attachment);
  50.         document.close();
  51.     }
  52. }
AddPointerAnnotation.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26752663/adding-maps-at-itext-java
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Image;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfAnnotation;
  13. import com.itextpdf.text.pdf.PdfArray;
  14. import com.itextpdf.text.pdf.PdfBorderDictionary;
  15. import com.itextpdf.text.pdf.PdfName;
  16. import com.itextpdf.text.pdf.PdfWriter;
  17.  
  18. import java.io.File;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class AddPointerAnnotation {
  25.        
  26.     public static final String IMG = "resources/images/map_cic.png";
  27.     public static final String DEST = "results/annotations/map_with_pointer_annotation.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddPointerAnnotation().createPdf(DEST);
  33.     }
  34.     public void createPdf(String dest) throws IOException, DocumentException {
  35.         Image img = Image.getInstance(IMG);
  36.         Document document = new Document(img);
  37.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  38.         document.open();
  39.         img.setAbsolutePosition(0, 0);
  40.         document.add(img);
  41.         Rectangle rect = new Rectangle(220, 350, 475, 595);
  42.         PdfAnnotation annotation = PdfAnnotation.createLine(writer, rect, "Cambridge Innovation Center", 225, 355, 470, 590);
  43.         PdfArray le = new PdfArray();
  44.         le.add(new PdfName("OpenArrow"));
  45.         le.add(new PdfName("None"));
  46.         annotation.setTitle("You are here:");
  47.         annotation.setColor(BaseColor.RED);
  48.         annotation.setFlags(PdfAnnotation.FLAGS_PRINT);
  49.         annotation.setBorderStyle(
  50.             new PdfBorderDictionary(5, PdfBorderDictionary.STYLE_SOLID));
  51.         annotation.put(new PdfName("LE"), le);
  52.         annotation.put(new PdfName("IT"), new PdfName("LineArrow"));
  53.         writer.addAnnotation(annotation);
  54.         document.close();
  55.     }
  56. }
AddStamp.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to
  3.  * http://stackoverflow.com/questions/29229629/how-to-add-a-printable-or-non-printable-bitmap-stamp-to-a-pdf
  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.PdfAppearance;
  12. import com.itextpdf.text.pdf.PdfContentByte;
  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 AddStamp {
  24.  
  25.     public static final String SRC = "resources/pdfs/hello.pdf";
  26.     public static final String DEST = "results/annotations/stamp_annotation.pdf";
  27.     public static final String IMG = "resources/images/itext.png";
  28.  
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new AddStamp().manipulatePdf(SRC, DEST);
  33.     }
  34.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         Image img = Image.getInstance(IMG);
  38.         float w = img.getScaledWidth();
  39.         float h = img.getScaledHeight();
  40.         Rectangle location = new Rectangle(36, 770 - h, 36 + w, 770);
  41.         PdfAnnotation stamp = PdfAnnotation.createStamp(
  42.                 stamper.getWriter(), location, null, "ITEXT");                    
  43.         img.setAbsolutePosition(0, 0);
  44.         PdfContentByte cb = stamper.getOverContent(1);
  45.         PdfAppearance app = cb.createAppearance(w, h);
  46.         app.addImage(img);
  47.         stamp.setAppearance(PdfName.N, app);
  48.         stamp.setFlags(PdfAnnotation.FLAGS_PRINT);
  49.         stamper.addAnnotation(stamp, 1);
  50.         stamper.close();
  51.         reader.close();
  52.     }
  53. }
AddRotatedAnnotation.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/29303345/java-itext-rotate-a-link-rectangle
  4.  */
  5. package sandbox.annotations;
  6.  
  7. import com.itextpdf.text.BaseColor;
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.PdfAction;
  11. import com.itextpdf.text.pdf.PdfAnnotation;
  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.  
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class AddRotatedAnnotation {
  23.  
  24.     public static final String SRC = "resources/pdfs/hello.pdf";
  25.     public static final String DEST = "results/annotations/rotated_annotation.pdf";
  26.  
  27.     public static void main(String[] args) throws IOException, DocumentException {
  28.         File file = new File(DEST);
  29.         file.getParentFile().mkdirs();
  30.         new AddRotatedAnnotation().manipulatePdf(SRC, DEST);
  31.     }
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         PdfAction action = new PdfAction("http://pages.itextpdf.com/ebook-stackoverflow-questions.html");
  36.         Rectangle linkLocation1 = new Rectangle(30, 770, 120, 800);
  37.         PdfAnnotation link1 = PdfAnnotation.createLink(stamper.getWriter(),
  38.                 linkLocation1, PdfAnnotation.HIGHLIGHT_INVERT, action);
  39.         link1.setColor(BaseColor.RED);
  40.         stamper.addAnnotation(link1, 1);
  41.         Rectangle linkLocation2 = new Rectangle(30, 670, 60, 760);
  42.         PdfAnnotation link2 = PdfAnnotation.createLink(stamper.getWriter(),
  43.                 linkLocation2, PdfAnnotation.HIGHLIGHT_INVERT, action);
  44.         link2.setColor(BaseColor.GREEN);
  45.         stamper.addAnnotation(link2, 1);
  46.         Rectangle linkLocation3 = new Rectangle(150, 770, 240, 800);
  47.         PdfAnnotation stamp1 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation3, "Landscape", "Confidential");
  48.         stamper.addAnnotation(stamp1, 1);
  49.         Rectangle linkLocation4 = new Rectangle(150, 670, 240, 760);
  50.         PdfAnnotation stamp2 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation4, "Portrait", "Confidential");
  51.         stamp2.setRotate(90);
  52.         stamper.addAnnotation(stamp2, 1);
  53.         Rectangle linkLocation5 = new Rectangle(250, 670, 340, 760);
  54.         PdfAnnotation stamp3 = PdfAnnotation.createStamp(stamper.getWriter(), linkLocation5, "Portrait", "Confidential");
  55.         stamp3.setRotate(45);
  56.         stamper.addAnnotation(stamp3, 1);
  57.         stamper.close();
  58.         reader.close();
  59.     }
  60. }
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