CompatibilityOptionsOptimizeFor Method

Allows to optimize the document contents as well as default Aspose.Words behavior to a particular versions of MS Word.

Use this method to prevent MS Word from displaying "Compatibility mode" ribbon upon document loading. (Note that you may also need to set the Compliance property to Iso29500_2008_Transitional or higher.)

Namespace:  Aspose.Words.Settings
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public void OptimizeFor(
	MsWordVersion version
)

Parameters

version
Type: Aspose.Words.SettingsMsWordVersion
Examples
Shows how to optimize document for different word versions.
public void OptimizeFor()
{
    // Create a blank document and get its CompatibilityOptions object
    Document doc = new Document();
    CompatibilityOptions options = doc.CompatibilityOptions;

    // By default, the CompatibilityOptions will contain the set of values printed below
    Console.WriteLine("\nDefault optimization settings:");
    PrintCompatibilityOptions(options);

    // These attributes can be accessed in the output document via File > Options > Advanced > Compatibility for...
    doc.Save(ArtifactsDir + "CompatibilityOptions.OptimizeFor.DefaultSettings.docx");

    // We can use the OptimizeFor method to set these values automatically
    // for maximum compatibility with some Microsoft Word versions
    doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);
    Console.WriteLine("\nOptimized for Word 2010:");
    PrintCompatibilityOptions(options);

    doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2000);
    Console.WriteLine("\nOptimized for Word 2000:");
    PrintCompatibilityOptions(options);
}

/// <summary>
/// Prints all options of a CompatibilityOptions object and indicates whether they are enabled or disabled
/// </summary>
private static void PrintCompatibilityOptions(CompatibilityOptions options)
{
    for (int i = 1; i >= 0; i--)
    {
        Console.WriteLine(Convert.ToBoolean(i) ? "\tEnabled options:" : "\tDisabled options:");
        SortedSet<string> optionNames = new SortedSet<string>();

        foreach (System.ComponentModel.PropertyDescriptor descriptor in System.ComponentModel.TypeDescriptor.GetProperties(options))
        {
            if (descriptor.PropertyType == Type.GetType("System.Boolean") && i == Convert.ToInt32(descriptor.GetValue(options)))
            {
                optionNames.Add(descriptor.Name);
            }
        }

        foreach (string s in optionNames)
        {
            Console.WriteLine($"\t\t{s}");
        }
    }
}
See Also