Word Comparison feasibility in Aspose word for C#

Hi Team,

We are looking for an advanced word comparison functionality. We would like to know if Aspose word have a comparison functionality in word that compares two files and highlight the differences in one file with track changes. Kindly let us know if this is feasible in Aspose word and If yes kindly share a sample code functionality.

Thanks,
Karthikeyan

@Karthik_Test_account Yes, Aspose.Words supports document compare feature. Please see our documentation to learn more about Compare Documents feature.

@alexey.noskov - Is it possible to compare two PDF with Aspose word dot net ?

@Karthik_Test_account Generally it is possible. However, you should note that Aspose.Words is designed to work with MS Word documents. MS Word documents are flow documents and they have structure very similar to Aspose.Words Document Object Model. But on the other hand PDF documents are fixed page format documents. Although PDF document might look the same visually, their structure might be different, that might lead into the difference in the DOM build by Aspose.Words and as a result Aspose.Words might detect the differences in visually the same PDF documents.

Also, you should note that while loading PDF document, Aspose.Words converts Fixed Page Document structure into the Flow Document Object Model. Unfortunately, such conversion does not guaranty 100% fidelity. I am afraid there is no way to 100% preserve PDF document Layout after PDF->Aspose.Words DOM->PDF roundtrip.

@alexey.noskov - I know there is a logic to find and replace content at document level but can we do the same at para level with the bookmark name of the para? can you please share a code snippet for that please

@Karthik_Test_account Sure, you can perform replace action on any node in the document. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Replace only in the first paragraph.
doc.FirstSection.Body.FirstParagraph.Range.Replace("find", "replace");

doc.Save(@"C:\Temp\out.docx");

Thank you for the reply.

I would also like to know if we can do the same with the bookmark name of a paragraph, because in my code I use bookmark name to uniquely identify a paragraph so it would be better if I can identify a para by bookmark and then do the replace functionality. Can you please help me with this?

@Karthik_Test_account Bookmark is usually is a child of paragraph, so you can use code like this to get parent paragraph of the bookmark:

// Get paragraph by bookmark.
Paragraph paragraph = doc.Range.Bookmarks["my_bookmark"].BookmarkStart.ParentNode as Paragraph;

@alexey.noskov - How do we insert a bookmark programmatically for a para by selecting the entire text of the para?

@Karthik_Test_account A bookmark in MS Word document consist of BookmarkStart and BookmarkEnd nodes. So to wrap whole content of a paragraph into a bookmark, you should add BookmarkStart at the beginning of the paragraph and BookmarkEnd at its end. For example see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get some paragraph.
Paragraph p = doc.FirstSection.Body.FirstParagraph;

// wrap whole paragraph into the bookmark.
string bkName = "my_bookmark";
p.PrependChild(new BookmarkStart(doc, bkName));
p.AppendChild(new BookmarkEnd(doc, bkName));

doc.Save(@"C:\Temp\out.docx");