刀片形状

本教程介绍如何使用 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 文档中。