Adding annotations to a table cell

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

31st May 2016
admin-marketing

Switch code for this example

LinkInTableCell.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:
  10.  * http://stackoverflow.com/questions/35288194
  11.  */
  12. package com.itextpdf.samples.sandbox.tables;
  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.kernel.pdf.annot.PdfAnnotation;
  18. import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation;
  19. import com.itextpdf.layout.Document;
  20. import com.itextpdf.layout.element.Cell;
  21. import com.itextpdf.layout.element.Link;
  22. import com.itextpdf.layout.element.Paragraph;
  23. import com.itextpdf.layout.element.Table;
  24. import com.itextpdf.layout.renderer.CellRenderer;
  25. import com.itextpdf.layout.renderer.DrawContext;
  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.  
  34. @Category(SampleTest.class)
  35. public class LinkInTableCell extends GenericTest {
  36.     public static final String DEST = "./target/test/resources/sandbox/tables/link_in_table_cell.pdf";
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         File file = new File(DEST);
  40.         file.getParentFile().mkdirs();
  41.         new LinkInTableCell().manipulatePdf(DEST);
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  47.         Document doc = new Document(pdfDoc);
  48.         Table table = new Table(1);
  49.         // Part of the content is a link:
  50.         Paragraph phrase = new Paragraph();
  51.         phrase.add("The founders of iText are nominated for a ");
  52.         Link chunk = new Link("European Business Award!",
  53.                 PdfAction.createURI("http://itextpdf.com/blog/european-business-award-kick-ceremony"));
  54.         phrase.add(chunk);
  55.         table.addCell(phrase);
  56.         // The complete cell is a link:
  57.         Cell cell = new Cell().add("Help us win a European Business Award!");
  58.         cell.setNextRenderer(new LinkInCellRenderer(cell, "http://itextpdf.com/blog/help-us-win-european-business-award"));
  59.         table.addCell(cell);
  60.         doc.add(table);
  61.         doc.close();
  62.     }
  63.  
  64.  
  65.     class LinkInCellRenderer extends CellRenderer {
  66.         protected String url;
  67.  
  68.         public LinkInCellRenderer(Cell modelElement, String url) {
  69.             super(modelElement);
  70.             this.url = url;
  71.         }
  72.  
  73.         @Override
  74.         public void draw(DrawContext drawContext) {
  75.             super.draw(drawContext);
  76.             PdfLinkAnnotation linkAnnotation = new PdfLinkAnnotation(getOccupiedAreaBBox());
  77.             linkAnnotation.setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT);
  78.             linkAnnotation.setAction(PdfAction.createURI(url));
  79.             drawContext.getDocument().getLastPage().addAnnotation(linkAnnotation);
  80.         }
  81.  
  82.     }
  83. }
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