如何使用 Apache POI 设置 docx 文件中段落的标题样式? [复制]

2023-11-29

我正在尝试使用 poi 创建 docx 文件,但无法设置段落的标题样式。

    XWPFDocument document= new XWPFDocument(); 
    
    //Write the Document in file system
    FileOutputStream out = new FileOutputStream(new File("C:/Users/2/Desktop/RequirementModelDocument.docx"));

    XWPFParagraph paragraph = document.createParagraph();
    XWPFRun run=paragraph.createRun();


    paragraph.setAlignment(ParagraphAlignment.LEFT);
    paragraph.setStyle("Heading1");
    
    run.setText(reqLevel.getName());
    run.setBold(true);
    run.setFontFamily("Calibri Light (Headings)");

它就像忽略了paragraph.setStyle("Heading1");线。我查看了 apache 的示例,但没有看到任何有关此问题的示例。


如果您开始一个新文档,则不会定义任何样式。要么从现有模板开始并复制样式,要么创建自己的样式(就像我所做的那样,基于https://stackoverflow.com/a/27864752/461499 )

看这里:

    XWPFDocument document = new XWPFDocument();
    XWPFStyles styles = document.createStyles();

    String heading1 = "My Heading 1";
    String heading2 = "My Heading 2";
    String heading3 = "My Heading 3";   
    String heading4 = "My Heading 4";
    addCustomHeadingStyle(document, styles, heading1, 1, 36, "4288BC");
    addCustomHeadingStyle(document, styles, heading2, 2, 28, "4288BC");
    addCustomHeadingStyle(document, styles, heading3, 3, 24, "4288BC");
    addCustomHeadingStyle(document, styles, heading4, 4, 20, "000000");

    XWPFParagraph paragraph = document.createParagraph();
    paragraph.setStyle(heading1);
    XWPFRun run = paragraph.createRun();
    run.setText("Nice header!");

And

private static void addCustomHeadingStyle(XWPFDocument docxDocument, XWPFStyles styles, String strStyleId, int headingLevel, int pointSize, String hexColor) {

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

    CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
    size.setVal(new BigInteger(String.valueOf(pointSize)));
    CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance();
    size2.setVal(new BigInteger("24"));
    
    CTFonts fonts = CTFonts.Factory.newInstance();
    fonts.setAscii("Loma" );

    CTRPr rpr = CTRPr.Factory.newInstance();
    rpr.setRFonts(fonts);
    rpr.setSz(size);
    rpr.setSzCs(size2);

    CTColor color=CTColor.Factory.newInstance();
    color.setVal(hexToBytes(hexColor));
    rpr.setColor(color);
    style.getCTStyle().setRPr(rpr);
    // is a null op if already defined

    style.setType(STStyleType.PARAGRAPH);
    styles.addStyle(style);

}

public static byte[] hexToBytes(String hexString) {
     HexBinaryAdapter adapter = new HexBinaryAdapter();
     byte[] bytes = adapter.unmarshal(hexString);
     return bytes;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Apache POI 设置 docx 文件中段落的标题样式? [复制] 的相关文章

随机推荐