java 如何使用 Apache POI 为 docx 文件中的段落设置标题样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28628815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:55:22  来源:igfitidea点击:

How to set heading style for a paragraph in a docx file using Apache POI?

javafileapache-poidocx

提问by Abdulbaki FurkAn Tanr?verdi

I am trying to create a docx file with poi but I cannot set heading style for a paragraph.

我正在尝试使用 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)");

Its like ignores the paragraph.setStyle("Heading1");line. I've looked at the apache's examples but I could not see any example about this issue.

它就像忽略了这paragraph.setStyle("Heading1");条线。我看过 apache 的例子,但我看不到任何关于这个问题的例子。

回答by RobAu

If you start a new document, there are no style defined. Either start from an existing template and copy the styles, or create your own (like I did, based on https://stackoverflow.com/a/27864752/461499)

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

See here:

看这里:

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

回答by Abdulbaki FurkAn Tanr?verdi

I found a solution in the link below. Sorry for duplication.

我在下面的链接中找到了解决方案。抱歉重复。

How can I use predefined formats in DOCX with POI?

如何将 DOCX 中的预定义格式与 POI 一起使用?

But, If you have any other solution without using template file, please let me know :)

但是,如果您在不使用模板文件的情况下有任何其他解决方案,请告诉我:)

regards

问候