12
Views
10
Comments
How to enable the track changes for word document using OpenXML Packages
Application Type
Reactive
Service Studio Version
11.55.21 (Build 64159)
Platform Version
11.35.0 (Build 45040)

Hi Team, Currently im enabling the track changes feature for Microsoft word document using the below code, this code working in my local machine but i have created the extension using the same code , it ecncountering the error "Method DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AddNewPart: type argument 'DocumentFormat.OpenXml.Packaging.DocumentSettingsPart' violates the constraint of type parameter 'T'. " kindly help me to resolve. Thanks in advance.


using System;

using System.IO;

using System.Linq;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;


namespace OutSystems.NssOpenXML_Enable_Track_Changes

{

    public class CssOpenXML_Enable_Track_Changes : IssOpenXML_Enable_Track_Changes

    {

        public void MssAction_OpenXML_Enable_Track_Changes(string ssInp_Base64, out string ssProcessed_Base64)

        {

            // Decode the base64 string to a byte array.

            byte[] inputBytes = Convert.FromBase64String(ssInp_Base64);


            // Create a MemoryStream from the byte array.

            using (MemoryStream inputStream = new MemoryStream(inputBytes))

            using (MemoryStream outputStream = new MemoryStream())

            {

                // Open the WordprocessingDocument from the input stream.

                using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))

                {

                    // Get or add the document settings part.

                    DocumentSettingsPart settingsPart;

                    if (document.MainDocumentPart.DocumentSettingsPart == null)

                    {

                        settingsPart = document.MainDocumentPart.AddNewPart();

                        settingsPart.Settings = new Settings();

                    }

                    else

                    {

                        settingsPart = document.MainDocumentPart.DocumentSettingsPart;

                    }


                    // Enable track changes.

                    settingsPart.Settings.Append(new TrackRevisions());


                    // Save changes.

                    document.MainDocumentPart.Document.Save();

                }


                // Write the modified document to the output stream.

                inputStream.Position = 0;

                inputStream.CopyTo(outputStream);


                // Convert the output stream to a base64 string.

                byte[] outputBytes = outputStream.ToArray();

                string base64Output = Convert.ToBase64String(outputBytes);


                ssProcessed_Base64= base64Output;

            }



        }

    }

}



2025-03-12 07-08-15
Nilesh Trivedi

Hi @Muruga Nandan, the error you're encountering is related to how you're adding the DocumentSettingsPart to the document.

Can you please change AddNewPart() to AddNewPart<DocumentSettingsPart>() to properly specify the type. 

Thank you.

2022-02-01 06-08-14
Muruga Nandan

Hi @Nilesh Trivedi, Thanks for the reply. i have paste the latest code, still the issue has encountering 

using System;

using System.Collections;

using System.Data;

using System.IO;

using OutSystems.HubEdition.RuntimePlatform;

using OutSystems.RuntimePublic.Db;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;


namespace OutSystems.NssEnableTrack_Changes {


    public class CssEnableTrack_Changes: IssEnableTrack_Changes {


        /// 


        /// 

        /// 

        /// 

        /// 

        public void MssAction_EnableTrackChanges(string ssInp_Base64, out string ssOut_Base64) {


            // Decode the base64 string to a byte array.

            byte[] inputBytes = Convert.FromBase64String(ssInp_Base64);


            // Create a MemoryStream from the byte array.

            using (MemoryStream inputStream = new MemoryStream(inputBytes))

            using (MemoryStream outputStream = new MemoryStream())

            {

                // Open the WordprocessingDocument from the input stream.

                using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))

                {

                    // Get or add the document settings part.

                    DocumentSettingsPart settingsPart;

                    if (document.MainDocumentPart.DocumentSettingsPart == null)

                    {

                        settingsPart = document.MainDocumentPart.AddNewPart<DocumentSettingsPart>();

                        settingsPart.Settings = new DocumentFormat.OpenXml.Wordprocessing.Settings();

                    }

                    else

                    {

                        settingsPart = document.MainDocumentPart.DocumentSettingsPart;

                    }


                    // Enable track changes.

                    settingsPart.Settings.Append(new TrackRevisions());


                    // Save changes.

                    document.MainDocumentPart.Document.Save();

                }


                // Write the modified document to the output stream.

                inputStream.Position = 0;

                inputStream.CopyTo(outputStream);


                // Convert the output stream to a base64 string.

                byte[] outputBytes = outputStream.ToArray();

                string base64Output = Convert.ToBase64String(outputBytes);


                ssOut_Base64= base64Output;

            }





            // TODO: Write implementation for action

        } // MssAction_EnableTrackChanges



    } // CssEnableTrack_Changes


} // OutSystems.NssEnableTrack_Changes


2023-09-07 20-35-56
Pawan Parmar

Hi @Muruga Nandan 

The method AddNewPart() requires T to be a class derived from OpenXmlPart and also have a public parameterless constructor.This works in older versions of the DocumentFormat.OpenXml library (e.g., v2.5 and earlier). But in later versions (like v2.11+), DocumentSettingsPart no longer has a public parameterless constructor, or the generics constraints are different.

replace
settingsPart = document.MainDocumentPart.AddNewPart();

with

settingsPart = document.MainDocumentPart.AddNewPart("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", "settings");



2022-02-01 06-08-14
Muruga Nandan

Hi @Pawan Parmar, Thanks for the reply i have changed the code, could you please refer the screen shot.it still issue encountering.

Thanks.

2025-03-12 07-08-15
Nilesh Trivedi

Can you please try with the below code.


using System;

using System.IO;

using DocumentFormat.OpenXml.Packaging;

using DocumentFormat.OpenXml.Wordprocessing;


namespace OutSystems.NssOpenXML_Enable_Track_Changes

{

    public class CssOpenXML_Enable_Track_Changes : IssOpenXML_Enable_Track_Changes

    {

        public void MssAction_OpenXML_Enable_Track_Changes(string ssInp_Base64, out string ssProcessed_Base64)

        {

            // Initialize output

            ssProcessed_Base64 = string.Empty;


            try

            {

                // Decode the base64 string to a byte array

                byte[] inputBytes = Convert.FromBase64String(ssInp_Base64);


                using (MemoryStream inputStream = new MemoryStream(inputBytes))

                using (MemoryStream outputStream = new MemoryStream())

                {

                    // Open the WordprocessingDocument

                    using (WordprocessingDocument document = WordprocessingDocument.Open(inputStream, true))

                    {

                        // Get or create the document settings part (corrected)

                        DocumentSettingsPart settingsPart = document.MainDocumentPart.DocumentSettingsPart;

                        

                        if (settingsPart == null)

                        {

                            // Correct way to add a new DocumentSettingsPart

                            settingsPart = document.MainDocumentPart.AddNewPart();

                            settingsPart.Settings = new Settings();

                        }


                        // Enable track changes

                        if (settingsPart.Settings == null)

                        {

                            settingsPart.Settings = new Settings();

                        }


                        // Remove existing TrackRevisions if any

                        var existingTrackRevisions = settingsPart.Settings.Elements().FirstOrDefault();

                        if (existingTrackRevisions != null)

                        {

                            existingTrackRevisions.Remove();

                        }


                        // Add new TrackRevisions

                        settingsPart.Settings.AppendChild(new TrackRevisions() { Val = OnOffValue.FromBoolean(true) });


                        // Save changes

                        settingsPart.Settings.Save();

                        document.MainDocumentPart.Document.Save();

                    }


                    // Reset stream position and copy to output

                    inputStream.Position = 0;

                    inputStream.CopyTo(outputStream);


                    // Convert to base64

                    ssProcessed_Base64 = Convert.ToBase64String(outputStream.ToArray());

                }

            }

            catch (Exception ex)

            {

                // Handle exceptions appropriately

                throw new Exception($"Error enabling track changes: {ex.Message}");

            }

        }

    }

}

2022-02-01 06-08-14
Muruga Nandan

Hi @Nilesh Trivedi, Thank you so much for your reply,I have changed the code and checked now different issue has occuring. Kindly refer the screenshot.

Thanks.

2025-03-12 07-08-15
Nilesh Trivedi

Hi, your error occurs because of version mismatches in the OpenXML SDK. Can you please check the version? The above code requires OpenXML SDK 2.13.0.

Thank you.

2022-02-01 06-08-14
Muruga Nandan


Hi @Nilesh Trivedi, i have installed latest version. can i install this version 2.13.0 ?

2025-03-12 07-08-15
Nilesh Trivedi


Hi @Muruga Nandan, you can try using version 2.13.0. However, before changing the version, I recommend backing up your code, as it might affect other parts of your project. I’ll also check with your latest version (3.3.0), and if I find any solution, I’ll let you know. 

2022-02-01 06-08-14
Muruga Nandan

Sure i will check and let yo know and Thank you so much @Nilesh Trivedi 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.