文檔金喜正規買球>>Spire.Doc系列教程>>Spire.Doc功能演示:在 Java 中獲取 Word 中特定注釋的標記文本
Spire.Doc功能演示:在 Java 中獲取 Word 中特定注釋的標記文本
Spire.Doc for Java 是一款專業的Java Word組件,開發人員使用它可以輕松地將Word文檔創建、讀取、編輯、轉換和打印等功能集成到自己的Java應用程序中。
在本文中,我們將向您展示如何使用 Spire.Doc for Java 在 Word 中獲取特定評論的標記文本。 可點擊此處下載最新版測試。
輸入文件

import com.spire.doc.Document; import com.spire.doc.documents.CommentMark; import com.spire.doc.documents.Paragraph; import com.spire.doc.fields.Comment; import com.spire.doc.fields.TextRange; public class GetMarkedTextOfSpecificComment { public static void main(String[] args){ //Load the Word document Document doc = new Document(); doc.loadFromFile("Input.docx"); //Get the first comment in the document Comment comment = doc.getComments().get(0); //Get the start mark and end mark of the comment Paragraph para = comment.getOwnerParagraph(); CommentMark start = comment.getCommentMarkStart(); CommentMark end = comment.getCommentMarkEnd(); //Get the index of the comment start mark and the comment end mark int indexOfStart = para.getChildObjects().indexOf(start); int indexOfEnd = para.getChildObjects().indexOf(end); String markedText = ""; //Get the marked text between the comment start mark and comment end mark according to the index for (int i = indexOfStart + 1; i < indexOfEnd; i++) { if (para.getChildObjects().get(i) instanceof TextRange) { TextRange range = (TextRange) para.getChildObjects().get(i); markedText += range.getText(); } } //Print out the marked text System.out.println(markedText); } }
輸出結果

Spire.Doc for Java 4.7.0已發布。該版本增強了轉換Word/RTF到PDF,以及轉換HTML到Word的功能。此外,本次更新還修復了加載和保存Word文檔等時出現的問題。
整合所有格式API處理套包Spire.office for Java正在慧都網火熱銷售中!立馬1分鐘了解全部咨詢!