0

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)

1 Answer 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:

6
  • The first approach sounds fitting my project, but I dont understand what you mean by "FOP on your classpath". Could you elaborate?
    – sav
    Commented Mar 11, 2022 at 8:52
  • Put the FOP jar and its dependencies on your classpath (a basic java concept), either manually (they are in optional/export-fo in docx4java.org/docx4j/archive/docx4j-8.3.2/… ) or using Maven (add github.com/plutext/docx4j/blob/master/… ). Commented Mar 11, 2022 at 18:59
  • Ok i missunderstood, thanks. I do have docx4j version 8.0.0 in my dependencies, also export-fo. I do also have documents4j dependencies: importxhtml, transformer-msoffice-word, api and local in my dependencies, version 1.1.5 So both approaches seem possible, which one would you recommend?
    – sav
    Commented Mar 17, 2022 at 9:51
  • documents4j will yield more accurate page numbers, but requires Word. It is more accurate because Word does the page layout, as opposed to XSL FO. Commented Mar 17, 2022 at 19:21
  • Hey, sorry for bothering again Jason, I have updated docx4j to 11.2.5, now I still get the error (the system is running on unix system later on, so using word is not an option sadly), but now I can see a cause for the TocException. I have edited the question to include the exception
    – sav
    Commented Mar 18, 2022 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.