zoukankan      html  css  js  c++  java
  • Java 获取Word中指定图片的坐标位置

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

    程序运行环境:

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

    方法步骤:

    1. 指定文件路径,本次测试代码路径为项目文件夹路径。即在IDEA项目文件下存入用于测试的Word文档,如:C:UsersAdministratorIdeaProjectsPicture_Docinput.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("坐标位置为:
     X=" + x + " Y=" + y);
                            }
                        }
                    }
                }
            }
    
        }
    }

    坐标获取结果:

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

  • 相关阅读:
    剑指offer 整数中1出现的次数(从1到n整数中1出现的次数)
    剑指offer 把数组排成最小的数
    剑指offer 丑数
    剑指offer 字符串的排列
    剑指offer 数组中出现次数超过一半的数字
    剑指offer 最小的K个数
    操作系统 页面置换算法(C++实现)
    剑指offer 二叉搜索树与双向链表
    剑指offer 复杂链表的复制
    操作系统 银行家算法(C++实现)
  • 原文地址:https://www.cnblogs.com/Yesi/p/14758492.html
Copyright © 2011-2022 走看看