如何在poi word中设置目录的字体样式,包括字体颜色、字体大小和粗体?
我使用 doc.createTOC();
在 poi word(XWPF) 中生成目录,并使用 addCustomHeadingStyle(doc, ChapterLvl.LVL_1.style, 1);代码>生成标题内容、虚线和页码.但是我想设置不同的标题级别的标题内容有不同的字体大小,字体颜色,粗体,虚线和页码与其对应的标题内容具有相同的样式,如图片附件,有人可以给我一些建议如何做到这一点?
I using doc.createTOC();
to generate table of content in poi word(XWPF), and using addCustomHeadingStyle(doc, ChapterLvl.LVL_1.style, 1);
to generate headings Content, dotted line and page number. But I want to set different heading level's headings Content have different font size,font color, bold, and dotted line and page number have same style with its corresponding heading content, like the picture attachment, can someone give me some advice how to do that?
private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
ctStyle.setQFormat(onoffnull);
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
您的很多问题似乎都与 XWPF 有关.这是一个不完整的 API.您可以阅读规范来确定如何做 API 中没有出现的事情,然后可能将其回馈给社区.规范位于此处.Apache POI 项目使用第一版模式.我发现第 1、3 和 4 部分最有用.
A lot of your questions seem to be about XWPF. This is an incomplete API. You could read the spec to determine how to do things not surfaced in the API, and then potentially donate it back to the community. The specs are here. The Apache POI project uses the first edition schema. I find that Parts 1, 3, and 4 are the most useful.
至于这个具体问题,您是否通过 doc.createTOC()
获得了您想要的结果?你得到什么?它是否位于文档中的适当位置?在 SO 上还有其他关于 TOC 的问题.这个特别有用.如果您需要其他行为,上面提到的规范包含更多 TOC 字段的开关,例如要包含的自定义标题样式.可以通过修改适当的 TOCx 样式来设置 TOC 级别的样式,其中 x
是 TOC 级别.
As to this specific question, are you getting what you want with doc.createTOC()
? What are you getting? Is it located in the appropriate place in the document? There are other questions concerning TOC here on SO. This one in particular may help. If you need additional behaviors, the specs mentioned above contain more switches for the TOC field such as custom header styles to be included. The style of the TOC levels can be set by modifying the appropriate TOCx style where x
is the TOC level.