i have some problems importing styles from a docx template. I want to extract these styles to apply it to my new document. Most of them have a numbering.
I can extract the style and numbering correctly from template. I can apply styles but can t apply numbering as my wordMLPackage.getMainDocumentPart().getNumberingDefinitionsPart() return null
If I try to not import NumberingDefinitionPart, all my styles implying listing with number turn to be considered as list without number (and that s not what I want).
I can t find a way to instanciate it and there is not method to set a new one. Here is my code.
public class ModuleToDocxGenerator {
private WordprocessingMLPackage wordMLPackage = null;
private XHTMLImporter xHTMLImporter = null;
private List<String> listStyle;
private ProjectType project;
private File file;
private Map<String, Object> mapElement = new HashMap<>();
private int taillePolice = 12;
private final String police = "Arial";
private Map<String, String> mapBookmark = new HashMap<>();
private int compteurIDBookMark = 1;
public void docxParser() {
// Récupération des styles du template
WordprocessingMLPackage wordMLPackage2 = null;
try {
wordMLPackage2 = WordprocessingMLPackage.load(new File("template.docx"));
} catch (Docx4JException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
wordMLPackage = WordprocessingMLPackage.createPackage();
} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
MainDocumentPart tempDocPart = wordMLPackage2.getMainDocumentPart();
//add Style part
StyleDefinitionsPart sdp = tempDocPart.getStyleDefinitionsPart();
Styles tempStyle = null;
// Add numbering part
NumberingDefinitionsPart ndp = tempDocPart.getNumberingDefinitionsPart();
Numbering numbStyle = null;
try {
tempStyle = sdp.getContents();
numbStyle=ndp.getContents();
} catch (Docx4JException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
wordMLPackage.getMainDocumentPart().getNumberingDefinitionsPart().setJaxbElement(numbStyle);
wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().setJaxbElement(tempStyle);
Could someone help me please?
Have a look at https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/NumberingRestart.java#L54
You need something like:
// Add numbering part
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.setJaxbElement( (Numbering) XmlUtils.unmarshalString(initialNumbering) );