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

1610阅读 0评论2021-05-12 fhadmin
分类:Java


点击(此处)折叠或打开

  1. import com.spire.doc.*;
  2. import com.spire.doc.documents.DocumentObjectType;
  3. import com.spire.doc.documents.Paragraph;
  4. import com.spire.doc.fields.DocPicture;

  5. //java项目www fhadmin org
  6. public class GetCoordinatesOfPicture {
  7.     public static void main(String[] args) {
  8.         //加载Word测试文档
  9.         Document doc = new Document();
  10.         doc.loadFromFile("input.docx");

  11.         //遍历section
  12.         for (int a = 0; a<doc.getSections().getCount();a++)
  13.         {
  14.             Section section = doc.getSections().get(a);

  15.             //遍历paragraph段落
  16.             for (int b =0 ;b<section.getParagraphs().getCount();b++)
  17.             {
  18.                 Paragraph paragraph = section.getParagraphs().get(b);

  19.                 //遍历段落中的对象
  20.                 for (int i = 0; i < paragraph.getChildObjects().getCount(); i++)
  21.                 {
  22.                     DocumentObject docobj = paragraph.getChildObjects().get(i);

  23.                     //判断对象是否为图片
  24.                     if (docobj.getDocumentObjectType()== DocumentObjectType.Picture)
  25.                     {
  26.                         DocPicture picture = (DocPicture) docobj ;

  27.                         if (picture.getTitle().equals("图片4"))//定位标题为“图片4”的图片
  28.                         {
  29.                             //获取图片坐标位置
  30.                             float x = picture.getHorizontalPosition();
  31.                             float y = picture.getVerticalPosition();
  32.                             System.out.println("坐标位置为:\n X=" + x + " Y=" + y);
  33.                         }
  34.                     }
  35.                 }
  36.             }
  37.         }

  38.     }
  39. }

上一篇:配置 mysql 表名忽略大小写
下一篇:SpringBoot整合使用阿里注册中心Nacos进行服务注册