Aspose.Words for .NET使用教程:如何比較兩個Word文檔

Aspose.Words For .Net是一種高級Word文檔處理API,用於執行各類文檔管理和操做任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持全部流行的Word處理文件格式,並容許將Word文檔導出或轉換爲固定佈局文件格式和最經常使用的圖像/多媒體格式。git

【下載Aspose.Words for .NET最新試用版】github

如何比較兩個word文檔


咱們能夠使用Document.Compare方法比較兩個文檔以查看它們之間的區別。此方法模仿Microsoft Word的比較功能,並生成文檔差別做爲許多編輯和格式修訂。主要的想法是,若是咱們拒絕全部修訂,那麼咱們獲得的文件與原始文件相同。相反,若是咱們接受全部修訂,那麼咱們將得到最終(比較目標)文檔。佈局

限制測試

在這裏,有一些廣泛的限制:spa

  • 在調用此方法以前,正在比較的文檔必須沒有修訂。
  • 標記——僅限於SmartTag,其餘標記徹底被忽略。
  • 比較回退形狀,而不是實際的Dml比較。

 

有一條關於「相等」的重要說明:實際上,「相等」在這裏的意思是比較法不能將更改表示爲修訂。通常來講,這意味着文檔文本和文本格式是相同的。可是文檔之間可能還有其餘不一樣之處。例如,Word只支持樣式的格式修改,咱們不能表示樣式的插入/刪除。所以文檔能夠有不一樣的樣式集,而Compare方法仍然不會產生修訂。orm

 

下面的示例顯示了正常的比較用例:資源

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document docA = new Document(dataDir + "TestFile.doc");
Document docB = new Document(dataDir + "TestFile - Copy.doc");
// DocA now contains changes as revisions. 
docA.Compare(docB, "user", DateTime.Now);

 

下面的例子展現瞭如何測試Word文檔是否「相等」:文檔

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document docA = new Document(dataDir + "TestFile.doc");
Document docB = new Document(dataDir + "TestFile - Copy.doc");
// DocA now contains changes as revisions. 
docA.Compare(docB, "user", DateTime.Now);
if (docA.Revisions.Count == 0)
    Console.WriteLine("Documents are equal");
else
    Console.WriteLine("Documents are not equal");

 

比較Word文檔並忽略文檔格式get

在比較文檔時,能夠忽略文檔格式、頁眉-頁腳、字段、腳註、表、文本框、註釋和大小寫更改。設置CompareOptions的值。屬性IgnoreFormatting設置爲true,以忽略文檔格式設置。當CompareOptions時,頁眉和頁腳內容將被忽略。IgnoreHeadersAndFooters被設置爲true。下面的代碼示例顯示瞭如何忽略文檔的格式化和頁眉頁腳內容:it

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document docA = new Document(dataDir + "TestFile.doc");
Document docB = new Document(dataDir + "TestFile - Copy.doc");

CompareOptions options = new CompareOptions();
options.IgnoreFormatting = true;
options.IgnoreHeadersAndFooters = true;
options.IgnoreCaseChanges = true;
options.IgnoreTables = true;
options.IgnoreFields = true;
options.IgnoreComments = true;
options.IgnoreTextboxes = true;
options.IgnoreFootnotes = true;

// DocA now contains changes as revisions. 
docA.Compare(docB, "user", DateTime.Now, options);
if (docA.Revisions.Count == 0)
    Console.WriteLine("Documents are equal");
else
    Console.WriteLine("Documents are not equal");

 

設置比較差別的目標文檔

在「比較文檔」對話框中,MS Word有「顯示更改」選項,比較結果取決於此選項。Target屬性用於此目的。此屬性指定比較期間將哪一個文檔應使用做爲目標。例如,這個選項和IgnoreFormatting設置一塊兒決定了必須使用哪一個文檔做爲相等文本範圍的格式化源。ComparisonTargetType枚舉用於指定將在比較期間使用的基本文檔。下面的代碼示例展現瞭如何爲兩個文檔的比較設置目標文檔:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document docA = new Document(dataDir + "TestFile.doc");
Document docB = new Document(dataDir + "TestFile - Copy.doc");

CompareOptions options = new CompareOptions();
options.IgnoreFormatting = true;
// Relates to Microsoft Word "Show changes in" option in "Compare Documents" dialog box. 
options.Target = ComparisonTargetType.New;

docA.Compare(docB, "user", DateTime.Now, options);

爲你推薦:Aspose專題 - Aspose最新資源合集

相關文章
相關標籤/搜索