Class TocGenerator

java.lang.Object
org.docx4j.toc.TocGenerator

public class TocGenerator
extends java.lang.Object
This class creates and populates a table of contents. If you want page numbers, getting this right (without opening the docx in Word to do so), is a challenge. This class supports generating the page numbers using export-fo (or the no-longer-available Converter). If you are using docx4j-export-documents4j-local|remote, you can use that for ToC generation.
Author:
jharrop
  • Constructor Summary

    Constructors
    Constructor Description
    TocGenerator​(WordprocessingMLPackage wordMLPackage)  
  • Method Summary

    Modifier and Type Method Description
    SdtBlock generateToc​(boolean skipPageNumbering)
    Generate Table of Contents using default TOC instruction, adding it at the beginning of the document To alter the ToC heading, use Toc.setTocHeadingText
    SdtBlock generateToc​(int index, java.lang.String instruction, boolean skipPageNumbering)
    Generate Table of Contents using provided TOC instruction, adding at the given index in the body of document, using dot leader for page numbering (if any).
    SdtBlock generateToc​(int index, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering)
    Generate Table of Contents using provided TOC instruction, and specified leader (eg dots) before page number, adding at the given index in the body of document It is an error if a ToC content control is already present; delete it or use updateToc.
    static SdtBlock generateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering)
    Deprecated.
    static SdtBlock generateToc​(WordprocessingMLPackage wordMLPackage, int index, java.lang.String instruction, boolean skipPageNumbering)
    Deprecated.
    protected SdtBlock generateToc​(SdtBlock sdt, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering)
    Generate Table of Contents in the given sdt with generated heading, and given TOC instruction.
    protected SdtBlock generateToc​(SdtBlock sdt, java.util.List<P> tocHeadingP, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering)
    Generate Table of Contents in the given sdt with provided tocHeadingP, and given TOC instruction.
    WordprocessingMLPackage getWordMLPackage()  
    void pageNumbersViaXSLT​(boolean useXSLT)
    Whether to use XSLT to generate page numbers in the TOC via FOP.
    void setStartingIdForNewBookmarks​(java.util.concurrent.atomic.AtomicInteger bookmarkId)
    Provide a way to set the starting bookmark ID number.
    SdtBlock updateToc()
    Update existing TOC in the document with TOC generated by generateToc() method, reusing the existing ToC heading paragraph.
    SdtBlock updateToc​(boolean skipPageNumbering)
    Update existing TOC in the document with TOC generated by generateToc() method, reusing the existing ToC heading paragraph.
    SdtBlock updateToc​(boolean skipPageNumbering, boolean reuseExistingToCHeadingP)
    Update existing TOC in the document with TOC generated by generateToc() method.
    SdtBlock updateToc​(boolean skipPageNumbering, boolean reuseExistingToCHeadingP, STTabTlc leader)
    Update existing TOC in the document with TOC generated by generateToc() method, using specified tab leader.
    static SdtBlock updateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering)
    Deprecated.
    SdtBlock updateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering, boolean reuseExistingToCHeadingP)
    Deprecated.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • getWordMLPackage

      public WordprocessingMLPackage getWordMLPackage()
    • generateToc

      @Deprecated public static SdtBlock generateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering) throws TocException
      Deprecated.
      Generate Table of Contents using default TOC instruction, adding it at the beginning of the document To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      body - of document
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      Returns:
      SdtBlock control
      Throws:
      TocException
    • generateToc

      @Deprecated public static SdtBlock generateToc​(WordprocessingMLPackage wordMLPackage, int index, java.lang.String instruction, boolean skipPageNumbering) throws TocException
      Deprecated.
      Generate Table of Contents using provided TOC instruction, adding at the given index in the body of document It is an error if a ToC content control is already present; delete it or use updateToc. To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      body -
      index -
      instruction -
      Returns:
      SdtBlock control
      Throws:
      TocException
    • generateToc

      public SdtBlock generateToc​(boolean skipPageNumbering) throws TocException
      Generate Table of Contents using default TOC instruction, adding it at the beginning of the document To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      body - of document
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      Returns:
      SdtBlock control
      Throws:
      TocException
    • generateToc

      public SdtBlock generateToc​(int index, java.lang.String instruction, boolean skipPageNumbering) throws TocException
      Generate Table of Contents using provided TOC instruction, adding at the given index in the body of document, using dot leader for page numbering (if any). It is an error if a ToC content control is already present; delete it or use updateToc. To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      index -
      instruction -
      skipPageNumbering -
      Returns:
      SdtBlock control
      Throws:
      TocException
    • generateToc

      public SdtBlock generateToc​(int index, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering) throws TocException
      Generate Table of Contents using provided TOC instruction, and specified leader (eg dots) before page number, adding at the given index in the body of document It is an error if a ToC content control is already present; delete it or use updateToc. To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      index -
      instruction -
      leader -
      skipPageNumbering -
      Returns:
      SdtBlock control
      Throws:
      TocException
      Since:
      3.3.2
    • generateToc

      protected SdtBlock generateToc​(SdtBlock sdt, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering) throws TocException
      Generate Table of Contents in the given sdt with generated heading, and given TOC instruction. See http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/TOC.html for TOC instruction. To alter the ToC heading, use Toc.setTocHeadingText
      Parameters:
      body -
      sdt - - assumed already attached to the document
      instruction -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      Returns:
      SdtBlock control
      Throws:
      TocException
    • generateToc

      protected SdtBlock generateToc​(SdtBlock sdt, java.util.List<P> tocHeadingP, java.lang.String instruction, STTabTlc leader, boolean skipPageNumbering) throws TocException
      Generate Table of Contents in the given sdt with provided tocHeadingP, and given TOC instruction. See http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/TOC.html for TOC instruction.
      Parameters:
      body -
      sdt -
      tocHeadingP - use this P as the ToC heading
      instruction -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      Returns:
      SdtBlock control
      Throws:
      TocException
    • setStartingIdForNewBookmarks

      public void setStartingIdForNewBookmarks​(java.util.concurrent.atomic.AtomicInteger bookmarkId)
      Provide a way to set the starting bookmark ID number. For efficiency, user code needs to pass this value through. If it isn't, the value will be calculated (less efficient).
      Parameters:
      bookmarkId -
      Since:
      3.3.0
    • updateToc

      public SdtBlock updateToc​(boolean skipPageNumbering) throws TocException
      Update existing TOC in the document with TOC generated by generateToc() method, reusing the existing ToC heading paragraph.
      Parameters:
      body -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      Returns:
      SdtBlock control, or null if no TOC was found
      Throws:
      TocException
    • updateToc

      public SdtBlock updateToc() throws TocException
      Update existing TOC in the document with TOC generated by generateToc() method, reusing the existing ToC heading paragraph. Page numbers are updated.
      Returns:
      SdtBlock control, or null if no TOC was found
      Throws:
      TocException
      Since:
      6.1.0
    • updateToc

      @Deprecated public static SdtBlock updateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering) throws TocException
      Deprecated.
      Throws:
      TocException
    • updateToc

      public SdtBlock updateToc​(boolean skipPageNumbering, boolean reuseExistingToCHeadingP) throws TocException
      Update existing TOC in the document with TOC generated by generateToc() method. Attempt to identify/re-use existing dot leader.
      Parameters:
      body -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      reuseExistingToCHeadingP -
      Returns:
      SdtBlock control, or null if no TOC was found
      Throws:
      TocException
    • updateToc

      public SdtBlock updateToc​(boolean skipPageNumbering, boolean reuseExistingToCHeadingP, STTabTlc leader) throws TocException
      Update existing TOC in the document with TOC generated by generateToc() method, using specified tab leader.
      Parameters:
      body -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      reuseExistingToCHeadingP -
      leader -
      Returns:
      SdtBlock control, or null if no TOC was found
      Throws:
      TocException
      Since:
      3.3.2
    • updateToc

      @Deprecated public SdtBlock updateToc​(WordprocessingMLPackage wordMLPackage, boolean skipPageNumbering, boolean reuseExistingToCHeadingP) throws TocException
      Deprecated.
      Update existing TOC in the document with TOC generated by generateToc() method.
      Parameters:
      body -
      skipPageNumbering - don't generate page numbers (useful for HTML output, or speed, or as a fallback in case of issues)
      reuseExistingToCHeadingP -
      Returns:
      SdtBlock control, or null if no TOC was founds
      Throws:
      TocException
    • pageNumbersViaXSLT

      public void pageNumbersViaXSLT​(boolean useXSLT)
      Whether to use XSLT to generate page numbers in the TOC via FOP. Of the three methods available, XSLT (the default) is 2nd most featureful, but slowest. From v3.3.0, DocumentServices is recommended as quickest and most accurate, and will be used unless docx4j-export-fo is present.
      Parameters:
      useXSLT -