2

I am using Apache POI to edit the content of a docx file. I also need to apply a specific style to said docx. The style I need to apply is in a dotx file. In this question How can I use predefined formats in DOCX with POI? I have found the following solution:

XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));       

XWPFDocument doc = new XWPFDocument();      
// let's copy styles from template to new doc
doc.createStyles().setStyles(template.getStyle());

It was working very well until something changed recently. After some research, I have found that the problem comes from the numbering.xml inside docx file. From what I understand, numbering.xml and styles.xml are linked. So setting style to XWPFDocument is good, but I also need to set numbering.

I have found equivalent method to createStyles().setStyles(xxx): createNumbering().setNumbering(yyy).

setNumbering expects a parameter of type CTNumbering (just like setStyles expects a parameter of CTSyles). While a document has a method getStyles() returning a CTSyles, it does not have a method returning a CTNumbering. It does however have a getNumbering method returning a XWPFNumbering (again, juste like it has a getStyle() -without a s- returning a XWPFStyles).

I looked into the getStyles() -with a s- method and I have been able to create an equivalent one returning a CTNumbering.

My question is: is there a simpler way to apply the numbering of a document to another ?

Anne
  • 219
  • 2
  • 8

0 Answers0