Page 1 of 1

Word rearranging content controls when reopened/saved?

PostPosted: Fri Oct 13, 2017 1:28 am
by martinuba
Hello,

we have setup a Word-template (using Word 2016) containing nested repeating sections inside tables.
We do not use any OpenDoPE-plugins for this, only standard Word content-controls are assigned from the XML-Pane.
When saving the template after assignment of content-controls and nested repeating sections inside a table (outer repeat is whole table, inner repeat is 3 rows inside the table) the docx4j3.3.5 binding-process works fine, all content gets mapped successfully.

The phenomenon we are facing is, when reopening the template with Word and - even without any changes - resaving it, the internal structure (inside word/document.xml) gets rearranged and any following binding-process fails with ClassCastException inside org.docx4j.model.datastorage.OpenDoPEHandler.processW15Repeat(Object, Map<String, CustomXmlPart>), in detail:
Code: Select all
      // replace its content
      SdtElement repeatingItem = (SdtElement)XmlUtils.unwrap(ca.getContent().get(0));

because the retrieved ca.getContent().get(0) represents a org.docx4j.wml.Tr-element instead of the expected SdtElement.

Now my questions are:
1. Did you in any other use-case detect this Word-behaviour?
2. Can't Word re-read it's own files preserving the original structure?
3. Are we mis-using these repeatable-section features, ie. can't this be done with Word content-controls?

Attached you find the originally saved (and working) Word-Template (original_repeating_multi_tablerows.docx) and it's contained document.xml (original_document.xml).
The offending changed document.xml can be produced by opening the Word-template and re-saving the template without changing anything. (I could not upload the xml because of file-size restrictions, which is strange also).
A sample XML input-file for binding is supplied as debug_input_spc.xml.

Please let me know your opinion, any help is greatly appreciated!
Of course I can supply additional information if needed,
kind regards, Martin

Re: Word rearranging content controls when reopened/saved?

PostPosted: Sat Oct 14, 2017 1:26 am
by jason
Thanks for your post.

I think its fair to say that most users for docx4j's binding code use it in conjunction with OpenDoPE repeats, not Word's. Because of this, you are the first to report this.

docx4j could be modified to handle the Word structures you describe, either by first transforming to the structure docx4j currently expects, or directly addressing the issue in OpenDoPEHandler.processW15Repeat

Or you could use OpenDoPE repeats. See further https://www.docx4java.org/blog/2015/01/ ... rime-time/

OpenDoPE repeats are used successfully in the real world for many levels of nesting. Plus, you can use OpenDoPE conditions (for which there is no Microsoft equivalent).

Re: Word rearranging content controls when reopened/saved?

PostPosted: Mon Oct 16, 2017 6:13 pm
by martinuba
Hello Jason,
thanks for your fast reply.
I will check if usage of the OpenDoPE-addin is an option for our template-editors ... after reading the linked article inside your answer there might not be another chance to have our templates working reliably.

The underlying problem of re-saving stored (and working) templates inside Word causing template-corruption might be reported to Microsoft, we will eventually post a support-request there.
I will keep you informed of any progress.