Search code examples
javafileapache-poidocx

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


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

    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.


Solution

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

    How can I use predefined formats in DOCX with POI?

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