Java 讀取Word批註中的文本和圖片

本文將介紹讀取Word批註的方法,包括讀取Word批註中的文本及圖片。關於操作Word批註的方法還可以參考這兩篇文章:下面將通過Java代碼來演示如何讀取批註。

工具使用: Word類庫(Free Spire.Doc for Java免費版

Jar 文件獲取:可通過官網,下載後解壓文件,並將lib文件夾下的Spire.Doc.jar文件導入java程序;也可以通過Maven倉庫安裝導入,具體路徑配置及導入方法可以參考

測試文檔如下:批註中包含文本和圖片

 

 

 

【示例1 讀取批註中的文本

import com.spire.doc.* ;
 import com.spire.doc.documents.Paragraph;
 import com.spire.doc.fields.Comment;
 import com.spire.doc.fields.TextRange;

public  class ReadComment {
     public  static  void main(String[] args) {
         // 加載測試文檔 
        Document doc = new Document();
        doc.loadFromFile( "sample.docx" );

        // 實例化String類型變量 
        String text = "" ;

        // 遍歷所有批註
        for ( int i = 0;i< doc.getComments().getCount();i++ ){
            Comment comment = doc.getComments().get(i);
             // 遍歷所有批註中的段落
            for ( int j= 0;j < comment.getBody().getParagraphs().getCount();j++ ) {
                Paragraph paragraph = comment.getBody().getParagraphs().get(j);
                 // 遍歷段落中的對象
                for (Object object : paragraph.getChildObjects()) {
                     // 讀取文本
                    if (object instanceof TextRange) {
                        TextRange textRange = (TextRange) object;
                        text = text + textRange.getText();
                    }
                }
            }
        }
        // 輸入文本內容
        System.out.println(text);
    }
}

批註文本讀取結果:

 

【示例2 讀取批註中的圖片

import com.spire.doc.* ;
 import com.spire.doc.documents.Paragraph;
 import com.spire.doc.fields.Comment;
 import com.spire.doc.fields.DocPicture;

import javax.imageio.ImageIO;
 import java.awt.image.RenderedImage;
 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;


public  class ExtractImgsInComment {
     public  static  void main(String[] args) throws IOException{
         // 加載測試文檔 
        Document doc = new Document();
        doc.loadFromFile( "sample.docx" );

        // 創建ArrayList數組對象 
        ArrayList images = new ArrayList();

        // 遍歷所有批註
        for ( int i = 0;i< doc.getComments().getCount();i++ ){
            Comment comment = doc.getComments().get(i);
             // 遍歷所有批註中的段落
            for ( int j= 0;j < comment.getBody().getParagraphs().getCount();j++ ) {
                Paragraph paragraph = comment.getBody().getParagraphs().get(j);
                 // 遍歷段落中的對象
                for (Object object : paragraph.getChildObjects()) {
                     // 獲取圖片對象
                    if (object instanceof DocPicture){
                        DocPicture picture = (DocPicture) object;
                        images.add(picture.getImage());
                    }
                }
            }
        }
        // 提取圖片,並指定圖片格式
        for ( int z = 0; z< images.size(); z++ ) {
            File file = new File(String.format("圖片-%d.png" , z));
            ImageIO.write((RenderedImage) images.get(z), "PNG" , file);
        }
    }
}

批註圖片讀取結果:

 

(本文完)

 

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

USB CONNECTOR掌控什麼技術要點? 帶您認識其相關發展及效能

※評比前十大台北網頁設計台北網站設計公司知名案例作品心得分享

※智慧手機時代的來臨,RWD網頁設計已成為網頁設計推薦首選

台灣海運大陸貨務運送流程

兩岸物流進出口一站式服務