翻譯|使用教程|編輯:胡濤|2023-02-07 11:33:35.507|閱讀 199 次
概述:本文主要介紹aspose如何使用 Java 在 Word (DOCX/DOC) 中插入或刪除注釋,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words 是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,
Aspose API支持流行文件格式處理,并允許將各類文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
注釋用于 word 文檔、DOCX或DOC中,用于建議改進和修改。讓我們探討如何使用 Java 以編程方式插入評論以及刪除或移除評論。您可以根據需要添加作者姓名、首字母縮寫、評論文本、日期和時間。我們將使用Aspose.Words for Java API 執行所有這些任務。
我們需要安裝 Aspose.Words for C++ API 來轉換 Microsoft Word (DOCX/DOC) 文件。您可以輕松地從NuGet庫安裝 API或在控制臺上使用以下命令安裝它。
Install-Package Aspose.Words.Cpp -Version 20.8.0
您可以根據下面提到的配置從下載部分或 Maven 存儲庫下載最新版本的 Aspose.Words for Java API :
存儲庫:
<repositories> <repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>//repository.aspose.com/repo/</url> </repository> </repositories>
依賴:
<dependencies> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>20.6</version> <classifier>jdk17</classifier> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>20.7</version> <classifier>javadoc</classifier> </dependency> </dependencies>
所以 API 現在已經配置好了,我們可以繼續探索在 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");
以上便是使用 Java 在 Word (DOCX/DOC) 中插入或刪除注釋 ,要是您還有其他關于產品方面的問題,歡迎咨詢我們,或者加入我們官方技術交流群。
歡迎下載|體驗更多Aspose產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn