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;
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.
Hi @Nilesh Trivedi, Thanks for the reply. i have paste the latest code, still the issue has encountering
using System.Collections;
using System.Data;
using OutSystems.HubEdition.RuntimePlatform;
using OutSystems.RuntimePublic.Db;
namespace OutSystems.NssEnableTrack_Changes {
public class CssEnableTrack_Changes: IssEnableTrack_Changes {
///
public void MssAction_EnableTrackChanges(string ssInp_Base64, out string ssOut_Base64) {
settingsPart = document.MainDocumentPart.AddNewPart<DocumentSettingsPart>();
settingsPart.Settings = new DocumentFormat.OpenXml.Wordprocessing.Settings();
ssOut_Base64= base64Output;
// TODO: Write implementation for action
} // MssAction_EnableTrackChanges
} // CssEnableTrack_Changes
} // OutSystems.NssEnableTrack_Changes
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.
replacesettingsPart = document.MainDocumentPart.AddNewPart();
with
settingsPart = document.MainDocumentPart.AddNewPart("application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml", "settings");
Hi @Pawan Parmar, Thanks for the reply i have changed the code, could you please refer the screen shot.it still issue encountering.Thanks.
Can you please try with the below code.
// Initialize output
ssProcessed_Base64 = string.Empty;
try
// Decode the base64 string to a byte array
// Open the WordprocessingDocument
// Get or create the document settings part (corrected)
DocumentSettingsPart settingsPart = document.MainDocumentPart.DocumentSettingsPart;
if (settingsPart == null)
// Correct way to add a new DocumentSettingsPart
// Enable track changes
if (settingsPart.Settings == null)
// 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();
// Reset stream position and copy to output
// Convert to base64
ssProcessed_Base64 = Convert.ToBase64String(outputStream.ToArray());
catch (Exception ex)
// Handle exceptions appropriately
throw new Exception($"Error enabling track changes: {ex.Message}");
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.
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.
Hi @Nilesh Trivedi, i have installed latest version. can i install this version 2.13.0 ?
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.
Sure i will check and let yo know and Thank you so much @Nilesh Trivedi