注释用于 word 文档、DOCX或DOC中,用于建议改进和修改。让我们探讨如何使用 Java 以编程方式插入评论以及删除或移除评论。您可以根据需要添加作者姓名、首字母缩写、评论文本、日期和时间。我们将使用Aspose.Words for Java API 执行所有这些任务。
一、在 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 :
存储库:
AsposeJavaAPI Aspose Java API https://repository.aspose.com/repo/
依赖:
com.aspose aspose-words 20.6 jdk17 com.aspose aspose-words 20.7 javadoc
所以 API 现在已经配置好了,我们可以继续探索在 Word 文档中处理评论的不同用例。
三、使用 Java 在现有 Word 文档中插入注释
您可以使用 Aspose.Words for Java API 在现有的 Microsoft Word 文件、DOCX 或 DOC 中插入或添加注释。这在审查文件时很有用,例如主管可以对可行性报告提出多项更改或改进建议。此外,任何拥有 word 文档编辑权限的人都可以使用评论。您需要按照以下步骤在 word 文件 (DOCX/DOC) 中插入注释:
以下代码片段显示了如何使用 Java 在 Word 文档中插入注释:
// Load source word document Document doc = new Document(dataDir + "Comments.docx");// Initialize DocumentBuilder object DocumentBuilder builder = new DocumentBuilder(doc);// Create new comment Comment 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 file doc.save(dataDir + "Comments_Output.docx");
下面的屏幕截图显示了在现有 Word 文档中添加的示例评论:
四、使用 Java 在新的 Word 文档中插入注释
创建新的 Word 文档时,注释也很有用。例如,某些文本可能需要详细说明,这可以在评论的帮助下进行解释。同样,在创建新的 DOCX 文件时,可能会有数百个用例,其中注释可以提供帮助。您可以按照以下步骤轻松添加或插入评论:
下面的代码片段显示了如何使用 Java 在从头开始创建新的 Word 文档时插入注释:
// Initialize new word document Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc);// Add some text builder.write("Some text is added.");// Create new comment Comment 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 file doc.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 document NodeCollection 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 file doc.save(dataDir + "output.docx");
六、使用 Java 删除 Word 文档中的所有评论
Word文档的所有评论都可以一次性删除。您可以按照以下步骤删除所有评论:
以下代码片段详细说明了如何使用 Java 删除 Word 文档中的所有评论:
// Open the document. Document doc = new Document(dataDir + "Comments.docx"); // Collect all comments in the document NodeCollection comments = doc.getChildNodes(com.aspose.words.NodeType.COMMENT, true); // Remove all comments. comments.clear(); doc.save(dataDir + "output.docx");
上一篇:仓库拣货标签——流程卡
下一篇:视图、存储过程、触发器