使用形状
本主题讨论如何使用 Aspose.Words 以编程方式处理形状。
Aspose.Words 中的形状表示绘图层中的对象,例如自选图形、文本框、自由格式、OLE 对象、ActiveX 控件或图片。 Word 文档可以包含一种或多种不同的形状。 Aspose.Words 中的形状由 Shape 类表示。
使用文档生成器插入形状
您可以使用 InsertShape 方法将指定类型和大小的内联形状以及指定位置、大小和文本换行类型的自由浮动形状插入到文档中。 InsertShape 方法允许将 DML 形状插入到文档模型中。文档必须以支持DML形状的格式保存,否则,在保存文档时,此类节点将被转换为VML形状。
以下代码示例演示如何将这些类型的形状插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Free-floating shape insertion. | |
Shape shape = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.Page, 100, RelativeVerticalPosition.Page, 100, 50, 50, WrapType.None); | |
shape.Rotation = 30.0; | |
builder.Writeln(); | |
//Inline shape insertion. | |
shape = builder.InsertShape(ShapeType.TextBox, 50, 50); | |
shape.Rotation = 30.0; | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.Docx); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.Compliance = OoxmlCompliance.Iso29500_2008_Transitional; | |
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir, so); |
设置长宽比锁定
使用 Aspose.Words,您可以通过 AspectRatioLocked 属性指定是否锁定形状的纵横比。
以下代码示例演示如何使用 AspectRatioLocked 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
在单元格中设置形状布局
您还可以使用 IsLayoutInCell 属性指定形状是显示在表格内部还是表格外部。
以下代码示例演示如何使用 IsLayoutInCell 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TextPlainText); | |
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page; | |
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page; | |
watermark.IsLayoutInCell = true; // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.Width = 300; | |
watermark.Height = 70; | |
watermark.HorizontalAlignment = HorizontalAlignment.Center; | |
watermark.VerticalAlignment = VerticalAlignment.Center; | |
watermark.Rotation = -40; | |
watermark.Fill.Color = Color.Gray; | |
watermark.StrokeColor = Color.Gray; | |
watermark.TextPath.Text = "watermarkText"; | |
watermark.TextPath.FontFamily = "Arial"; | |
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid()); | |
watermark.WrapType = WrapType.None; | |
Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run; | |
builder.MoveTo(run); | |
builder.InsertNode(watermark); | |
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010); | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
创建剪角矩形
您可以使用 Aspose.Words 创建剪角矩形。形状类型为 SingleCornerSnipped、TopCornersSnipped、DiagonalCornersSnipped、TopCornersOneRoundedOneSnipped、SingleCornerRounded、TopCornersRounded 和 DiagonalCornersRounded.
DML 形状是使用 InsertShape 方法使用这些形状类型创建的。这些类型不能用于创建 VML 形状。尝试使用"Shape"类的公共构造函数创建形状会引发"NotSupportedException"异常。
以下代码示例展示了如何将这些类型的形状插入到文档中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.InsertShape(ShapeType.TopCornersSnipped, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.Docx); | |
so.Compliance = OoxmlCompliance.Iso29500_2008_Transitional; | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.Save(dataDir, so); |
获取实际形状边界点
使用 Aspose.Words API,您可以获得包含块的形状相对于最顶部形状的锚点的位置和大小(以点为单位)。为此,请使用 BoundsInPoints 属性。
以下代码示例演示如何使用 BoundsInPoints 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
Console.Write("\nGets the actual bounds of the shape in points."); | |
Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints); |
指定垂直锚点
您可以使用 VerticalAnchor 属性指定形状内的文本垂直对齐方式。
以下代码示例演示如何使用 VerticalAnchor 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"VerticalAnchor.docx"); | |
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); | |
Shape textBoxShape = shapes[0] as Shape; | |
if (textBoxShape != null) | |
{ | |
textBoxShape.TextBox.VerticalAnchor = TextBoxAnchor.Bottom; | |
} | |
doc.Save(dataDir + "VerticalAnchor_out.docx"); |
检测 SmartArt 形状
Aspose.Words 还允许检测 Shape 是否具有 SmartArt
对象。为此,请使用 HasSmartArt 属性。
以下代码示例演示如何使用 HasSmartArt 属性:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + "input.docx"); | |
int count = 0; | |
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true)) | |
{ | |
if (shape.HasSmartArt) | |
count++; | |
} | |
Console.WriteLine("The document has {0} shapes with SmartArt.", count); |
将水平线插入文档
您可以使用 InsertHorizontalRule 方法将水平线形状插入到文档中。
以下代码示例展示了如何执行此操作:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Insert a horizontal rule shape into the document."); | |
builder.InsertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.Save(dataDir); |
Aspose.Words API 提供 HorizontalRuleFormat 属性来访问水平线形状的属性。 HorizontalRuleFormat 类公开了用于水平线格式设置的基本属性,例如高度、颜色、NoShade 等。
以下代码示例展示了如何设置 HorizontalRuleFormat:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
Shape shape = builder.InsertHorizontalRule(); | |
HorizontalRuleFormat horizontalRuleFormat = shape.HorizontalRuleFormat; | |
horizontalRuleFormat.Alignment = HorizontalRuleAlignment.Center; | |
horizontalRuleFormat.WidthPercent = 70; | |
horizontalRuleFormat.Height = 3; | |
horizontalRuleFormat.Color = Color.Blue; | |
horizontalRuleFormat.NoShade = true; | |
builder.Document.Save("HorizontalRuleFormat.docx"); |
将带有 Math XML 的形状作为形状导入 DOM
您可以使用 ConvertShapeToOfficeMath 属性将带有 EquationXML 的形状转换为 Office Math 对象。此属性的默认值对应于 Microsoft Word 行为,即带有方程式 XML 的形状不会转换为 Office 数学对象。
以下代码示例演示如何将形状转换为 Office Math 对象:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions lo = new LoadOptions(); | |
lo.ConvertShapeToOfficeMath = true; | |
// Specify load option to use previous default behaviour i.e. convert math shapes to office math ojects on loading stage. | |
Document doc = new Document(dataDir + @"OfficeMath.docx", lo); | |
//Save the document into DOCX | |
doc.Save(dataDir + "ConvertShapeToOfficeMath_out.docx", SaveFormat.Docx); |