如何生成目录

How to generate TOC

我的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>

我尝试下面的代码但抛出 Docx4JException,但是 github 演示只是 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[=12=](WordTest.java:66)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater(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(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

这已在 https://github.com/plutext/docx4j/commit/50cfa5cabe08ab133f28c54b59f73d082d3e80a4 中修复,将在 v11.4.6

在此期间,您可以尝试更早的版本,例如 8.3.2 或 11.3.2。