Page 1 of 1

Updating text in header/footer results in corrupt docx file

PostPosted: Thu Mar 15, 2018 5:39 pm
by meworkingman
I am trying to merge text changes into headers/footers of a docx document (using version 3.3.0 of docx4j). Here is a code snippet where I am doing this:

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.

Re: Updating text in header/footer results in corrupt docx f

PostPosted: Fri Mar 16, 2018 2:58 am
by meworkingman
In case it would be helpful for anyone to figure out what the problem is, I'm uploading the original, non-updated, docx file and the one that I updated via docx4j.

Thanks again.

Re: Updating text in header/footer results in corrupt docx f

PostPosted: Fri Mar 16, 2018 6:25 am
by jason
meworkingman wrote: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


Please try docx4j 3.3.6.

Re: Updating text in header/footer results in corrupt docx f

PostPosted: Fri Mar 16, 2018 11:36 am
by meworkingman
Thanks for the reply, Jason. That did the trick. My header/footer merges are working now.