Creating Table of Contents using events

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

25th October 2015
admin-marketing

Switch code for this example

CreateTOCinColumn.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29092738/itext-chapter-title-and-columntext
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.Rectangle;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PdfAction;
  14. import com.itextpdf.text.pdf.PdfDestination;
  15. import com.itextpdf.text.pdf.PdfOutline;
  16. import com.itextpdf.text.pdf.PdfPTable;
  17. import com.itextpdf.text.pdf.PdfPageEventHelper;
  18. import com.itextpdf.text.pdf.PdfWriter;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import sandbox.WrapToTest;
  26.  
  27. @WrapToTest
  28. public class CreateTOCinColumn {
  29.    
  30.     public static final String DEST = "results/events/columns_with_toc.pdf";
  31.     public static final Rectangle[] COLUMNS = {
  32.         new Rectangle(36, 36, 192, 806),
  33.         new Rectangle(204, 36, 348, 806),
  34.         new Rectangle(360, 36, 504, 806)
  35.     };
  36.    
  37.     public class TOCEntry {
  38.         protected PdfAction action;
  39.         protected String title;
  40.     }
  41.    
  42.     public class TOCCreation extends PdfPageEventHelper {
  43.  
  44.         protected PdfOutline root;
  45.         protected List toc = new ArrayList();
  46.        
  47.         public TOCCreation() {
  48.         }
  49.        
  50.         public void setRoot(PdfOutline root) {
  51.             this.root = root;
  52.         }
  53.        
  54.         public List getToc() {
  55.             return toc;
  56.         }
  57.        
  58.         @Override
  59.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  60.             PdfDestination dest = new PdfDestination(PdfDestination.XYZ, rect.getLeft(), rect.getTop(), 0);
  61.             new PdfOutline(root, dest, text);
  62.             TOCEntry entry = new TOCEntry();
  63.             entry.action = PdfAction.gotoLocalPage(writer.getPageNumber(), dest, writer);
  64.             entry.title = text;
  65.             toc.add(entry);
  66.         }
  67.     }
  68.    
  69.     public static void main(String[] args) throws IOException, DocumentException {
  70.         File file = new File(DEST);
  71.         file.getParentFile().mkdirs();
  72.         new CreateTOCinColumn().createPdf(DEST);
  73.     }
  74.     public void createPdf(String dest) throws IOException, DocumentException {
  75.         Document document = new Document();
  76.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  77.         TOCCreation event = new TOCCreation();
  78.         writer.setPageEvent(event);
  79.         document.open();
  80.         event.setRoot(writer.getRootOutline());
  81.         ColumnText ct = new ColumnText(writer.getDirectContent());
  82.         int start;
  83.         int end;
  84.         for (int i = 0; i
CreateTOC.java
  1. /*
  2.  * This example is written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/39037594
  4.  */
  5. package sandbox.events;
  6.  
  7. import com.itextpdf.text.Chunk;
  8. import com.itextpdf.text.Document;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Font;
  11. import com.itextpdf.text.Font.FontFamily;
  12. import com.itextpdf.text.Paragraph;
  13. import com.itextpdf.text.Rectangle;
  14. import com.itextpdf.text.pdf.PdfPageEventHelper;
  15. import com.itextpdf.text.pdf.PdfWriter;
  16. import com.itextpdf.text.pdf.draw.DottedLineSeparator;
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import java.util.AbstractMap.SimpleEntry;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import sandbox.WrapToTest;
  24.  
  25. /**
  26.  *
  27.  * @author Bruno Lowagie (iText Software)
  28.  */
  29. @WrapToTest
  30. public class CreateTOC {
  31.    
  32.     public static final String DEST = "results/events/create_toc.pdf";
  33.     public final Font titleFont = new Font(FontFamily.HELVETICA, 16);
  34.    
  35.     public class TOCEvent extends PdfPageEventHelper {
  36.  
  37.         protected List toc = new ArrayList();
  38.        
  39.         @Override
  40.         public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
  41.             toc.add(new SimpleEntry(text, writer.getPageNumber()));
  42.         }
  43.        
  44.         public List getTOC() {
  45.             return toc;
  46.         }
  47.     }
  48.    
  49.    
  50.     public static void main(String[] args) throws IOException, DocumentException {
  51.         File file = new File(DEST);
  52.         file.getParentFile().mkdirs();
  53.         new CreateTOC().createPdf(DEST);
  54.     }
  55.    
  56.     public void createPdf(String dest) throws IOException, DocumentException {
  57.         Document document = new Document();
  58.         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
  59.         TOCEvent event = new TOCEvent();
  60.         writer.setPageEvent(event);
  61.         document.open();
  62.         for (int i = 0; i < 10; i++) {
  63.             String title = "This is title " + i;
  64.             Chunk c = new Chunk(title, titleFont);
  65.             c.setGenericTag(title);
  66.             document.add(new Paragraph(c));
  67.             for (int j = 0; j < 50; j++) {
  68.                 document.add(new Paragraph("Line " + j + " of title " + i));
  69.             }
  70.         }
  71.         document.newPage();
  72.         document.add(new Paragraph("Table of Contents", titleFont));
  73.         Chunk dottedLine = new Chunk(new DottedLineSeparator());
  74.         List entries = event.getTOC();
  75.         Paragraph p;
  76.         for (SimpleEntry entry : entries) {
  77.             p = new Paragraph(entry.getKey());
  78.             p.add(dottedLine);
  79.             p.add(String.valueOf(entry.getValue()));
  80.             document.add(p);
  81.         }
  82.         document.close();
  83.     }
  84. }
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