java - 无法使用 docx4j 更新文档目录中的页码
问题描述
生成目录时,我无法更新页码。它将异常显示为“遇到损坏的书签;未配置为补救”。我使用下面的代码来更新 TOC ..
TocGenerator tocGenerator = new TocGenerator(wordMLPackage);
tocGenerator.generateToc(0, "TOC \o \"1-3\" \h \z \u ", false);
tocGenerator.updateToc(false);
解决方案
消息来自这里:
/**
* 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 转换器服务的现有被许可人,否则您将无法使用 getPageNumbersMapViaService()。
有关可能的替代方法,请参阅https://www.docx4java.org/blog/2020/03/documents4j-for-toc-update/,它基于https://www.docx4java.org/blog/2020/03 /documents4j-for-pdf-输出/
推荐阅读
- html - 如何将h1元素与导航栏放在同一行
- javascript - fullcalender angular 10初始视图不起作用
- performance - 更高的 SAS 自愿等待时间阅读观察
- java - 为什么在运行 StAX Parser 时出现 NullPointerException?
- python - 如何将 ZipFile 对象转换为支持缓冲区 API 的对象?
- c# - Unity - 动态文本网格字体更改故障?
- c++ - 从服务器接收数据时如何在后台检测键盘事件(C++ TCP客户端)
- r - ggplot2中热图的特定单元格上的删除线覆盖
- javascript - Gmail 不加载 nodemailer 以 html 发送的图片
- javascript - JS:在算法中跳过布尔值