- Code: Select all
RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
org.docx4j.relationships.Relationship hdrRelationship = rp.getRelationshipByType(Namespaces.HEADER);
HeaderPart hdrPart = (HeaderPart)rp.getPart(hdrRelationship);
HeaderPart newHdrPart = new HeaderPart();
newHdrPart.setContents(XmlUtils.deepCopy(hdrPart.getContents()));
wordMLPackage.getMainDocumentPart().addTargetPart(newHdrPart, RelationshipsPart.AddPartBehaviour.RENAME_IF_NAME_EXISTS);
content = newHdrPart.getContent();
mergeMSWordDocContentViaDocX4J(wordMLPackage, content, replacementMap);
The call to mergMSWordDocContentViaDocX4J() method is something that I created that just walks through the paragraphs/runs searching for text to replace and replacing it with whatever the replacementMap parameter says should go there.
It seems to work correctly until I try to open the resulting docx file. Word won't open it because there is a problem with the contents. When I validate the file using the Open XML SDK Productivity Tool, it gives me a list of 14 errors like these:
Document /word/document.xml The Ignorable attribute is invalid - The value 'w14 w15 w16se w16cid wp14' contains an invalid prefix that is not defined.
Paragraph /word/footer1.xml The 'http://schemas.microsoft.com/office/word/2010/wordml:paraId' attribute is not declared
Paragraph /word/footer1.xml The 'http://schemas.microsoft.com/office/word/2010/wordml:textId' attribute is not declared
etc.
These all look like namespace problems to me but I'm not doing anything to change/set namespaces (if that is even possible) in docX4J.
The code in my mergeMSWordDocContentViaDocX4J() method works fine if the document contains nothing in the header/footer to be changed - it produces a document that Word can open just fine. One note: if I unzip one of those docx files that Word can open, and view the document or header or footer xml files, it looks like the namespaces have changed just as the namespaces are changed in the merged files that fail to open. Does anyone have an idea what would cause the corruption that I describe above and how I fix the problem?
Thanks in advance.