How to set a font family to an entire Word document in Apache POI XWPF

WEGSY85 picture WEGSY85 · Feb 10, 2015 · Viewed 8.3k times · Source

Is there a way to set a default font family to a word document generated by Apache POI instead of setting the font family to each XWPFRun?

Answer

WEGSY85 picture WEGSY85 · Jul 6, 2017

I created a new method

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

Then I call it like that:

XWPFParagraph paragraphHeader = document.createParagraph();
XWPFRun runTextHeader = createRun(paragraphHeader, WordStyling.FONT_FAMILY_TIMES, WordStyling.FONT_SIZE_14);

or more generic:

public static XWPFRun createRun(XWPFParagraph paragraph){
    XWPFRun run = paragraph.createRun();
    run.setFontSize(12);
    run.setFontFamily("Times New Roman");
    return run;
}