Attain word document compatibilityMode value

Hello team.
We need to know the Compatibility Mode of a DOCX in order to fully implement the solution for the issue mentioned in this thread:
https://forum.aspose.com/t/document-bookmark-misplaced-in-the-last-document-after-document-appenddocument/320452/16

In order to do that we need to attain the CompatabilityMode from a given Document. While we understand it is possible to set the compatibility mode via the Document.getCompatibilityOptions().optimizeFor() API, we need a way to get the value corresponding to the MsWordVersion constants. Using Word VBA, it is possible to attain the compatibility mode via the Document.CompatibilityMode ( Document.CompatibilityMode property (Word) | Microsoft Learn) - but there doesn’t seem to be a corresponding API in the Aspose Words library.

We also understand it is possible to explore the “settings.xml” file embedded in the DOCX to attain the value, but it would be preferable to have an Aspose API for this information.

Thank you.

@oraspose

You want to read the compatibility mode (the MsWordVersion value) that a DOCX file was created with.

Aspose.Words for Java only provides a setter for this optionDocument.getCompatibilityOptions().optimizeFor(...) [2]. There is no public getter in the API, so the current mode has to be obtained from the document package itself.

Reading the value manually

import java.io.*;
import java.util.zip.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;

public class CompatibilityModeReader {
    public static String getCompatibilityMode(String docxPath) throws Exception {
        try (ZipFile zip = new ZipFile(docxPath)) {
            ZipEntry entry = zip.getEntry("word/settings.xml");
            if (entry == null) return "settings.xml not found";

            try (InputStream is = zip.getInputStream(entry)) {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                Document doc = dbf.newDocumentBuilder().parse(is);

                // The attribute w:compatibilityMode is stored in the w:documentSettings element
                NodeList list = doc.getElementsByTagNameNS("*", "documentSettings");
                if (list.getLength() == 0) return "documentSettings element not found";

                Element settings = (Element) list.item(0);
                String mode = settings.getAttributeNS("*", "compatibilityMode");
                return mode.isEmpty() ? "not set" : mode;   // e.g. "15" for Word 2013
            }
        }
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getCompatibilityMode("input.docx"));
    }
}

The integer returned matches the MsWordVersion enumeration used by Aspose Words (e.g., 12 = Word 2007, 14 = Word 2010, 15 = Word 2013, 16 = Word 2016/2019/365, etc.). You can map the value according to the enumeration defined in the Aspose Words API.

@oraspose You can try using the BuiltInDocumentProperties.Version property. This property represents the version number of the application that created the document. Below are the version numbers for MS Word.

11 = Word 2003
12 = Word 2007
14 = Word 2010
15 = Word 2013
16 = Word 2016
Document doc = new Document("in.docx");
int MSWVersion = doc.getBuiltInDocumentProperties().getVersion() >> 16;
System.out.println(MSWVersion);

We tried doing just that, but the problem is - all documents are created with the same version of the MS Word app 16 ( 16.0000 ) , but then some of them were altered to lower compatibility mode . Therefore we can’t rely on the BuiltInDocumentProperties.Version property.
Thank you.

@oraspose You can try using Document.CompatibilityOptions to check what compatibility options are set in each document. It is not mandatory the document has compatibility options set for specific version of MS Word. The user can set any compatibility options in any combinations.