如何设置目录的字体样式,包括poi word中的字体颜色,字体大小和粗体? [英] How to set table of content's font style including font color, font size and bold in poi word?

查看:1330
本文介绍了如何设置目录的字体样式,包括poi word中的字体颜色,字体大小和粗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用doc.createTOC();生成poi word(XWPF)中的目录,并使用addCustomHeadingStyle(doc, ChapterLvl.LVL_1.style, 1);生成标题Content,虚线和页码.但是我想设置不同的标题级别的标题内容具有不同的字体大小,字体颜色,粗体以及虚线和页码具有与相应的标题内容相同的样式,例如图片附件,有人可以给我一些建议怎么做?

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()可以得到想要的东西吗?你得到什么?它是否位于文档中的适当位置?关于SOC,这里还有其他问题. 这个特别有帮助.如果您需要其他行为,则上面提到的规范会为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.

这篇关于如何设置目录的字体样式,包括poi word中的字体颜色,字体大小和粗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆