java - 如何通过poi为word中的toc设置字体
问题描述
我已经创建了目录,我想设置目录字体、段落间距和缩进样式。以下是我的代码。截图的风格是我想要的。 在此处输入图片描述 请帮助我,谢谢!
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.math.BigInteger;
public class CreateWordNumberedHeadings {
public static void main(String[] args) throws Exception {
static String cTStyleTOC1 ="<w:style xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:type=\"paragraph\" w:styleId=\"TOC1\">"
+ "<w:name w:val=\"toc 1\"/>"
+ "<w:basedOn w:val=\"Normal\"/>"
+ "<w:next w:val=\"Normal\"/>"
+ "<w:autoRedefine/><w:unhideWhenUsed/>"
+ "<w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:cs=\"仿宋_GB2312\" /><w:b/><w:bCs/><w:caps/><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr>"
+ "</w:style>";
static String cTStyleTOC2 ="<w:style xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:type=\"paragraph\" w:styleId=\"TOC2\">"
+ "<w:name w:val=\"toc 2\"/>"
+ "<w:basedOn w:val=\"Normal\"/>"
+ "<w:next w:val=\"Normal\"/>"
+ "<w:autoRedefine/><w:unhideWhenUsed/>"
+ "<w:rPr><w:rFonts w:ascii=\"仿宋_GB2312\" w:cs=\"仿宋_GB2312\" /><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/></w:rPr>"
+ "</w:style>";
XWPFDocument document = new XWPFDocument();
createTOC(document);
createtFirstTitle(document, "First Title");
createtSecondTitle(document, "Second Title");
createtSecondTitle(document, "Second Title");
createtContent(document, "content ....");
createtFirstTitle(document, "First Title");
createtSecondTitle(document, "Second Title");
createtSecondTitle(document, "Second Title");
createtContent(document, "content ....");
FileOutputStream out = new FileOutputStream("f:\\CreateWordNumberedHeadings.docx");
document.write(out);
}
private static void createTOC( XWPFDocument document) throws XmlException {
XWPFParagraph p = document.createParagraph();
p.setSpacingBetween(1.25);
p.setSpacingBeforeLines(150);
p.setSpacingAfterLines(150);
p.setAlignment(ParagraphAlignment.CENTER);
XWPFRun r = p.createRun();
r.setText("目 录");
r.setFontFamily("黑体");
r.setFontSize(15);
XWPFParagraph paragraph = document.createParagraph();
CTSimpleField toc = paragraph.getCTP().addNewFldSimple();
toc.setInstr("TOC \\* MERGEFORMAT");
toc.setDirty(STOnOff.TRUE);
XWPFStyles styles = document.createStyles();
addCustomHeadingStyle(styles,"index1",0);
addCustomHeadingStyle(styles,"index2",1);
CTStyles cTStyles = CTStyles.Factory.parse(cTStyleTOC1);
CTStyle cTStyle = cTStyles.getStyleArray(0);
styles.addStyle(new XWPFStyle(cTStyle));
cTStyles = CTStyles.Factory.parse(cTStyleTOC2);
cTStyle = cTStyles.getStyleArray(0);
styles.addStyle(new XWPFStyle(cTStyle));
}
private static void addCustomHeadingStyle(XWPFStyles styles, 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));
// lower number > style is more prominent in the formats bar
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// style shows up in the formats bar
ctStyle.setQFormat(onoffnull);
// style defines a heading of the given level
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
public static void createtFirstTitle(XWPFDocument doc,String title){
XWPFParagraph para = doc.createParagraph();
para.setSpacingBetween(1.25);
para.setSpacingBeforeLines(150);
para.setSpacingAfterLines(150);
para.setAlignment(ParagraphAlignment.CENTER);
para.setStyle("index1");
para.setPageBreak(true);
XWPFRun r = para.createRun();
r.setText(title);
r.setFontSize(15);
r.setFontFamily("黑体");
}
public static void createtSecondTitle(XWPFDocument doc,String title){
XWPFParagraph para = doc.createParagraph();
para.setStyle("index2");
para.setFirstLineIndent(600);
para.setAlignment(ParagraphAlignment.LEFT);
para.setSpacingBetween(1.25);
para.setSpacingBefore(270);
para.setSpacingAfter(270);
XWPFRun r = para.createRun();
r.setText(title);
r.setFontSize(14);
r.setBold(true);
r.setFontFamily("仿宋_GB2312");
}
public static void createtContent(XWPFDocument doc, String content){
XWPFParagraph para = doc.createParagraph();
para.setAlignment(ParagraphAlignment.LEFT);
para.setFirstLineIndent(600);
para.setSpacingBetween(1.25);
XWPFRun run = para.createRun();
run.setText(content);
run.setFontFamily("仿宋_GB2312");
run.setFontSize(14);
}
}
解决方案
请注意,目录包含在段落中。您可以通过向段落添加属性来将样式应用于段落,例如
CTPPr ctPPr = ctP.isSetPPr() ? ctP.getPPr() : ctP.addNewPPr();
CTString pStyle = ctPPr.isSetPStyle() ? ctPPr.getPStyle() : ctPPr.addNewPStyle();
pStyle.setVal("TOC");
因为您使用的是简单字段,所以整个目录将使用相同的样式。您当然需要使用适当的字体创建样式。
推荐阅读
- reactjs - React Js、TypeScript、将标签映射到下拉菜单
- python - 在不同操作系统中从 github 克隆项目时出现虚拟环境问题
- php - 使用 amazon ses php SDK 发送多封电子邮件
- c - 在 C 中,声明一个二维字符数组还是一个参差不齐的字符串数组更节省空间?(具体问题如下)
- flutter - 缓存数据还是在flutter中使用存储?
- python-3.x - s3 后签名与 AWS 文档示例不匹配(python)
- arrays - 我无法将 csv 加载到临时表 HIVE
- matlab - readtable() 函数正在从我的表中删除前三行
- postgresql - 使用 Postgresql 授予对所有数据库及其表的访问权限
- android - 2020 年我可以在不上传 Play 商店的情况下使用 Admob 吗?