Revision Tracking

Hi,

we are trying to generate a word document with revisions. We want keep original version of the text and to track what user has added/deleted.

Our plan is to store every user actions, e.g. User A inserted text “add text” to index 15, User B deleted 10 character from index 30.

I tried to use this code below to do insertion and track change, but it delete the whole paragraph and add a new paragraph. How do I just track inserting the additional text without deleting the whole paragraph?

original.txt:

this is paragraph 1

this is paragraph 2

this is paragraph 3

Sample code:

string originalTextPath = "original.txt";
string originalText = File.ReadAllText(originalTextPath);
doc.TrackRevisions = true;
doc.StartTrackRevisions(author, date);

var builder = new DocumentBuilder(doc);
builder.MoveToDocumentStart();
int current = 0;
int currentPara = 0;
int index = 10;
string text = "inserted text";
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string paraText = para.ToString(SaveFormat.Text);
    if (index <= current + paraText.Length)
    {
        int offset = index - current;
        builder.MoveToParagraph(currentPara, offset);
        builder.Write(text);
        break;
    }
    current += paraText.Length;
    currentPara += 1;
}
doc.StopTrackRevisions();
doc.Save(outputPath);

output-with-revisions.docx (7.1 KB)

@ryanwidjaya

To track changes in a Word document without deleting the entire paragraph, you can use the DocumentBuilder to insert text at a specific index while tracking revisions. Here’s a clear approach to achieve this:

Cause: You want to modify a Word document by inserting text at a specific position while keeping track of the changes made.

Solution: You can utilize the DocumentBuilder class to insert text at the desired index while tracking revisions. The following steps outline the process:

  1. Start tracking revisions before making any changes.
  2. Use the DocumentBuilder to navigate to the desired position and insert the text.
  3. Stop tracking revisions after the changes are made.

Here’s an example of how to implement this:

string originalTextPath = "original.txt";
string originalText = File.ReadAllText(originalTextPath);
Document doc = new Document();
doc.TrackRevisions = true;
doc.StartTrackRevisions("Author", DateTime.Now);

// Load the original text into the document
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln(originalText);

// Move to the specific index where you want to insert text
builder.MoveToDocumentStart(); // Move to the start of the document
builder.MoveToParagraph(0, 0); // Move to the first paragraph

// Insert text at the desired index
builder.InsertText("add text ", 15); // Adjust the index as needed

doc.StopTrackRevisions();
doc.Save("output.docx");

In this code, the InsertText method is used to add text at a specific position without deleting the existing content. Ensure to adjust the index based on your requirements. This method allows you to effectively track all insertions and deletions.

Next Steps: Implement the provided code in your project, adjusting the index as necessary for your specific use case. Test the functionality to ensure that revisions are tracked correctly.

If you have any further questions or need additional assistance, feel free to ask!

I couldn’t find this builder.InsertText method

I would also like to know if there is any method to delete texts from index, e.g. delete 10 characters from index 30.

@ryanwidjaya You can modify your code like this to get the expected output:

Document doc =new Document(@"C:\Temp\original.txt");
DocumentBuilder builder = new DocumentBuilder(doc);
            
int current = 0;
int currentPara = 0;
int index = 10;
string text = "inserted text";
foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string paraText = para.ToString(SaveFormat.Text);
    if (index <= current + paraText.Length)
    {
        int offset = index - current;
        builder.MoveToParagraph(currentPara, offset);
        // Force Aspose.Words to split the runs in the paragraph at the current position.
        Run splitter = new Run(doc);
        builder.InsertNode(splitter);
        splitter.Remove();
        // Start tracking changes and insert the text.
        doc.StartTrackRevisions("AW", DateTime.Now);
        builder.Write(text);
        doc.StopTrackRevisions();
        break;
    }
    current += paraText.Length;
    currentPara += 1;
}
            
doc.Save(@"C:\Temp\out.docx");

out.docx (6.6 KB)

1 Like

Updated with the splitter and it works perfectly now for the insertion.

I also managed to delete texts by the position index with revision by inserting a splitter at the start of the position to delete, add another splitter at the end of the position to delete, and delete all runs in between with track revisions.

Thank you so much for your help.

@ryanwidjaya It is perfect that you managed to achieve what you need.