获取实际形状边界点

本教程介绍如何使用 Aspose.Words for .NET 在 Word 文档中检索以点(测量单位)为单位的形状的实际边界。边界表示文档中形状的大小和位置。

先决条件

要学习本教程,您需要具备以下条件:

  • 已安装 Aspose.Words for .NET 库。
  • C# 和 Word 文档文字处理的基础知识。

第 1 步:创建新文档和 DocumentBuilder

创建一个新实例Document类和一个DocumentBuilder对象使用该文档。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

第 2 步:插入图像形状

使用InsertImage的方法DocumentBuilder对象将图像形状插入到文档中。提供图像文件的路径作为参数。

Shape shape = builder.InsertImage(ImagesDir + "Transparent background logo.png");
shape.AspectRatioLocked = false;

第 3 步:检索实际形状边界点

访问形状的ShapeRenderer使用GetShapeRenderer方法。然后,使用以下命令检索形状的实际边界(以点为单位):BoundsInPoints财产。

Console.Write("\nGets the actual bounds of the shape in points: ");
Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints);

使用 Aspose.Words for .NET 获取实际形状边界点的示例源代码

	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);
	Shape shape = builder.InsertImage(ImagesDir + "Transparent background logo.png");
	shape.AspectRatioLocked = false;
	Console.Write("\nGets the actual bounds of the shape in points: ");
	Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints);

就是这样!您已使用 Aspose.Words for .NET 成功检索了 Word 文档中形状的实际边界(以点为单位)。