本文将介绍如何使用Spire.Presentation for Java获取PowerPoint文档中文本的位置。
import com.spire.presentation.IAutoShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import java.awt.geom.Point2D;
public class GetPositionOfText {
public static void main(String []args) throws Exception {
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.loadFromFile("Sample.pptx");
//获取第一张幻灯片
ISlide slide = ppt.getSlides().get(0);
//获取第一个形状
IAutoShape shape = (IAutoShape)slide.getShapes().get(0);
//获取形状中文本的位置
Point2D location =shape.getTextFrame().getTextLocation();
//打印文本位置(相对于幻灯片)的x和y坐标
String point1="文本位置(相对于幻灯片): x= "+location.getX()+" y = "+location.getY();
System.out.println(point1);
//打印文本位置(相对于形状)的x和y坐标
String point2 = "文本位置(相对于形状): x= " + (location.getX() - shape.getLeft()) + " y = " + (location.getY() - shape.getTop());
System.out.println(point2);
}
}
输出结果: