I'm dynamically generating a DOCX document with multiple paragraphs. Since docx4j was complaining that the namespace definitions were missing, I added them in my <w:p />
element. It seems like this works fine.
WordprocessingMLPackage wpMLPackage = getWordTemplate(FOOBAR);
MainDocumentPart mdp = wpMLPackage.getMainDocumentPart();
for (i = 0; i < LIMIT; ++i) {
String paragraphXml = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:v=\"urn:schemas-microsoft-com:vml\" w:rsidR=\"00085705\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\" w:rsidRDefault=\"0088320D\">...</w:p>";
mdp.addParagraph(paragraphXml);
}
However, since I'm repeating the paragraph in a loop, the namespace definitions will be added more than once. It would make more sense to insert them only once on a higher level. But I don't know how to do that.
How can I add the xmlns:*
arguments on a higher level in the XML structure? Are there any methods in MainDocumentPart
or WordprocessingMLPackage
that I can use? Word adds the namespace definitions to <w:document />
.
It should "just work". That is, without changing your code, JAXB will put the namespace declarations at the document level.