Apache poi multiline bullet point is working but not multiple paragaraph?
Question: Generate word document using apache poi library bullet point is working but i
trying multiple paragraph not working,i have pasted below,

my java class code :

    
    
    package samplebuller;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.math.BigInteger;
    
    import org.apache.poi.xwpf.usermodel.TextAlignment;
    import org.apache.poi.xwpf.usermodel.XWPFAbstractNum;
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
    import org.apache.poi.xwpf.usermodel.XWPFNumbering;
    import org.apache.poi.xwpf.usermodel.XWPFParagraph;
    import org.apache.poi.xwpf.usermodel.XWPFRun;
    import org.apache.xmlbeans.XmlException;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
    
    
    
    public class Samplebullet {
           String fileName=""; 
           InputStream in = null;
           CTAbstractNum abstractNum = null;
           public Samplebullet() {
               try {
                   InputStream in = new FileInputStream("numbering.xml");
                   abstractNum = CTAbstractNum.Factory.parse(in);
               } catch (XmlException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
           public String createDocument(String fileName, String content) {
               this.fileName=fileName;
               XWPFDocument doc = new XWPFDocument();
    
               doc.createNumbering();
               XWPFNumbering numbering=null;
               numbering=doc.createNumbering();
                   XWPFParagraph para = doc.createParagraph();
                   para.setVerticalAlignment(TextAlignment.CENTER);
                   para.setNumID(addListStyle(abstractNum, doc, numbering));
                   XWPFRun run=para.createRun();
                   run.setText("JAVA Code");
                   run.addBreak();
                   XWPFParagraph para1 = doc.createParagraph();
                   para1.setVerticalAlignment(TextAlignment.CENTER);
                   para1.setNumID(addListStyle(abstractNum, doc, numbering));
                   XWPFRun run1=para.createRun();
                   run1.setText("PYTHON CODE");
                   run1.addBreak();
    
                   XWPFParagraph para2 = doc.createParagraph();
                   para2.setVerticalAlignment(TextAlignment.CENTER);
                   para2.setNumID(addListStyle(abstractNum, doc, numbering));
                   XWPFRun run2=para.createRun();
                   run2.setText("PHP CODE");
                   run2.addBreak();
    
    
               try {
                   FileOutputStream out = new FileOutputStream(fileName);
                   doc.write(out);
                   out.close();
                   in.close();
               } catch(Exception e) {}
               return null;
           }
           private BigInteger addListStyle(CTAbstractNum abstractNum, XWPFDocument doc, XWPFNumbering numbering) {
               try {
    
                   XWPFAbstractNum abs = new XWPFAbstractNum(abstractNum, numbering);
                   BigInteger id = BigInteger.valueOf(0);
                   boolean found = false;
                   while (!found) {
                       Object o = numbering.getAbstractNum(id);
                       found = (o == null);
                       if (!found)
                           id = id.add(BigInteger.ONE);
                   }
                   abs.getAbstractNum().setAbstractNumId(id);
                   id = numbering.addAbstractNum(abs);
                   return doc.getNumbering().addNum(id);
               } catch (Exception e) {
                   e.printStackTrace();
                   return null;
               }
           }
           public static void main(String[] args) throws Exception {
               String fileName="Test.docx";
               new Samplebullet().createDocument(fileName, "First Level@@Second Level@@Second Level@@First Level");
           }
    }
    

My xml numbering file code:

    
    
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <w:numbering xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
    
        <w:abstractNum w:abstractNumId="0">
        <w:nsid w:val="3A6A237F"/>
        <w:multiLevelType w:val="hybridMultilevel"/>
        <w:tmpl w:val="5C9890C4"/>
        <w:lvl w:ilvl="0" w:tplc="0409000F"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="1" w:tplc="04090019" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/><w:lvlText w:val="%2."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="1440" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="2" w:tplc="0409001B" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerRoman"/><w:lvlText w:val="%3."/><w:lvlJc w:val="right"/><w:pPr><w:ind w:left="2160" w:hanging="180"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%4."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="2880" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/><w:lvlText w:val="%5."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="3600" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerRoman"/><w:lvlText w:val="%6."/><w:lvlJc w:val="right"/><w:pPr><w:ind w:left="4320" w:hanging="180"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%7."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="5040" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/><w:lvlText w:val="%8."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="5760" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerRoman"/><w:lvlText w:val="%9."/><w:lvlJc w:val="right"/><w:pPr><w:ind w:left="6480" w:hanging="180"/></w:pPr></w:lvl>
        </w:abstractNum>
    
        <w:abstractNum w:abstractNumId="1">
        <w:nsid w:val="5E7736F6"/>
        <w:multiLevelType w:val="hybridMultilevel"/>
        <w:tmpl w:val="F602653C"/>
        <w:lvl w:ilvl="0" w:tplc="0409000F"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="1" w:tplc="04090001"><w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val=""/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="1440" w:hanging="360"/></w:pPr><w:rPr><w:rFonts w:ascii="Symbol" w:hAnsi="Symbol" w:hint="default"/></w:rPr></w:lvl>
        <w:lvl w:ilvl="2" w:tplc="04090003"><w:start w:val="1"/><w:numFmt w:val="bullet"/><w:lvlText w:val="o"/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="2340" w:hanging="360"/></w:pPr><w:rPr><w:rFonts w:ascii="Courier New" w:hAnsi="Courier New" w:hint="default"/></w:rPr></w:lvl>
        <w:lvl w:ilvl="3" w:tplc="0409000F" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%4."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="2880" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="4" w:tplc="04090019" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/><w:lvlText w:val="%5."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="3600" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="5" w:tplc="0409001B" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerRoman"/><w:lvlText w:val="%6."/><w:lvlJc w:val="right"/><w:pPr><w:ind w:left="4320" w:hanging="180"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="6" w:tplc="0409000F" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="decimal"/><w:lvlText w:val="%7."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="5040" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="7" w:tplc="04090019" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerLetter"/><w:lvlText w:val="%8."/><w:lvlJc w:val="left"/><w:pPr><w:ind w:left="5760" w:hanging="360"/></w:pPr></w:lvl>
        <w:lvl w:ilvl="8" w:tplc="0409001B" w:tentative="1"><w:start w:val="1"/><w:numFmt w:val="lowerRoman"/><w:lvlText w:val="%9."/><w:lvlJc w:val="right"/><w:pPr><w:ind w:left="6480" w:hanging="180"/></w:pPr></w:lvl>
        </w:abstractNum>
    
        <w:num w:numId="1"><w:abstractNumId w:val="1"/></w:num>
        <w:num w:numId="2"><w:abstractNumId w:val="0"/></w:num>
    
        </w:numbering>
    

Please anyone help me multiline bullet point,Thanks in advance


Answer: There mainly two issues with your code:

  1. Your `run`s are all in `para`, which is the first paragraph. I don't believe that this shall be so.

  2. Your `addListStyle` creates abstract numberings each time it is called. So your `numbering.xml` gets unnecessary big and contains multiple abstract numberings with the same IDs 0 and 1. The `addListStyle` should be called only once. The `BigInteger` should be used as `numID` for all paragraphs which uses the same numbering.

But I would the whole thing not do that complicated. Consider the following
example:

    
    
    import java.io.File;
    import java.io.FileOutputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTAbstractNum;
    
    import java.math.BigInteger;
    
    public class CreateWordBulletOrDecimalList {
    
     static String cTAbstractNumBulletXML = 
      "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"0\">"
    + "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
    + "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Symbol\" w:hAnsi=\"Symbol\" w:hint=\"default\"/></w:rPr></w:lvl>"
    + "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"o\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Courier New\" w:hAnsi=\"Courier New\" w:cs=\"Courier New\" w:hint=\"default\"/></w:rPr></w:lvl>"
    + "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"bullet\"/><w:lvlText w:val=\"\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr><w:rPr><w:rFonts w:ascii=\"Wingdings\" w:hAnsi=\"Wingdings\" w:hint=\"default\"/></w:rPr></w:lvl>"
    + "</w:abstractNum>";
    
     static String cTAbstractNumDecimalXML = 
      "<w:abstractNum xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" w:abstractNumId=\"0\">"
    + "<w:multiLevelType w:val=\"hybridMultilevel\"/>"
    + "<w:lvl w:ilvl=\"0\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"720\" w:hanging=\"360\"/></w:pPr></w:lvl>"
    + "<w:lvl w:ilvl=\"1\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"1440\" w:hanging=\"360\"/></w:pPr></w:lvl>"
    + "<w:lvl w:ilvl=\"2\" w:tentative=\"1\"><w:start w:val=\"1\"/><w:numFmt w:val=\"decimal\"/><w:lvlText w:val=\"%1.%2.%3\"/><w:lvlJc w:val=\"left\"/><w:pPr><w:ind w:left=\"2160\" w:hanging=\"360\"/></w:pPr></w:lvl>"
    + "</w:abstractNum>";
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument document = new XWPFDocument();
    
      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run=paragraph.createRun();  
      run.setText("The List:");
    
      CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.parse(cTAbstractNumBulletXML);
      //CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.parse(cTAbstractNumDecimalXML);
    
      XWPFAbstractNum abstractNum = new XWPFAbstractNum(cTAbstractNum);
    
      XWPFNumbering numbering = document.createNumbering();
    
      BigInteger abstractNumID = numbering.addAbstractNum(abstractNum);
    
      BigInteger numID = numbering.addNum(abstractNumID);
    
      for (int i = 0; i < 3; i++) {
       paragraph = document.createParagraph();
       paragraph.setNumID(numID);
       run = paragraph.createRun();
       run.setText("List item " + i); 
       if (i < 2) paragraph.setSpacingAfter(0);
       if (i == 1) {
        paragraph = document.createParagraph();
        paragraph.setNumID(numID);
        paragraph.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(1));
        run = paragraph.createRun();
        run.setText("Sub list item " + i + " a");
        paragraph.setSpacingAfter(0); 
       }
      }
    
      paragraph = document.createParagraph();
      run=paragraph.createRun();  
      run.setText("Paragraph after the list.");
    
      FileOutputStream out = new FileOutputStream("CreateWordBulletOrDecimalList.docx");    
      document.write(out);
    
      System.out.println("CreateWordBulletOrDecimalList written successully");
     }
    }
    

