如何在poi中为toc设置字体

时间:2018-04-27 03:46:30

标签: java fonts apache-poi tableofcontents

我创建了TOC,我想设置TOC字体,段落间距和缩进样式。以下是我的代码。 截图的风格是我想要的。 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);
  }
}

2 个答案:

答案 0 :(得分:1)

请注意,TOC包含在段落中。您可以通过向段落添加属性(例如

)将样式应用于段落
CTPPr ctPPr = ctP.isSetPPr() ? ctP.getPPr() : ctP.addNewPPr();
CTString pStyle = ctPPr.isSetPStyle() ? ctPPr.getPStyle() : ctPPr.addNewPStyle();
pStyle.setVal("TOC");

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

答案 1 :(得分:0)

问题在于您为标题创建了样式index1index2,但是您仍然手动格式化标题中的文本运行。但是,手动设置的格式不会被TOC中的样式覆盖。相反,手动设置的格式也将在TOC中考虑。这使得Word用户可以单独设置单个标题的格式,而与标题样式无关。个人格式也将在TOC中考虑。这是Microsoft Word的主要原则之一:通过样式进行全局格式化是可能的。但是,用户运行的单个文本的单独格式将被视为所需格式。

因此,您应该仅使用样式而不是手动格式化标题(标题)。

使用您的代码完成示例:

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_Bill {

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

  public static void main(String[] args) throws Exception {

    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("CreateWordNumberedHeadings_Bill.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, "黑体", 15f, false);
    addCustomHeadingStyle(styles,"index2", 1, "仿宋_GB2312", 14f, true);
    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, String fontFamily, float fontSize, boolean bold) {
    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);

    CTRPr rPr = ctStyle.addNewRPr();
    rPr.addNewRFonts().setAscii(fontFamily);
    rPr.getRFonts().setCs(fontFamily);
    rPr.addNewSz().setVal(BigInteger.valueOf((long)(fontSize*2)));
    rPr.addNewSzCs().setVal(BigInteger.valueOf((long)(fontSize*2)));
    if (bold) {
     rPr.addNewB();
     rPr.addNewBCs();
    }

    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);
  }
}
相关问题