'Dovx4J: Enabling page numbers in ToC results in TocException

I am using Docx4j in an internship project and when I try to generate a ToC with page numbers it does not work.

Executing the code with

if (template.isGenerateToc()) {
            Toc.setTocHeadingText("Inhaltsverzeichnis");

            TocGenerator tocGenerator = new TocGenerator(wordPackage);
            tocGenerator.generateToc(2, " TOC \\o \"1-3\" \\h \\z \\u ", true);
}

Works perfectly fine, but results in no page numbers in the .docx (when exporting to PDF via the Gotenberg API (https://gotenberg.dev), the page numbers are existant and correct).

When I try to pass "false" for the third parameter, the application crashes with the following exception/stacktrace:

org.docx4j.toc.TocException: Exception exporting package
   at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:871)
   at org.docx4j.toc.TocGenerator.getPageNumbersMap(TocGenerator.java:694)
   at org.docx4j.toc.TocGenerator.populateToc(TocGenerator.java:441)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:306)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:234)
   at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:194)

If usefull: This is my TocHeading:

private static String XML_TOCHeading_BasedOn_Nothing = "<w:style w:styleId=\"TOCHeading\" w:type=\"paragraph\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            + "<w:name w:val=\"TOC Heading\"/>"
            // + "<w:basedOn w:val=\"Heading1\"/>"  // would be ok if provided already present, since
            + "<w:next w:val=\"Normal\"/>"
            + "<w:uiPriority w:val=\"39\"/>"
            + "<w:semiHidden/>"
            + "<w:unhideWhenUsed/>"
            + "<w:qFormat/>"
            + "<w:pPr>"
            + "<w:keepNext/>"
            + "<w:keepLines/>"
            + "<w:spacing w:after=\"150\" w:before=\"480\"/>"
            + "<w:outlineLvl w:val=\"9\"/>"
            + "</w:pPr>"
            + "<w:rPr>"
            + "<w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" w:cs=\"Arial\"/>"
            + "<w:b/>"
            + "<w:bCs/>"
            + "<w:sz w:val=\"36\"/>"
            + "<w:szCs w:val=\"36\"/>"
            + "</w:rPr>"
            + "</w:style>";

Any ideas on how to fix this exception and generate page numbers in my toc?

EDIT:

I updated the dependencies and now I am still getting the TocException but now with another cause:

Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:106)
    at org.docx4j.Docx4J.toFO(Docx4J.java:734)
    at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:843)
    ... 97 more
Caused by: java.lang.NullPointerException
    at org.docx4j.convert.out.common.preprocess.Containerization.borderChanged(Containerization.java:390)
    at org.docx4j.convert.out.common.preprocess.Containerization.bordersChanged(Containerization.java:377)
    at org.docx4j.convert.out.common.preprocess.Containerization.groupBodyContent(Containerization.java:204)
    at org.docx4j.convert.out.common.preprocess.Containerization.groupAdjacentBorders(Containerization.java:129)
    at org.docx4j.convert.out.common.preprocess.Containerization.process(Containerization.java:120)
    at org.docx4j.convert.out.common.Preprocess.process(Preprocess.java:165)
    at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:51)
    at org.docx4j.convert.out.common.AbstractWmlExporter.preprocess(AbstractWmlExporter.java:32)
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:63)


Solution 1:[1]

Not sure which version of docx4j you are using, but generally, to update page numbers, you need FOP on your classpath.

We broke this in 8.3.3; it is fixed at https://github.com/plutext/docx4j/commit/50cfa5cabe08ab133f28c54b59f73d082d3e80a4#diff-29cfd63b63bfe585aa3468870ca54f1afb6e2246342c396155801b0d0aa9ae43L763

So you could try 8.3.2 with FOP on your classpath.

Alternatively, you could try the approach described at https://www.docx4java.org/blog/2020/03/documents4j-for-toc-update/

See further:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JasonPlutext