Hi,
I’m trying to change the font of a run in a document that has tracked changes enabled. The following code doesn’t work for me unless I call AcceptAllRevisions() before, but I need to retain the revisions. Any ideas?
var document = new Document(@"c:\temp\support\TrackedChangesDoc.docx");
foreach(var run in document.GetChildNodes(NodeType.Run, true).Cast<Run>())
{
Debug.Assert(run.Font.Name == "Aptos");
}
document.RevisionsView = RevisionsView.Final;
foreach (var run in document.GetChildNodes(NodeType.Run, true).Cast<Run>())
{
Debug.Assert(run.Font.Name == "Times New Roman");
}
foreach (var run in document.GetChildNodes(NodeType.Run, true).Cast<Run>())
{
run.Font.Name = "Calibri";
// fails assertion
Debug.Assert(run.Font.Name == "Calibri");
}
@cbowman
When working with a document that has tracked changes enabled, modifying the font of a run can be tricky because the changes are tracked and revisions are marked. If you want to change the font of a run without accepting all revisions, you can follow these steps:
-
Set the Revisions View: Before making any changes, ensure that you set the RevisionsView
to Final
. This allows you to see the document as it would appear if all revisions were accepted, but it does not actually accept the revisions.
-
Modify the Font: After setting the revisions view, you can iterate through the runs in the document and change their font properties.
Here is an example of how you can implement this in C#:
var document = new Document(@"c:\temp\support\TrackedChangesDoc.docx");
// Set the revisions view to Final
document.RevisionsView = RevisionsView.Final;
// Iterate through all runs in the document
foreach (var run in document.GetChildNodes(NodeType.Run, true).Cast<Run>())
{
// Change the font name
run.Font.Name = "Calibri";
// Assert to check if the font name has been changed
Debug.Assert(run.Font.Name == "Calibri");
}
// Save the modified document
document.Save(@"c:\temp\support\ModifiedDoc.docx");
In this code, the RevisionsView
is set to Final
, allowing you to see the document as it would appear with all changes accepted. You can then change the font of each run without losing the tracked changes.
If you still encounter issues, ensure that the document is not in a state that prevents modifications, such as being read-only or having other restrictions.
This approach should help you retain the revisions while still allowing you to change the font of the runs in your document.
The sample code I submitted includes setting the RevisionsView to Final and it doesn’t work as expected. The document is a simple document created for this example and is fully writable and whatnot.
@cbowman
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28166
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
@cbowman Could you please clarify, whether the provided code snippet is the attempt to implement some kind of Track-Format-Changes feature?
Are you looking for something like this:
doc.StartTrackRevisions();
run.Font.Name = "Times New Roman";
// Now run should have format revision in changing font name.
Currently, Aspose.Words is missing functionality to track format changes (only insertion/deletion is tracked)
Hi Alexey,
Thanks for following up. I’m simply trying to change the font of a run in a document that has tracked changes enabled. I have a client that uses tracked changes all the time and I am finding it very difficult to just change the font. The document used in this example is a new document with a single line of text, turning on tracked changes, then changing the font of that single line. Initially, the text is in Atpos, after turning on tracked changes, I change the font to Times New Roman.
Thanks for any help / ideas!
TrackedChangesDoc.docx (14.0 KB)
@cbowman Thank you for additional information. I forwarded the details to our development team. We will keep you updated and let you know once the issue is resolved or we have more information for you .