Home >Java >javaTutorial >How to read text and pictures in Word comments in Java
This article will introduce how to read Word comments, including reading text and pictures in Word comments. The following will demonstrate how to read annotations through Java code.
Tool usage: Word library (Free Spire.Doc for Java free version)
Jar file acquisition: can be downloaded from the official website, and unzip the file after downloading , and import the Spire.Doc.jar file in the lib folder into the java program; it can also be installed and imported through the Maven repository. For specific path configuration and import methods, please refer to the tutorial https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html.
(Recommended learning: Java video tutorial)
The test document is as follows: Annotation in progress Contains text and images
[Example 1] Read the text in the annotation
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); } }
Annotation text reading result:
[Example 2] Read the image in the annotation
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); } } }
Annotation image reading result:
This article comes from php Chinese website, java tutorial column, welcome to learn!
The above is the detailed content of How to read text and pictures in Word comments in Java. For more information, please follow other related articles on the PHP Chinese website!