Search code examples
javaapache-poidocxxwpf

How can I create a Word file with a standard (Dutch) heading style?


How can I add a standard (Dutch) heading style to a new Word file?

enter image description here

The first title should have style (Dutch) style "Kop 1" (= Heading1).

I tried a number of standard solutions, but they don't work. The result is always the default standard style.

1: Create an empty Word file and use it as a kind of template with styles:

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");

Or

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Kop 1");

2: Use a template file.

XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));       
XWPFDocument doc = new XWPFDocument();
XWPFStyles newStyles = doc.createStyles();
newStyles.setStyles(template.getStyle());

XWPFParagraph para = doc.createParagraph();
para.setStyle("Heading1");
// OR: para.setStyle( "Kop 1");

XWPFRun run = para.createRun();
run.setText("Header 1");
// or: run.setText("Kop 1");

Solution

  • Multiple problems here.

    At first the Word template must contain the style already. Not all Word documents contain all styles per default. Only those styles are contained which got used in the document before. The term "got used before" only means the style sometime was used in the document. There needs not to be current text formatted using that style. Therefore, any styles that are to be saved in the document must be used temporarily before saving the template.

    And seems someone at Microsoft has mixed up ID and Name of styles. As I would understand ID vs. Name, the ID should be a technically internal identifier while the name should be the visible title, which also could be localized. But with Word styles, the ID is localized. In German Word for example, the heading style IDs are "berschrift1", "berschrift2", ... That comes from "Überschrift1", "Überschrift2", ... without the umlauts, which are not allowed for IDs. In opposite the names of that styles are "heading 1", "heading 2", ... even for German Word documents. That should be vice versa in my opinion.

    I guess in your Dutch Word the style IDs are "Kop1", "Kop2", ... without the spaces. But the better way is to get the styles by name as the names seems to be English for all Word locales.

    Complete example:

    WordTemplate.docx:

    enter image description here

    Code:

    import java.io.FileOutputStream;
    import java.io.FileInputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    public class WordUseStylesByName {
        
     private static XWPFStyle getStyleByName(XWPFStyles styles, String styleName) { 
      if (styles == null || styleName == null) return null;
      XWPFStyle style = styles.getStyleWithName(styleName); 
      if (style == null) {
       System.out.println("No style with name " + styleName + " present.");
      }
      return style;
     }    
    
     public static void main(String[] args) throws Exception {
      try (
            XWPFDocument document = new XWPFDocument(new FileInputStream("./WordTemplate.docx"));
            FileOutputStream out = new FileOutputStream("./WordResult.docx");
           ) {
      
       XWPFStyles styles;
       XWPFStyle style;
       String styleId;
       XWPFParagraph paragraph;
       XWPFRun run;
       
       // get heading 1 style
       styles = document.getStyles();
       style = getStyleByName(styles, "heading 1");
       styleId = (style != null)?style.getStyleId():"";
       System.out.println(styleId);
      
       paragraph = document.createParagraph();
       paragraph.setStyle(styleId);
       run = paragraph.createRun();
       run.setText("Text formatted as heading 1");
       
       // get heading 2 style
       style = getStyleByName(styles, "heading 2");
       styleId = (style != null)?style.getStyleId():"";
       System.out.println(styleId);
    
       paragraph = document.createParagraph();
       paragraph.setStyle(styleId);
       run = paragraph.createRun();
       run.setText("Text formatted as heading 2");
    
       document.write(out);
      }
     }
    }
    

    WordResult.docx:

    enter image description here