Search code examples
docx4j

How to generate TOC


My Version:jkd11

     <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-JAXB-MOXy</artifactId>
            <version>11.4.5</version>
        </dependency>
        <dependency>
            <groupId>org.docx4j</groupId>
            <artifactId>docx4j-export-fo</artifactId>
            <version>11.4.5</version>
        </dependency>

I try below code but throw Docx4JException,but the github demo just that https://github.com/plutext/docx4j/blob/docx4j-parent-11.1.0/docx4j-samples-docx4j/src/main/java/org/docx4j/samples/TocAdd.java

TocGenerator tocGenerator = new TocGenerator(word);
tocGenerator.generateToc(0,"TOC \\o \"1-3\" \\h \\z \\u ",false);
org.docx4j.toc.TocException: Exception exporting package
    at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:792)
    at org.docx4j.toc.TocGenerator.getPageNumbersMap(TocGenerator.java:651)
    at org.docx4j.toc.TocGenerator.populateToc(TocGenerator.java:398)
    at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:262)
    at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:230)
    at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:190)
    at com.erayt.studio.business.word.BusWordCreate.createWord(BusWordCreate.java:106)
    at com.erayt.studio.test.WordTest.lambda$test$0(WordTest.java:66)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:108)
    at org.docx4j.Docx4J.toFO(Docx4J.java:710)
    at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:764)
    ... 14 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: You must invoke FORendererApacheFOP.getFOUserAgent
    at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:124)
    at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
    at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:83)
    ... 16 more

Solution

  • This was fixed at https://github.com/plutext/docx4j/commit/50cfa5cabe08ab133f28c54b59f73d082d3e80a4 and will be in v11.4.6

    In the meantime you could try an earlier release such as 8.3.2 or 11.3.2.