I want to create a bulleted/numbered list in a docx word document with Java. I am using the Apache POI 3.10 library. If I understand correctly, the steps would be like this:
I was trying to do something similar and hit my head up against it until it started working.
Here was my approach to adding AbstractNum to the document's numbering object. Calling 'addAbstractNum()' turns out to have a null bug in the version I am using (3.10-FINAL) for doing this. So to get around it, you will need to generate your AbstractNum in XML, parse it, manually assign it an id, and add it to the document's numbering object.
This is how I did it:
protected XWPFDocument doc;
private BigInteger addListStyle(String style)
{
try
{
XWPFNumbering numbering = doc.getNumbering();
// generate numbering style from XML
CTAbstractNum abstractNum = CTAbstractNum.Factory.parse(style);
XWPFAbstractNum abs = new XWPFAbstractNum(abstractNum, numbering);
// find available id in document
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);
}
// assign id
abs.getAbstractNum().setAbstractNumId(id);
// add to numbering, should get back same id
id = numbering.addAbstractNum(abs);
// add to num list, result is numid
return doc.getNumbering().addNum(id);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
The format of the 'style' string, that is passed to the method, requires knowledge of XWPF documents -- of which, I have none. So I created a word document with a numbering style that I wanted. Saved it to a '.docx' file. Unzipped the '.docx' file, and copied the XML fragment from 'numbering.xml'. It will look something like this:
Take that string, pass it to the method above. Now you have a numID that you can make lists out of.
XWPFParagraph para = doc.createParagraph();
para.setStyle("ListParagraph");
para.setNumID(listType);
para.getCTP().getPPr().getNumPr().addNewIlvl().setVal(BigInteger.valueOf(level));
Good Luck.