>>如果想要测试这项新功能,可点击这里下载最新版试用。
- 使用Java在现有Word文档中插入注释
- 使用Java在新Word文档中插入注释
- 使用Java从Word文档中删除特定注释
- 使用Java从Word文档中删除所有注释
①使用Java在现有Word文档中插入注释
- 使用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文档中插入注释
创建新的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文档中删除特定注释
将建议的改进或修改合并到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进行处理,非常感谢!