2021-05-12

Java 获取Word中指定图片的坐标位置

本文介绍通过Java程序获取Word文档中指定图片的坐标位置。

程序运行环境:

  • Word测试文档:.docx 2013
  • Free Spire.doc.jar 3.9.0
  • IntelliJ IDEA
  • JDK 1.8.0

方法步骤:

1. 指定文件路径,本次测试代码路径为项目文件夹路径。即在IDEA项目文件下存入用于测试的Word文档,如:C:\Users\Administrator\IdeaProjects\Picture_Doc\input.docx。文件路径也可自定义为其他路径。

2. 在程序中引入jar文件,如下图:

 

3.Java程序代码

import com.spire.doc.*;import com.spire.doc.documents.DocumentObjectType;import com.spire.doc.documents.Paragraph;import com.spire.doc.fields.DocPicture;public class GetCoordinatesOfPicture { public static void main(String[] args) {  //加载Word测试文档  Document doc = new Document();  doc.loadFromFile("input.docx");  //遍历section  for (int a = 0; a<doc.getSections().getCount();a++)  {   Section section = doc.getSections().get(a);   //遍历paragraph段落   for (int b =0 ;b<section.getParagraphs().getCount();b++)   {    Paragraph paragraph = section.getParagraphs().get(b);    //遍历段落中的对象    for (int i = 0; i < paragraph.getChildObjects().getCount(); i++)    {     DocumentObject docobj = paragraph.getChildObjects().get(i);     //判断对象是否为图片     if (docobj.getDocumentObjectType()== DocumentObjectType.Picture)     {      DocPicture picture = (DocPicture) docobj ;      if (picture.getTitle().equals("图片4"))//定位标题为"图片4"的图片      {       //获取图片坐标位置       float x = picture.getHorizontalPosition();       float y = picture.getVerticalPosition();       System.out.println("坐标位置为:\n X=" + x + " Y=" + y);      }     }    }   }  } }}

坐标获取结果:

 

原创文章,如需转载请务必注明出处!

 









原文转载:http://www.shaoqun.com/a/737775.html

跨境电商:https://www.ikjzd.com/

笨鸟海淘:https://www.ikjzd.com/w/1550

reverb:https://www.ikjzd.com/w/1273


本文介绍通过Java程序获取Word文档中指定图片的坐标位置。程序运行环境:Word测试文档:.docx2013FreeSpire.doc.jar3.9.0IntelliJIDEAJDK1.8.0方法步骤:1.指定文件路径,本次测试代码路径为项目文件夹路径。即在IDEA项目文件下存入用于测试的Word文档,如:C:\Users\Administrator\IdeaProjects\Picture_
square:https://www.ikjzd.com/w/2106
虚拟信用卡:https://www.ikjzd.com/w/1055
picitup:https://www.ikjzd.com/w/446
亚马逊高阶运营技巧之广告关键词匹配方式:https://www.ikjzd.com/tl/108333
女友太漂亮总被骚扰怎么办:http://lady.shaoqun.com/a/270997.html
最后通牒:英国或于4月12日无协议脱欧,拖欧再无可能?:https://www.ikjzd.com/home/21362

No comments:

Post a Comment