Java版Word开发工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或删除注释

在本文中,我们将探讨如何使用Java以编程方式插入注释以及删除或删除注释。

注释用于Word文档DOCX或DOC中,以建议改进和修改。让我们探讨如何使用Java以编程方式插入注释以及删除或删除注释。您可以根据需要添加作者姓名,缩写,注释文本,日期和时间。

在本文中,将学习以下与Word文档中的注释相关的用例:

>>如果想要测试这项新功能,可点击这里下载最新版试用。

  • 使用Java在现有Word文档中插入注释
  • 使用Java在新Word文档中插入注释
  • 使用Java从Word文档中删除特定注释
  • 使用Java从Word文档中删除所有注释

①使用Java在现有Word文档中插入注释

可以使用Aspose.Words for Java API在现有的Microsoft Word文件DOCX或DOC中插入或添加注释。这在审查文档时可能会有所帮助,例如主管可以对可行性报告提出一些变更或改进建议。此外,具有word文档编辑权限的任何人都可以使用注释。您需要按照以下步骤在Word文件(DOCX / DOC)中插入注释:

  • 使用Document类加载现有的DOCX文件
  • 建立注解
  • 保存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文档中添加的示例注释:

Java版Word开发工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或删除注释

②使用Java在新的Word文档中插入注释

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

  • 初始化DocumentBuilder对象
  • 添加示例文字
  • 创建自定义注解
  • 保存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文档上添加注释的输出:

Java版Word开发工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或删除注释

③使用Java从Word文档中删除特定注释

将建议的改进或修改合并到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文档的所有注释都可以立即删除。您可以按照以下步骤删除所有评论:

  • 打开Word docx文件
  • 收集文件中的所有注解
  • 删除所有注解

以下代码片段详细说明了如何使用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");

还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群,我们很高兴为您提供查询和咨询
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年7月10日
下一篇 2020年7月10日

相关推荐

发表回复

登录后才能评论