刀片形状

本教程介绍如何使用 Aspose.Words for .NET 将形状插入到 Word 文档中。形状可用于增强文档的视觉外观和布局。

先决条件

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

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

第 1 步:设置文档目录

首先设置文档目录的路径。代替"YOUR DOCUMENT DIRECTORY"与要保存文档的目录的实际路径。

string dataDir = "YOUR DOCUMENT DIRECTORY";

第 2 步:创建新文档和 DocumentBuilder

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

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

第 3 步:插入形状

使用InsertShape的方法DocumentBuilder将形状插入文档的对象。指定形状类型、相对水平和垂直位置、页面尺寸、尺寸和换行类型。如果需要,您还可以设置形状的旋转角度。

Shape shape = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.Page, 100,
	RelativeVerticalPosition.Page, 100, 50, 50, WrapType.None);
shape.Rotation = 30.0;
builder.Writeln();
shape = builder.InsertShape(ShapeType.TextBox, 50, 50);
shape.Rotation = 30.0;

步骤 4:保存文档

使用以下命令将文档保存到指定目录Save方法。提供所需的文件名和适当的文件扩展名。在此示例中,我们将文档另存为“WorkingWithShapes.InsertShape.docx”。

OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Docx)
{
	Compliance = OoxmlCompliance.Iso29500_2008_Transitional
};
doc.Save(dataDir + "WorkingWithShapes.InsertShape.docx", saveOptions);

使用 Aspose.Words for .NET 插入形状的示例源代码

	//文档目录的路径
	string dataDir = "YOUR DOCUMENT DIRECTORY";

	Document doc = new Document();
	DocumentBuilder builder = new DocumentBuilder(doc);
	Shape shape = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.Page, 100,
		RelativeVerticalPosition.Page, 100, 50, 50, WrapType.None);
	shape.Rotation = 30.0;
	builder.Writeln();
	shape = builder.InsertShape(ShapeType.TextBox, 50, 50);
	shape.Rotation = 30.0;
	OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.Docx)
	{
		Compliance = OoxmlCompliance.Iso29500_2008_Transitional
	};
	doc.Save(dataDir + "WorkingWithShapes.InsertShape.docx", saveOptions);

就是这样!您已使用 Aspose.Words for .NET 成功将形状插入到 Word 文档中。