javaapache-poixwpf

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


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?


Solution

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