gpt4 book ai didi

java - 如何通过poi在word中设置目录字体

转载 作者:行者123 更新时间:2023-12-02 11:19:15 34 4
gpt4 key购买 nike

我已经创建了目录,我想设置目录字体、段落间距和缩进样式。以下是我的代码。截图的风格是我想要的。 enter image description here请帮助我,谢谢!

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");

因为您使用的是简单字段,所以整个目录将使用相同的样式。您当然需要创建具有适当字体的样式。

关于java - 如何通过poi在word中设置目录字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054921/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com