Java版Word開發工具Aspose.Words功能解析:在Word(DOCX / DOC)中插入或刪除注釋
注釋用于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 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文件時提供幫助。您可以按照以下步驟輕松添加或插入評論:
- 初始化DocumentBuilder對象
- 添加示例文字
- 創建自定義注解
- 保存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文檔的所有注釋都可以立即刪除。您可以按照以下步驟刪除所有評論:
- 打開Word docx文件
- 收集文件中的所有注解
- 刪除所有注解
以下代碼片段詳細說明了如何使用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");
還想要更多嗎?您可以點擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術交流群(642018183),我們很高興為您提供查詢和咨詢。