Adding annotations to a table cell

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

9th February 2016
admin-marketing

Switch code for this example

LinkInTableCell.java
  1. /*
  2.  * This example was written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/35288194
  4.  */
  5. package sandbox.tables;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.PdfAction;
  13. import com.itextpdf.text.pdf.PdfAnnotation;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfPCell;
  16. import com.itextpdf.text.pdf.PdfPCellEvent;
  17. import com.itextpdf.text.pdf.PdfPTable;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import sandbox.WrapToTest;
  23.  
  24. /**
  25.  * @author Bruno Lowagie (iText Software)
  26.  */
  27. @WrapToTest
  28. public class LinkInTableCell {
  29.     public static final String DEST = "results/tables/link_in_table_cell.pdf";
  30.  
  31.     class LinkInCell implements PdfPCellEvent {
  32.         protected String url;
  33.         public LinkInCell(String url) {
  34.             this.url = url;
  35.         }
  36.         public void cellLayout(PdfPCell cell, Rectangle position,
  37.             PdfContentByte[] canvases) {
  38.             PdfWriter writer = canvases[0].getPdfWriter();
  39.             PdfAction action = new PdfAction(url);
  40.             PdfAnnotation link = PdfAnnotation.createLink(
  41.                 writer, position, PdfAnnotation.HIGHLIGHT_INVERT, action);
  42.             writer.addAnnotation(link);
  43.         }
  44.     }
  45.    
  46.     public void createPdf(String dest) throws IOException, DocumentException {
  47.         Document document = new Document();
  48.         PdfWriter.getInstance(document, new FileOutputStream(dest));
  49.         document.open();
  50.         PdfPTable table = new PdfPTable(1);
  51.         // Part of the content is a link:
  52.         Phrase phrase = new Phrase();
  53.         phrase.add("The founders of iText are nominated for a ");
  54.         Chunk chunk = new Chunk("European Business Award!");
  55.         chunk.setAnchor("http://itextpdf.com/blog/european-business-award-kick-ceremony");
  56.         phrase.add(chunk);
  57.         table.addCell(phrase);
  58.         // The complete cell is a link:
  59.         PdfPCell cell = new PdfPCell(new Phrase("Help us win a European Business Award!"));
  60.         cell.setCellEvent(new LinkInCell("http://itextpdf.com/blog/help-us-win-european-business-award"));
  61.         table.addCell(cell);
  62.         document.add(table);
  63.         document.close();
  64.     }
  65.    
  66.     public static void main(String[] args) throws IOException, DocumentException {
  67.         File file = new File(DEST);
  68.         file.getParentFile().mkdirs();
  69.         new LinkInTableCell().createPdf(DEST);
  70.     }
  71. }
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