Creating Table of Contents using events

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

31st May 2016
admin-marketing

Switch code for this example

CreateTOCinColumn.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 by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/29092738/itext-chapter-title-and-columntext
  11.  */
  12.  
  13. package com.itextpdf.samples.sandbox.events;
  14.  
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.*;
  17. import com.itextpdf.kernel.pdf.navigation.PdfDestination;
  18. import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
  19. import com.itextpdf.layout.ColumnDocumentRenderer;
  20. import com.itextpdf.layout.Document;
  21. import com.itextpdf.layout.element.*;
  22. import com.itextpdf.layout.renderer.DrawContext;
  23. import com.itextpdf.layout.renderer.TextRenderer;
  24. import com.itextpdf.samples.GenericTest;
  25. import com.itextpdf.test.annotations.type.SampleTest;
  26.  
  27. import org.junit.experimental.categories.Category;
  28.  
  29. import java.util.ArrayList;
  30. import java.util.List;
  31.  
  32. @Category(SampleTest.class)
  33. public class CreateTOCinColumn extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/events/create_toc_in_column.pdf";
  35.  
  36.     protected List list = new ArrayList();
  37.  
  38.     public static void main(String[] args) throws Exception {
  39.         new CreateTOCinColumn().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
  45.         Document doc = new Document(pdfDoc);
  46.         Rectangle[] columns = {
  47.                 new Rectangle(36, 36, 173, 770),
  48.                 new Rectangle(213, 36, 173, 770),
  49.                 new Rectangle(389, 36, 173, 770)
  50.         };
  51.         doc.setRenderer(new ColumnDocumentRenderer(doc, columns));
  52.         PdfOutline root = pdfDoc.getOutlines(false);
  53.         int start;
  54.         int end;
  55.         for (int i = 0; i
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