Adding Shapes to Word Document

0
25

Introduction

Shapes are drawing elements, such as rectangles, circles, polygons, and lines. A shape can be filled and/or outlined. Multiple shapes can be grouped so that you can move, resize or arrange them together, as though they were a single shape or object. This tip gives you examples of how to create and customize shape and shape group using .NET Word API with C#.

Background

This library offers a ShapeObject class and a ShapeGroup class to work with a single shape and a shape group respectively. A shape group consists of at least 2 single shapes. Both shape and shape group are attached to an anchoring paragraph. You can call AppendShape() and AppendShapeGroup() method of Paragraph class to add them to the specified paragraph. The following sections will dive into the details about how to:

  • insert, position and format shape
  • reorder the overlapping shapes
  • create multiple shapes and group them together

Using the code

Part I. Insert a shape

Following code snippet adds a 5-pointed star (predefined in ShapeType enumeration) at the specified position in Word document, and sets the fill color, line style, line weight and line color as well.         


Document doc = new Document();


Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();


ShapeObject shape = paragraph.AppendShape(80, 80, ShapeType.Star);


shape.VerticalOrigin = VerticalOrigin.Margin;
shape.HorizontalOrigin = HorizontalOrigin.Margin;
shape.VerticalPosition = 50;
shape.HorizontalPosition = 0;


shape.LineStyle = ShapeLineStyle.Triple;


shape.StrokeWeight = 5;
shape.StrokeColor = System.Drawing.Color.Purple;


shape.FillColor = System.Drawing.Color.Yellow;


doc.SaveToFile("output.docx", FileFormat.Docx2013);

Part II. Change the order of overlapping shapes

Sometimes, you may need to stack a few shapes on top of each other and control the order in which they appear in the stack. Just like in MS Word, the newly created shape always comes in front of the previous one. To change the order of overlapping shapes, set the ZOrder property of ShapeObject instance.


Document doc = new Document();


Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();


ShapeObject shape1 = paragraph.AppendShape(60, 60, ShapeType.Rectangle);
shape1.VerticalPosition = 0;
shape1.HorizontalPosition = 0;
shape1.FillColor = System.Drawing.Color.Blue;


ShapeObject shape2 = paragraph.AppendShape(80, 80, ShapeType.Ellipse);
shape2.VerticalPosition = 30;
shape2.HorizontalPosition = 30;
shape2.FillColor = System.Drawing.Color.Green;


shape1.ZOrder = 2;
shape2.ZOrder = 1;


doc.SaveToFile("output.docx", FileFormat.Docx2013);

Part III. Group your shapes

Shape group itself is a square box with no actual meaning. After appending a ShapeGroup object to the specified paragraph, you need to create more shapes and add them to group for forming a grouped shape. In this instance, three textboxes and two line shapes has been grouped as one unit.


Document doc = new Document();


Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();


ShapeGroup group = paragraph.AppendShapeGroup(500, 100);
group.VerticalPosition = 50;


TextBox textbox1 = new TextBox(doc);
textbox1.Body.AddParagraph().AppendText("Visual C#");
textbox1.Body.Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
textbox1.Format.TextAnchor = ShapeVerticalAlignment.Center;
textbox1.Width = 150;
textbox1.Height = 300;
textbox1.FillColor = System.Drawing.Color.LightGray;
textbox1.StrokeColor = System.Drawing.Color.Black;
textbox1.HorizontalPosition = 0;
textbox1.StrokeWeight = 1;

TextBox textbox2 = new TextBox(doc);
textbox2.Body.AddParagraph().AppendText("Java");
textbox2.Body.Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
textbox2.Format.TextAnchor = ShapeVerticalAlignment.Center;
textbox2.Body.Paragraphs[0].Format.TextAlignment = TextAlignment.Bottom;
textbox2.Width = 150;
textbox2.Height = 300;
textbox2.FillColor = System.Drawing.Color.LightGray;
textbox2.StrokeColor = System.Drawing.Color.Black;
textbox2.HorizontalPosition = 260;
textbox2.StrokeWeight = 1;

TextBox textbox3 = new TextBox(doc);
textbox3.Body.AddParagraph().AppendText("Php");
textbox3.Body.Paragraphs[0].Format.HorizontalAlignment = HorizontalAlignment.Center;
textbox3.Format.TextAnchor = ShapeVerticalAlignment.Center;
textbox3.Width = 150;
textbox3.Height = 300;
textbox3.FillColor = System.Drawing.Color.LightGray;
textbox3.StrokeColor = System.Drawing.Color.Black;
textbox3.HorizontalPosition = 520;
textbox3.StrokeWeight = 1;


group.ChildObjects.Add(textbox1);
group.ChildObjects.Add(textbox2);
group.ChildObjects.Add(textbox3);


group.ChildObjects.Add(new Spire.Doc.Fields.ShapeObject(doc, ShapeType.Line)
{
    Width = 100,
    FillColor = System.Drawing.Color.Red,
    HorizontalPosition = 155,
    VerticalPosition = 150,
    StrokeWeight = 1,
});

group.ChildObjects.Add(new Spire.Doc.Fields.ShapeObject(doc, ShapeType.Line)
{
    Width = 100,
    FillColor = System.Drawing.Color.Red,
    HorizontalPosition = 415,
    VerticalPosition = 150,
    StrokeWeight = 1,
});


doc.SaveToFile("output.docx", FileFormat.Docx2013);

LEAVE A REPLY