原創|使用教程|編輯:李顯亮|2020-08-13 10:01:09.540|閱讀 555 次
概述:在本文中,我們將探討如何使用Java以編程方式插入注釋以及刪除或刪除注釋。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
注釋用于Word文檔DOCX或DOC中,以建議改進和修改。讓我們探討如何使用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文檔中添加的示例注釋:
創建新的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文檔上添加注釋的輸出:
將建議的改進或修改合并到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");
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");
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn