Word处理控件Aspose.Words功能演示:使用 Java 在 Word (DOCX/DOC) 中插入或删除注释

一、在 Word 文件 (DOCX/DOC) API 中插入或删除评论 – 安装

我们需要安装 Aspose.Words for C++ API 来转换 Microsoft Word (DOCX/DOC) 文件。您可以轻松地从NuGet库安装 API或在控制台上使用以下命令安装它。

Install-Package Aspose.Words.Cpp -Version 20.8.0
二、使用 C++ 将 Word (DOCX/DOC) 转换为 HTML

您可以根据下面提到的配置从下载部分或 Maven 存储库下载最新版本的 Aspose.Words for Java API :

存储库:

<repositories><repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://repository.aspose.com/repo/</url></repository></repositories>

依赖:

<dependencies><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>20.6</version><classifier>jdk17</classifier></dependency><dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>20.7</version><classifier>javadoc</classifier></dependency></dependencies>

所以 API 现在已经配置好了,我们可以继续探索在 Word 文档中处理评论的不同用例。

三、使用 Java 在现有 Word 文档中插入注释
  1. 使用 Document 类加载现有的 DOCX 文件
  2. 创建评论
  3. 保存 DOCX 文件

以下代码片段显示了如何使用 Java 在 Word 文档中插入注释:

// Load source word documentDocument doc = new Document(dataDir + "Comments.docx");// Initialize DocumentBuilder objectDocumentBuilder builder = new DocumentBuilder(doc);// Create new commentComment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());builder.getCurrentParagraph().appendChild(comment);comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));// Save output filedoc.save(dataDir + "Comments_Output.docx");

下面的屏幕截图显示了在现有 Word 文档中添加的示例评论:

在word中插入评论
四、使用 Java 在新的 Word 文档中插入注释

创建新的 Word 文档时,注释也很有用。例如,某些文本可能需要详细说明,这可以在评论的帮助下进行解释。同样,在创建新的 DOCX 文件时,可能会有数百个用例,其中注释可以提供帮助。您可以按照以下步骤轻松添加或插入评论:

  1. 初始化 DocumentBuilder 对象
  2. 添加示例文本
  3. 创建自定义评论
  4. 保存 DOCX 文件

下面的代码片段显示了如何使用 Java 在从头开始创建新的 Word 文档时插入注释:

// Initialize new word documentDocument doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);// Add some textbuilder.write("Some text is added.");// Create new commentComment comment = new Comment(doc, "Aspose", "Initials", new java.util.Date());builder.getCurrentParagraph().appendChild(comment);comment.getParagraphs().add(new com.aspose.words.Paragraph(doc));comment.getFirstParagraph().getRuns().add(new com.aspose.words.Run(doc, "Sample Comment"));// Save output DOCX filedoc.save(dataDir + "Comments_Output.docx");

下面的屏幕截图显示了在新 word 文档上添加评论的输出:

删除word中的评论
五、使用 Java 从 Word 文档中删除特定评论

当将建议的改进或修改合并到 word 文档中时,注释通常会被删除。当您需要删除特定评论时,您可以按照以下步骤操作:

  1. 加载源word文档

下面的代码片段显示了如何使用 Java 从 word 文件中删除特定评论:

// Open the document.Document doc = new Document(dataDir + "Comments.docx");String authorName = "Aspose";// Collect all comments in the documentNodeCollection comments = doc.getChildNodes(NodeType.COMMENT, true);// Look through all comments and remove those written by the Aspose author.for (int i = comments.getCount() - 1; i >= 0; i--) {Comment comment = (Comment) comments.get(i);if (comment.getAuthor().equals(authorName))comment.remove();}// Save output DOCX filedoc.save(dataDir + "output.docx");
六、使用 Java 删除 Word 文档中的所有评论

Word文档的所有评论都可以一次性删除。您可以按照以下步骤删除所有评论:

  1. 打开word docx文件
  2. 收集文件中的所有评论
  3. 删除所有评论

以下代码片段详细说明了如何使用 Java 删除 Word 文档中的所有评论:

// Open the document.Document doc = new Document(dataDir + "Comments.docx");// Collect all comments in the documentNodeCollection comments = doc.getChildNodes(com.aspose.words.NodeType.COMMENT, true);// Remove all comments.comments.clear();doc.save(dataDir + "output.docx");

以上便是使用 Java 在 Word (DOCX/DOC) 中插入或删除注释,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。


欢迎下载|体验更多Aspose产品

点此获取更多Aspose产品信息 或 加入Aspose技术交流群(
标签:

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

上一篇 2023年1月4日
下一篇 2023年1月4日

相关推荐

发表回复

登录后才能评论