点击(此处)折叠或打开
-
import com.spire.doc.*;
-
import com.spire.doc.documents.DocumentObjectType;
-
import com.spire.doc.documents.Paragraph;
-
import com.spire.doc.fields.DocPicture;
-
-
//java项目www fhadmin org
-
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);
-
}
-
}
-
}
-
}
-
}
-
-
}
- }