與形狀一起工作

本篇文章討論如何使用 Aspose.Words 程式化地與形状一起工作。

圖形 Aspose.Words 代表繪圖層中的物件,例如 AutoShape、文字方框、自由形式圖形、OLE 物件、ActiveX 控制或圖像。 Word 文檔可以包含一個或多個不同形狀。 Aspose.Words中的圖形是由 Shape 類別表示的。

透過 Document Builder 插入形狀

您可以使用 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也允許檢查形狀是否具有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屬性來存取水平線形狀的屬性。 The HorizontalRuleFormat 類別會公開基本的性質,例如高度、顏色、NoShadow 等,用於格式化水平線。

以下範例顯示如何設定 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");

將具有數學 XML 的形狀导入到 DOM

您可以使用ConvertShapeToOfficeMath屬性將方程式 XML 中的形狀轉換為 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);