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


本文將介紹讀取Word批注的方法,包括讀取Word批注中的文本及圖片。關於操作Word批注的方法還可以參考這兩篇文章:Java 添加、回復、修改、刪除Word批注Java 給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);
        }
    }
}

批注圖片讀取結果:

 

(本文完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM