Hi,
When I compare two Word documents, I notice that some additional links appear. What could be the reason for this, and how can I resolve the issue?
Aspose Word Version : 25.5
Snippet :
byte[] oldDoc = null; // here is the bytes of old document
byte[] newDoc = null; // here is the bytes of new document
CompareOptions compareOptions = new CompareOptions();
Aspose.Words.Document doc1 = new Aspose.Words.Document(new MemoryStream(oldDoc ));
Aspose.Words.Document doc2 = new Aspose.Words.Document(new MemoryStream(newDoc ));
using (MemoryStream ms = new MemoryStream())
{
doc1.Compare(doc2, "Unknown", DateTime.Now, compareOptions);
doc1.Save(ms, Aspose.Words.SaveFormat.Docx);
File.WriteAllBytes("C:\\Temp\\compared.docx", ms.ToArray());
}
Input :
oldDoc.docx (43.1 KB)
newDoc.docx (43.1 KB)
Output :
compared.docx (35.8 KB)
Screenshot for extra hyperlink :
@AlpeshChaudhariDev
When comparing two Word documents, additional hyperlinks may appear due to differences in the documents’ content, such as changes in text, formatting, or embedded links. These hyperlinks can be generated if the comparison detects new or modified links in the new document that were not present in the old document.
To resolve this issue, you can try the following steps:
-
Review the Documents: Check both documents for any unintended hyperlinks or changes that might have been introduced.
-
Adjust CompareOptions: You can customize the CompareOptions
to ignore certain types of changes. For example, if you want to ignore formatting changes, you can set the IgnoreFormatting
property to true
.
Here’s an example of how to adjust the CompareOptions
:
CompareOptions compareOptions = new CompareOptions
{
IgnoreFormatting = true // This will ignore formatting changes
};
- Manual Cleanup: After generating the compared document, you may need to manually remove any unwanted hyperlinks.
If the issue persists, consider consulting the Aspose documentation or community forums for more specific guidance related to your version and scenario.
@AlpeshChaudhariDev I see the highlighted links in the output document only if update fields in the document. But I can see the same links in both input documents if update fields in them. So I do not see any incorrect behavior of Aspose.Words here.
In both of my source documents, the hyperlinks are hidden. So why are hidden hyperlinks appearing in the comparison output?
@AlpeshChaudhariDev They are not hidden, if update fields in the document they become visible.
Okay got it. I want to hide the hyperlinks that were previously hidden. What should I do?
@AlpeshChaudhariDev You can try locking fields to prevent their update. Please see Field.IsLocked property.
Can I do it like - before first get the hidden hyperlinks before comparing, and then hide them again after the comparison?
@AlpeshChaudhariDev Please try using the following code:
Document v1 = new Document(@"C:\Temp\v1.docx");
Document v2 = new Document(@"C:\Temp\v2.docx");
v1.Compare(v2, "AW", DateTime.Now);
// Lock hyperlink fields.
foreach (Field f in v1.Range.Fields)
if (f.Type == FieldType.FieldHyperlink)
f.IsLocked = true;
v1.Save(@"C:\Temp\out.docx");
1 Like
Yes I have used this code. Its working fine. Thanks @alexey.noskov
1 Like