Links in tables

teaser

31st May 2016
iText PDF

Switch code for this example

LinkInPositionedTable.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 in answer to:
  10.  * http://stackoverflow.com/questions/33633363/itextpdf-cannot-use-writeselectedrows-on-a-table-where-an-anchor-has-been-in
  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.layout.Document;
  18. import com.itextpdf.layout.property.Property;
  19. import com.itextpdf.layout.element.AreaBreak;
  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.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import java.io.File;
  28.  
  29. import org.junit.experimental.categories.Category;
  30.  
  31. @Category(SampleTest.class)
  32. public class LinkInPositionedTable extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/tables/link_in_positioned_table.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new LinkInPositionedTable().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
  44.         Document doc = new Document(pdfDoc);
  45.  
  46.         Table table = new Table(1);
  47.         table.setWidth(500);
  48.         Cell cell = new Cell();
  49.         Paragraph p = new Paragraph();
  50.         Link link = new Link("link to top of next page", PdfAction.createGoTo("top"));
  51.         p.add(link);
  52.         cell.add(p);
  53.         table.addCell(cell);
  54.         doc.add(table);
  55.         doc.add(new AreaBreak());
  56.  
  57.         Paragraph target = new Paragraph("top");
  58.         target.setProperty(Property.DESTINATION, "top");
  59.         doc.add(target);
  60.  
  61.         doc.close();
  62.     }
  63. }
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