翻譯|使用教程|編輯:胡濤|2022-12-23 10:24:56.347|閱讀 184 次
概述:本文介紹了如何使用 Java 比較兩個 MS Word (DOC/DOCX) 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
執(zhí)行 MS Word 文檔的比較以了解文檔的兩個版本之間的差異。MS Word 提供了一個內(nèi)置選項來比較兩個文檔的內(nèi)容。然而,隨著文檔數(shù)量的增加,手動進行文檔比較變得困難。為了自動執(zhí)行此過程,本文介紹了如何使用 Java 比較兩個 MS Word (DOC/DOCX) 文檔。
Aspose.Words for Java是一個功能強大的文檔操作 API,可讓您創(chuàng)建、讀取、修改和轉(zhuǎn)換 MS Word 文檔。此外,它允許您比較兩個 Word 文檔,同時考慮或忽略內(nèi)容的格式。您可以下載API 或使用 Maven 配置安裝它。
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>//repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>20.9</version> <classifier>jdk17</classifier> </dependency>
以下是使用 Aspose.Words for Java API 執(zhí)行簡單 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
可能會出現(xiàn)文檔對內(nèi)容應用不同格式的情況。在這種情況下,您更愿意只比較文本而忽略格式、頁眉/頁腳、腳注、表格、注釋等。以下是在比較兩個 MS Word 文檔時忽略文檔格式的步驟。
以下代碼示例顯示了如何使用 Java 比較忽略內(nèi)容格式的 MS Word 文檔。
// 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);
以上便是如何使用 Java 比較 MS Word 文檔 詳細步驟 ,要是您還有其他關于產(chǎn)品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產(chǎn)品
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務必注明出處、不得修改原文相關鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn