Page 1 of 1

2007 - 2010 compatibility mode for files saved with docx4j

PostPosted: Tue Jan 03, 2012 11:36 pm
by nicolas
hi

when i open a file created in word 2007 in word 2010 it its opened in compatibility mode.

i'm working with word 2010 files. but when i load a word 2010 file with docx4j and save it without modifying and open it in word, it is also opened in compatibilty mode.
is docx4j saving files in word 2007 format? and is it possible to change this behaviour?

greetings, nicolas

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Wed Jan 04, 2012 1:58 pm
by jason
Good question.

A docx saved in Word 2010 in compatibility mode has the following in /word/settings.xml:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:compat>
    <w:useFELayout/>
    <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="12"/>
  </w:compat>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


whereas a docx saved "natively" in Word 2010 in compatibility mode has:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:compat>
    <w:useFELayout/>
    <w:compatSetting w:name="compatibilityMode" w:uri="http://schemas.microsoft.com/office/word" w:val="14"/>
    <w:compatSetting w:name="overrideTableStyleFontSizeAndJustification" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
    <w:compatSetting w:name="enableOpenTypeFeatures" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
    <w:compatSetting w:name="doNotFlipMirrorIndents" w:uri="http://schemas.microsoft.com/office/word" w:val="1"/>
  </w:compat>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Documents created from scratch in docx4j have no settings.xml part, unless you specifically add on. It appears that in the absence of any w:compat element, Word 2010 assumes compatibility mode, which makes sense.

So, to get out of compatibility mode, you need to add the above w:compat setting (w:compatSetting/@w:name='compatibilityMode' at least). It would be good to add a method to org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart to set this. It would also be good to add a property to docx4j.properties, which would determine whether this is set when a new document is created.

cheers .. Jason

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Thu Jan 05, 2012 3:59 am
by nicolas
Ok, I try to implement it but I need some hints how to do it...
I can add the settings programmatically. But then I need to create a CTSettings Object and an instance of CTCompat to specify the compatibility mode. But there are no methods for setting the compatibility mode in CTCompat. Do I have to add those setters or should I try to do it with XmlUtils.unmarshalString(...)? Please give me some hints which classes I have to modify.

Thanks, Nicolas

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Thu Jan 05, 2012 5:14 am
by nicolas
why is the ct_compatsetting missing in the xsd? is it possible to generate the compatsetting elements programmatically anyway?

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Thu Jan 05, 2012 2:11 pm
by jason
Now that you point this out, having looked into it further ....

CT_CompatSetting is defined in (ECMA-376 2ed & 3ed):

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <xsd:complexType name="CT_Compat">
    <xsd:sequence>
      <xsd:element name="spaceForUL" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="balanceSingleByteDoubleByteWidth" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="doNotLeaveBackslashAlone" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="ulTrailSpace" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="doNotExpandShiftReturn" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="adjustLineHeightInTable" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="applyBreakingRules" type="CT_OnOff" minOccurs="0"/>
      <xsd:element name="compatSetting" type="CT_CompatSetting" minOccurs="0" maxOccurs="unbounded"
     />
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="CT_CompatSetting">
    <xsd:attribute name="name" type="s:ST_String"/>
    <xsd:attribute name="uri" type="s:ST_String"/>
    <xsd:attribute name="val" type="s:ST_String"/>
  </xsd:complexType>
 
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


But it is not in the first edition, which is what docx4j uses. In the first edition, there are a bunch of other elements in the CT_Compat sequence, which seem to have gone in the 2nd & 3rd editions...

What to do? It does seem useful to have this element available, so what if we add it into the schema used by docx4j, and regenerate? I guess the risk is that a consuming application (eg a existing or future version of Word?) might complain if it encounters a mix of elements from the 1st and 2nd or 3rd editions. It would be up to users of docx4j to avoid saving such a mix.

OK .. added, see http://www.docx4java.org/trac/docx4j/changeset/1741

In case you are not building docx4j from source, I'll create a nightly later today.

cheers .. Jason

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Thu Jan 05, 2012 10:16 pm
by nicolas
Thank you very much. I'm not building it from source and wait for the nightly.

Re: 2007 - 2010 compatibility mode for files saved with docx

PostPosted: Fri Jan 06, 2012 12:39 am
by jason
No worries. The nightly is there now :-)