Tutorial: iText by Example

RTF Table of Contents support

Creating a Table of Contents:
Creating a Table of Contents with the RtfTableOfContents is very simple, but comes with three important caveats:
  • The RtfWriter2 does not render the RTF document, thus it can't compute the page numbers for the table of contents.
  • The RtfWriter2 only performs a single pass, thus the entries into the table of contents cannot be generated, even without page numbers.
  • The RtfWriter2 cannot force the renderer to automatically update the table of contents on loading. The user must do this himself.

To add a table of contents, simply create a Paragraph and add a RtfTableOfContetns to it. The constructor takes one parameter and that is the text that will be initially displayed before the user updates the table of contents.
// Create a Paragraph and add the table of contents to it
Paragraph par = new Paragraph();
par.add(new RtfTableOfContents("Right-click here and select \"Update\" " +
        "to see the table of contents."));
document.add(par);
    	  
Example: java com.lowagie.examples.rtf.features.toc.TableOfContents
Generates a document with a table of contents: see TableOfContents.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page
Adding Entries to the Table of Contents:
The next step is adding entries to the table of contents. There is a RtfTOCEntry class, but its use is not recommended. The recommended method is using paragraph stylesheets to define the different headers. By default header styles for the levels 1 to 3 are provided, but you can add more up to level 6. These header styles have a special side effect and that is that they automatically become part of the table of contents, when it is updated.
// Create a level 1 heading
document.add(new Paragraph("Heading " + i, RtfParagraphStyle.STYLE_HEADING_1));
for(int j = 1; j <= 3; j++) {
    // Create a level 2 heading
    document.add(new Paragraph("Heading " + i + "." + j, RtfParagraphStyle.STYLE_HEADING_2));
    	  
Example: java com.lowagie.examples.rtf.features.toc.TableOfContents
Generates a document with a table of contents: see TableOfContents.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page
Styling the Table of Contents:
Finally you can use the paragraph stylesheets to style the resulting table of contents. To do this you must create an RtfParagraphStyle for each heading level that you will use in the document. These styles must be named toc N, where N is the heading level that the style will apply to.
// Create paragraph stylesheets for each heading level. They must be named
// "toc N" for each heading level you are using
RtfParagraphStyle tocLevel1Style = new RtfParagraphStyle("toc 1",
        "Times New Roman", 11, Font.NORMAL, Color.BLACK);
RtfParagraphStyle tocLevel2Style = new RtfParagraphStyle("toc 2",
        "Times New Roman", 10, Font.NORMAL, Color.BLACK);
tocLevel2Style.setIndentLeft(10);

// Register the paragraph stylesheets with the RtfWriter2
rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel1Style);
rtfWriter2.getDocumentSettings().registerParagraphStyle(tocLevel2Style);
    	  
Example: java com.lowagie.examples.rtf.features.toc.TableOfContents
Generates a document with a table of contents: see TableOfContents.rtf
Extra jars needed in your CLASSPATH: itext-rtf.jar
Go to top of the page

iText, the #1 Java-PDF library




Amazon books:
amazon.co.uk-link