翻譯|使用教程|編輯:李顯亮|2020-10-12 10:36:20.537|閱讀 1252 次
概述:比較MS Word文檔是為了了解文檔的兩個版本之間的差異。為了使此過程自動化,本文介紹了如何使用Java比較兩個MS Word(DOC / DOCX)文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
比較MS Word文檔是為了了解文檔的兩個版本之間的差異。MS Word提供了一個內置選項來比較兩個文檔的內容。但是,隨著文檔數量的增加,手動進行文檔比較變得困難。
為了使此過程自動化,本文介紹了如何使用Java比較兩個MS Word(DOC / DOCX)文檔。
>>如果想要測試這項新功能,可點擊這里下載最新版試用。(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
Aspose.Words for Java是功能強大的文檔處理API,可以創建,讀取,修改和轉換MS Word文檔。此外,它還允許比較兩個Word文檔以及考慮或忽略內容的格式。可以下載API或使用以下Maven配置進行安裝。
以下是使用Aspose.Words for Java API執行簡單Word文檔比較的步驟。
下面的代碼示例演示如何使用Java比較兩個MS Word文檔。
Document docA = new Document(dataDir + "DocumentA.doc"); Document docB = new Document(dataDir + "DocumentB.doc"); docA.compare(docB, "user", new Date()); // docA now contains changes as revisions
在某些情況下,文檔具有適用于內容的不同格式。在這種情況下,您寧愿只比較文本,而忽略格式,頁眉/頁腳,腳注,表格,注釋等。下面是比較兩個MS Word文檔時忽略文檔格式的步驟。
下面的代碼示例演示如何比較MS Word文檔,而忽略使用Java的內容格式。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-Java String dataDir = Utils.getDataDir(CompareTwoWordDocumentswithCompareOptions.class); com.aspose.words.Document docA = new com.aspose.words.Document(dataDir + "DocumentA.doc"); com.aspose.words.Document docB = new com.aspose.words.Document(dataDir + "DocumentB.doc"); com.aspose.words.CompareOptions options = new com.aspose.words.CompareOptions(); options.setIgnoreFormatting(true); options.setIgnoreHeadersAndFooters(true); options.setIgnoreCaseChanges(true); options.setIgnoreTables(true); options.setIgnoreFields(true); options.setIgnoreComments(true); options.setIgnoreTextboxes(true); options.setIgnoreFootnotes(true); // DocA now contains changes as revisions. docA.compare(docB, "user", new Date(), options); if (docA.getRevisions().getCount() == 0) System.out.println("Documents are equal"); else System.out.println("Documents are not equal");
Aspose.Words for Java還允許您在比較過程中指定源Word文檔。為此,您可以使用與MS Word的“顯示更改”選項相關的CompareOptions.setTarget()屬性。下面的代碼示例演示如何在比較中指定目標文檔。
Document docA = new Document(dataDir + "TestFile.doc"); Document docB = new Document(dataDir + "TestFile - Copy.doc"); CompareOptions options = new CompareOptions(); options.setIgnoreFormatting(true); // Relates to Microsoft Word "Show changes in" option in "Compare Documents" dialog box. options.setTarget(ComparisonTargetType.NEW); docA.compare(docB, "user", new Date(), options);
可以在比較兩個MS Word文檔時設置更改的粒度。這可以使用CompareOptions.setGranularity()屬性完成。以下是可能的粒度選項。
以下代碼示例顯示了使用Java比較MS Word文檔時如何設置粒度。
DocumentBuilder builderA = new DocumentBuilder(new Document()); DocumentBuilder builderB = new DocumentBuilder(new Document()); builderA.writeln("This is A simple word"); builderB.writeln("This is B simple words"); CompareOptions co = new CompareOptions(); co.setGranularity(Granularity.CHAR_LEVEL); builderA.getDocument().compare(builderB.getDocument(), "author", new Date(), co);
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn