无法使用docx4j更新文档目录中的页码

问题描述 投票:0回答:1

虽然生成目录,但是我无法更新页码。它将异常显示为“遇到损坏的书签;未配置为修复”。我使用以下代码更新目录。.

[TocGenerator tocGenerator =新的TocGenerator(wordMLPackage);

tocGenerator.generateToc(0,“ TOC \ o \” 1-3 \“ \ h \ z \ u”,false);

tocGenerator.updateToc(false);

java maven bookmarks docx4j tableofcontents
1个回答
0
投票

消息从这里来:

/**
 * Calculate page numbers
 * 
 * @return
 * @throws TocException
 */
private Map<String, Integer> getPageNumbersMap() throws TocException {

    // @since 6.1, check bookmarks are ok first
    // what to do if not ok?
    // - default behaviour is to fail
    // - but can be configured to remediate:
    boolean remediate = Docx4jProperties.getProperty("docx4j.toc.BookmarksIntegrity.remediate", false);

    // 
    BookmarksIntegrity bm = new BookmarksIntegrity();
    StringWriter sw = new StringWriter();
    bm.setWriter(sw);
    BookmarksStatus result = null;
    try {
        // Checks are performed on all bookmarks, not just those with
        // a name of the form "_Toc*".  We don't check for missing _Toc bookmarks.
        result = bm.check(wordMLPackage.getMainDocumentPart(), remediate);
    } catch (Exception e) { /* won't happen */}
    if (result==BookmarksStatus.BROKEN) {
        throw new TocException("Encountered broken bookmarks; not configured to remediate. \n" + sw.toString());
    }

    if (Docx4J.pdfViaFO()) {
        return getPageNumbersMapViaFOP();
    } else {
        // recommended
        return getPageNumbersMapViaService();
    }
}

[请注意,除非您是Plutext PDF Converter服务的现有许可证持有人,否则将无法使用getPageNumbersMapViaService()。

[有关可能的替代方法,请参阅以https://www.docx4java.org/blog/2020/03/documents4j-for-toc-update/为基础的https://www.docx4java.org/blog/2020/03/documents4j-for-pdf-output/

© www.soinside.com 2019 - 2024. All rights reserved.