Shape

Shape constructor

创建一个新的形状对象。

public Shape(DocumentBase doc, ShapeType shapeType)
范围类型描述
docDocumentBase所有者文件。
shapeTypeShapeType要创建的形状的类型。

评论

创建形状后,您应该指定所需的形状属性。

例子

演示如何将带有图像的形状从本地文件系统插入到文档中。

Document doc = new Document();

// “Shape”类的公共构造函数将创建一个具有“ShapeMarkupLanguage.Vml”标记类型的形状。
// 如果需要创建非原始类型的形状,例如SingleCornerSnipped、TopCornersSnipped、DiagonalCornersSnipped,
// TopCornersOneRoundedOneSnipped、SingleCornerRounded、TopCornersRounded 或 DiagonalCornersRounded,
// 请使用 DocumentBuilder.InsertShape。
Shape shape = new Shape(doc, ShapeType.Image);
shape.ImageData.SetImage(ImageDir + "Windows MetaFile.wmf");
shape.Width = 100;
shape.Height = 100;

doc.FirstSection.Body.FirstParagraph.AppendChild(shape);

doc.Save(ArtifactsDir + "Image.FromFile.docx");

演示如何创建文本框并设置其格式。

Document doc = new Document();

// 创建一个浮动文本框。
Shape textBox = new Shape(doc, ShapeType.TextBox);
textBox.WrapType = WrapType.None;
textBox.Height = 50;
textBox.Width = 200;

// 设置形状内文本的水平和垂直对齐方式。
textBox.HorizontalAlignment = HorizontalAlignment.Center;
textBox.VerticalAlignment = VerticalAlignment.Top;

// 将一个段落添加到文本框并添加文本框将显示的一系列文本。
textBox.AppendChild(new Paragraph(doc));
Paragraph para = textBox.FirstParagraph;
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
Run run = new Run(doc);
run.Text = "Hello world!";
para.AppendChild(run);

doc.FirstSection.Body.FirstParagraph.AppendChild(textBox);

doc.Save(ArtifactsDir + "Shape.CreateTextBox.docx");

也可以看看