Enable Track Changes / Revisions & Replace Text with a Paragraph Break in Word Document C# .NET

Suppose I have the following text:

This is paragraph one.

This is paragraph two.

And I replace one.&pThis with replaced while TrackRevisions is enabled.

When doing the equivalent replacement in Word, you would see “one.”, the paragraph break, and “This” marked as deletions and "replaced marked as an insertion. When doing this in Aspose.Word for .NET v21.5, I get the following:

“This is paragraph one.” as a MovedFrom revision.
“This is paragraph one.” as a Deletion revision.
“This is paragraph replaced” as an Insertion revision.
“This” as a Deletion revision.
“is paragraph two.” as normal text.

If you accept all revisions, the final outcome is correct. However, in our application, it is important to display the effect of the replacement to the end user in tracked changes. The representation in tracked changes here is poor, as it is very confusing and therefore less informative to look at, and very divergent from Word’s behavior.

@ssmolkin1,

I have attached a sample source Word DOCX document containing the two Paragraphs, MS Word 2019 and Aspose.Words for .NET 21.6 generated output DOCX files here for your reference: (see Docs 230469.zip (29.2 KB)).

I produced this “ms word 2019.docx” document by performing the following steps on my end.

  • Open “This is paragraph one.docx” with MS Word 2019
  • Enable Review | Track Changes
  • Press Ctrl+H to bring Find and Replace box
  • Enter one.^pThis in “Find what:” textbox
  • Enter replaced in “Replace with:” texetbox
  • Click “Replace All” button
  • Review | Reviewing Pane | Reviewing Pane Vertical shows the desired behavior
  • Save

Secondly, I produced this “21.6.docx” document by running the following C# code of Aspose.Words for .NET API.

Document doc = new Document("C:\\Temp\\This is paragraph one.docx");
doc.StartTrackRevisions("user");
doc.Range.Replace("one.&pThis", "replaced");
doc.StopTrackRevisions();
doc.Save("C:\\Temp\\21.6.docx");

In this case, the Revisions shown in “Reviewing Pane” are different to what are shown in “ms word 2019.docx” file. I have logged this problem with ID WORDSNET-22321. We will further look into the details of this problem and will keep you updated on the status of correction. Sorry for the inconvenience.

The issues you have found earlier (filed as WORDSNET-22321) have been fixed in this Aspose.Words for .NET 21.7 update and this Aspose.Words for Java 21.7 update.

DeleteEmptyParagraphsMultiLinebreak.docx (11.9 KB)
DeleteEmptyParagraphsMultiLinebreak - updated with revisions.docx (12.0 KB)

This was not fixed in 21.7, it is still broken, now in a new way.

Attached is the original document. On this document, I ran the following operations via Range.Replace while TrackRevisions was enabled:

Replace "delete2&p&p" with ""
Replace "delete&p" with ""
Replace "thisthat" with "other"

The resulting document was attached. Observe that line dividers were added to the document for some unknown reason.

Furthermore, if all revisions are accepted and the resulting document is compared with the original document, the following error occurs in the Document.Compare operation:

 Message: 
    Test method OnD.DocumentOperations.Tests.ReplaceTests.ReferenceTests.ReplaceMultiLineMatchWithFieldSimple threw exception: 
    System.InvalidCastException: Unable to cast object of type 'Aspose.Words.Run' to type 'Aspose.Words.SpecialChar'.

  Stack Trace: 
       .        (Char )
       . (Int32 )
       . (    )
       . ()
       . ()
       .(Node , Node ,     )
       .()
       .(Document , Document , CompareOptions )
    Document.Compare(Document document, String author, DateTime dateTime, CompareOptions options)
    Document.Compare(Document document, String author, DateTime dateTime)

@ssmolkin1,

After an initial test with the licensed latest (21.7) version of Aspose.Words for .NET, we were unable to reproduce this issue on our end. We used the following C# code of Aspose.Words for testing on our end (see output by aspose.words 21.7.docx (9.4 KB)):

Document doc = new Document("C:\\Temp\\DeleteEmptyParagraphsMultiLinebreak.docx");
doc.StartTrackRevisions("user");
doc.Range.Replace("delete2&p&p", "");
doc.Range.Replace("delete&p", "");
doc.Range.Replace("thisthat", "other");
doc.StopTrackRevisions();
doc.Save("C:\\Temp\\output by aspose.words 21.7.docx");

Also, we have manually produced this document (see output by ms word 2019.docx (12.9 KB)) by doing the following steps in MS Word 2019:

  • Open “DeleteEmptyParagraphsMultiLinebreak.docx” with MS Word 2019
  • Enable Review | Track Changes
  • Press Ctrl+H to bring Find and Replace box
  • Enter delete2^p^p in “Find what:” textbox
  • Enter "" in “Replace with:” texetbox
  • Click “Replace All” button
  • Again press Ctrl+H to bring Find and Replace box
  • Enter delete^p in “Find what:” textbox
  • Enter "" in “Replace with:” texetbox
  • Click “Replace All” button
  • Save

On our end, Review | Reviewing Pane | Reviewing Pane Vertical shows three revisions for both above documents.

I’ve tested again, and indeed it appears to be an issue caused due to some other code in our app. Thank you.

@ssmolkin1,

It is great that you were able to find what you were looking for. In case you have further inquiries or may need any help in future, please let us know by posting a new thread in Aspose.Words’ forum.