MS Word allows users to select a shape from shapes menu, drag and place it to any desired location on the page. From Spire.Doc Version 6.0 or above, we added a new feature to work with shape using code. The following section will present how to insert shapes and shape group in a Word document at the specified locations using Spire.Doc.
Code Snippets:
Step 1: Initialize a new instance of Document class.
Document doc = new Document();
Step 2: Add a new section to Word document, and add a paragraph to the section.
Section sec = doc.AddSection(); Paragraph para1 =sec.AddParagraph();
Step 3: Add shapes to the paragraph by calling AppendShape() method. In order to locate where the shape will be placed, you can just set the HorizontalPosition and VerticalPosition properties of ShapeObject class. We can also format the shape by set the FillColor,StrokeColor and LineStyle properties.
ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart); shape1.FillColor = Color.Red; shape1.StrokeColor = Color.Red; shape1.HorizontalPosition = 200; shape1.VerticalPosition = 100; ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow); shape2.FillColor = Color.Purple; shape2.StrokeColor = Color.Black; shape2.LineStyle = ShapeLineStyle.Double; shape2.StrokeWeight = 3; shape2.HorizontalPosition = 200; shape2.VerticalPosition = 200;
Step 4: Add a new paragraph and insert a shape group to the paragraph by calling AppendShapeGroup() method.
Paragraph para2 = sec.AddParagraph(); ShapeGroup shapegr = para2.AppendShapeGroup(200, 400); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle) { Width = 500, Height = 300, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Blue, StrokeWeight = 1.5, }); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle) { Width = 500, Height = 300, VerticalPosition = 301, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Green, StrokeWeight = 1.5, }); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow) { Width = 500, Height = 300, VerticalPosition = 601, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Blue, StrokeWeight = 1.5, });
Step 5: Save the document to file.
doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010);
Result:
Full code:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace InsertShape { class Program { static void Main(string[] args) { Document doc = new Document(); Section sec = doc.AddSection(); Paragraph para1 = sec.AddParagraph(); ShapeObject shape1 = para1.AppendShape(50, 50, ShapeType.Heart); shape1.FillColor = Color.Red; shape1.StrokeColor = Color.Red; shape1.HorizontalPosition = 200; shape1.VerticalPosition = 100; ShapeObject shape2 = para1.AppendShape(100, 100, ShapeType.Arrow); shape2.FillColor = Color.Purple; shape2.StrokeColor = Color.Black; shape2.LineStyle = ShapeLineStyle.Double; shape2.StrokeWeight = 3; shape2.HorizontalPosition = 200; shape2.VerticalPosition = 200; Paragraph para2 = sec.AddParagraph(); ShapeGroup shapegr = para2.AppendShapeGroup(200, 400); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.Rectangle) { Width = 500, Height = 300, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Blue, StrokeWeight = 1.5, }); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.RightTriangle) { Width = 500, Height = 300, VerticalPosition = 301, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Green, StrokeWeight = 1.5, }); shapegr.ChildObjects.Add(new ShapeObject(doc, ShapeType.QuadArrow) { Width = 500, Height = 300, VerticalPosition = 601, LineStyle = ShapeLineStyle.ThickThin, StrokeColor = System.Drawing.Color.Blue, StrokeWeight = 1.5, }); doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010); } } }
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace InsertShape Class Program Private Shared Sub Main(args As String()) Dim doc As New Document() Dim sec As Section = doc.AddSection() Dim para1 As Paragraph = sec.AddParagraph() Dim shape1 As ShapeObject = para1.AppendShape(50, 50, ShapeType.Heart) shape1.FillColor = Color.Red shape1.StrokeColor = Color.Red shape1.HorizontalPosition = 200 shape1.VerticalPosition = 100 Dim shape2 As ShapeObject = para1.AppendShape(100, 100, ShapeType.Arrow) shape2.FillColor = Color.Purple shape2.StrokeColor = Color.Black shape2.LineStyle = ShapeLineStyle.[Double] shape2.StrokeWeight = 3 shape2.HorizontalPosition = 200 shape2.VerticalPosition = 200 Dim para2 As Paragraph = sec.AddParagraph() Dim shapegr As ShapeGroup = para2.AppendShapeGroup(200, 400) shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.Rectangle) With { _ Key .Width = 500, _ Key .Height = 300, _ Key .LineStyle = ShapeLineStyle.ThickThin, _ Key .StrokeColor = System.Drawing.Color.Blue, _ Key .StrokeWeight = 1.5 _ }) shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.RightTriangle) With { _ Key .Width = 500, _ Key .Height = 300, _ Key .VerticalPosition = 301, _ Key .LineStyle = ShapeLineStyle.ThickThin, _ Key .StrokeColor = System.Drawing.Color.Green, _ Key .StrokeWeight = 1.5 _ }) shapegr.ChildObjects.Add(New ShapeObject(doc, ShapeType.QuadArrow) With { _ Key .Width = 500, _ Key .Height = 300, _ Key .VerticalPosition = 601, _ Key .LineStyle = ShapeLineStyle.ThickThin, _ Key .StrokeColor = System.Drawing.Color.Blue, _ Key .StrokeWeight = 1.5 _ }) doc.SaveToFile("InsertShapes.docx", FileFormat.Docx2010) End Sub End Class End Namespace