比较MS Word文档是为了了解文档的两个版本之间的差异。MS Word提供了一个内置选项来比较两个文档的内容。但是,随着文档数量的增加,手动进行文档比较变得困难。
>>如果想要测试这项新功能,可点击这里下载最新版试用。(安装包仅提供部分功能,并设置限制,如需试用完整功能请申请免费授权。)
- 使用Java比较MS Word文档
- 在MS Word文档比较中忽略格式
- 设置目标文档以进行比较差异
- 设置粒度以比较MS Word文档
IT行业爆品采购节来啦!整合所有格式的Aspose.Total永久授权正在火热促销中,乐享85折起!联系客服立马1分钟了解全部咨询!
比较MS Word文档的Java API
Aspose.Words for Java是功能强大的文档处理API,可以创建,读取,修改和转换MS Word文档。此外,它还允许比较两个Word文档以及考虑或忽略内容的格式。可以下载API或使用以下Maven配置进行安装。

使用Java比较MS Word文档
以下是使用Aspose.Words for Java API执行简单Word文档比较的步骤。
- 使用Document类加载要比较的Word文档。
- 调用Document.compare(Document,String,Date)方法将文档与作为参数传递的文档进行比较。
下面的代码示例演示如何使用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
在Word文档比较中忽略格式
在某些情况下,文档具有适用于内容的不同格式。在这种情况下,您宁愿只比较文本,而忽略格式,页眉/页脚,脚注,表格,注释等。下面是比较两个MS Word文档时忽略文档格式的步骤。
- 使用Document类加载要比较的Word文档。
- 创建一个CompareOptions类的对象。
- 设置CompareOptions.setIgnoreFormatting(true)。
- 使用Document.compare(Document,String,Date,CompareOptions)方法比较文档。
下面的代码示例演示如何比较MS Word文档,而忽略使用Java的内容格式。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-JavaString 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");
设置目标Word文档以进行比较差异
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);
设置粒度以比较Word文档
可以在比较两个MS Word文档时设置更改的粒度。这可以使用CompareOptions.setGranularity()属性完成。以下是可能的粒度选项。
- CHAR_LEVEL
- WORD_LEVEL
以下代码示例显示了使用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技术交流群(),我们很高兴为您提供查询和咨询。

标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!