有没有办法为Apache POI生成的Word文档设置默认字体系列,而不是为每个XWPFRun设置字体系列?

最佳答案

我创建了一个新方法

public static XWPFRun createRun(XWPFParagraph paragraph, String     fontFamily, int fontSize, boolean bold, UnderlinePatterns underline){
    XWPFRun run = paragraph.createRun();
    run.setFontFamily(fontFamily);
    run.setBold(bold);
    run.setUnderline(underline);
    run.setFontSize(fontSize);
    return run;
}

然后我这样称呼它:
XWPFParagraph paragraphHeader = document.createParagraph();
XWPFRun runTextHeader = createRun(paragraphHeader, WordStyling.FONT_FAMILY_TIMES, WordStyling.FONT_SIZE_14);

或更通用的:
public static XWPFRun createRun(XWPFParagraph paragraph){
    XWPFRun run = paragraph.createRun();
    run.setFontSize(12);
    run.setFontFamily("Times New Roman");
    return run;
}

07-24 09:16