Word documents can contain more than just text. Developers might want to add shapes to highlight essential items or emphasize ideas on the document. Bringing attention to those items helps readers better understand the content. With the advent of the v3.2 release of GrapeCity Documents for Word, it is now possible to add various shapes in Word documents. The base class for the following varieties is ShapeBase.
Adding Shapes to Word Document
Shapes are drawing elements, such as rectangles, circles, polygons, and lines. A shape can be filled and/or outlined. It is also possible to format and position the shape. Additionally, it can contain a description.
Here is the code snippet to draw a yellow diamond shape:
GcWordDocument doc = new GcWordDocument(); Paragraph para = doc.Body.Paragraphs.Add(); //Define a run object to add the shapes Run run = para.GetRange().Runs.Add(); var shape = run.GetRange().Shapes.Add(200, 300, GeometryType.Diamond); shape.Line.Fill.Type = FillType.Solid; shape.Line.Fill.SolidFill.ThemeCOlor = ThemeColorId.Accent4;
Adding a GroupShape to Word Documents
GroupShape is an object used for grouping the shapes and pictures together. It can be challenging to manipulate shapes if they are scattered throughout a page or document. We can group shapes only if they are inside a single run. This scenario is where grouping is helpful. Multiple shapes or pictures can be grouped as if they are a single entity. And you can resize, move, or even rotate them all at once. This action is helpful if there are complex images or drawings incorporated into the document. It provides unified properties for child shapes and can be viewed as "shape container."
Below is the code snippet, which shows how you can add multiple shapes like Rectangle and Star into a GroupShapes object.
var group_shape = run.GetRange().GroupShapes.Add(500, 500); var firstShape = group_shape.GetRange().Shapes.Add(100, 100, GeometryType.Star12); var secondshape = group_shape.GetRange().Shapes.Add(100, 39, GeometryType.Rectangle); firstShape.Fill.Type = FillType.Solid; firstshape.Fill.SolidFill.ThemeColor = ThemeColorID.Accent4; secondShape.Fill.Type = FillType.Solid; secondShape.Fill.SolidFill.ThemeColor = ThemeColorID.Accent2; secondShape.Position.Horizontal.Offset = 100; secondShape.Position.Veritcal.Offset = 100;
Organize Drawing Objects with CanvasShape
A canvas shape helps organize the drawing objects. Essentially, it provides a container for the pieces and parts that make up your drawing.
Below is the code snippet which shows how you can draw multiple shapes into a CanvasShapes object:
var canvas_shape = run.GetRange().CanvasShapes.Add(500, 500); var firstShape = canvas_shape.GetRange().Shapes.Add(100, 100, GeometryType.Round2DiagonalRectangle); var secondShape = canvas_shape.GetRange().ShapesAdd(100, 39, GeometryType.Star8); firstShape.Fill.Type = FillType.Solid; firstShape.Fill.SolidFill.ThemeColor = ThemeColorId.Accent5; secondShape.Fill.Type = FillType.Solid; secondShape.Fill.SolidFill.ThemeColor = ThemeColorId.Accent1; secondShape.Position.Horizontal.Offset = 50; secondShape.Position.Vertical.Offset = 50;
How to Add InkShapes to Word Documents
Lines and curves are drawn using the InkShapes object. Currently, GcWord supports loading predefined InkShapes only.
var doc = new GcWordDocument(); doc.Load("ink.docx"); if (doc.Body.InkShapes.Count == 0) return;
Adding Pictures to Word Documents
The picture class represents an image element in the body of the document. A picture is a well-known object in GcWord since v2, but in v3.2, it is rewritten under the ShapeBase class.
var pic = run.GetRange().Pictures.Add(); var picBytes = File.ReadAllBytes(@".filename.jpg"); pic.ImageData.SetImage(picBytes, "image/jpg"); pic.Position.Vertical.RelativeTo = ShapeVerticalRelativePosition.Page; pic.Position.Vertical.Offset = 100; pic.Line.Width = 2; pic.Size.EffectExtent.AllEdges
The different shapes supported by GcWord are detailed above. Now consider how this feature is used to draw flow charts in Word documents.
Flow Chart for Healthcare Program using GcWordAPI
Insurance companies offer various health care programs that require documents to decide the flow for checking a person's health through an application. Such documents usually present information in the form of algorithms and flowcharts. We will demonstrate this by drawing a flow chart using GcWord and showing a healthcare program that has existed for over 40 years.
Step1: Define a Run object in the document where we can add different shapes.
//Create a document object GcWordDocument doc = new GcWordDocument(); Paragraph para = doc.Body.Paragraphs.Add(); //Define a run object to add the shapes Run run = para.GetRange().Runs.Add();
Step 2: Add different GeometryShapes: FlowChartDecision, FlowChartProcess, Rectangle, and arrow lines in the document. Style and position them to generate a meaningful document.
//Define a Ellipse shape to show the starting point of the Flowchart Shape shape = run.GetRange().Shapes.Add(90, 40, GeometryType.Ellipse); shape.AddTextFrame(" Start"); shape.Fill.Type = FillType.Solid; shape.Fill.SolidFill.ThemeColor = ThemeColorId.Accent3; shape.Position.Horizontal.RelativeTo = ShapeHorizontalRelativePosition.LeftMargin; shape.Position.Vertical.RelativeTo = ShapeVerticalRelativePosition.TopMargin; shape.WrapFormat.Type = WrapType.Square; shape.Position.Horizontal.Type = ShapePositionType.Percents; shape.Position.Vertical.Type = ShapePositionType.Percents; shape.Position.Horizontal.Offset = 340; shape.Position.Vertical.Offset = 200; shape.Position.AllowOverlap = true; shape.Position.Horizontal.Alignment = ShapeHorizontalRelativeAlignment.Center; //Draw the arrow in the Flowchart to show the flow of the process Shape line = run.GetRange().Shapes.Add(5, 53, GeometryType.DownArrow); line.Fill.Type = FillType.Solid; line.Fill.SolidFill.ThemeColor = ThemeColorId.Dark1; line.Position.Horizontal.RelativeTo = ShapeHorizontalRelativePosition.LeftMargin; line.Position.AllowOverlap = true; line.Position.Vertical.RelativeTo = ShapeVerticalRelativePosition.TopMargin; line.WrapFormat.Type = WrapType.Square; line.Position.Horizontal.Type = ShapePositionType.Percents; line.Position.Vertical.Type = ShapePositionType.Percents; line.Position.Horizontal.Offset = 401; line.Position.Vertical.Offset = 255; //Draw a diamond shape to show a condition in the Flowchart shape = run.GetRange().Shapes.Add(150, 30, GeometryType.FlowChartProcess); shape.AddTextFrame("Health Check"); shape.Fill.Type = FillType.Solid; shape.Fill.SolidFill.ThemeColor = ThemeColorId.Accent2; shape.Position.Horizontal.RelativeTo = ShapeHorizontalRelativePosition.LeftMargin; shape.Position.Vertical.RelativeTo = ShapeVerticalRelativePosition.TopMargin; shape.WrapFormat.Type = WrapType.Square; shape.Position.Horizontal.Type = ShapePositionType.Percents; shape.Position.Vertical.Type = ShapePositionType.Percents; shape.Position.Horizontal.Offset = 293; shape.Position.Vertical.Offset = 470; shape.Position.AllowOverlap = true;
In the code above, we illustrate how to add the unique shapes used in the flow chart. For the complete implementation, a sample is attached at the bottom of this post.
Step 3: Save the document to a Word file:
Try using GcWord to create Word documents and explore the different shapes supported by the product.
Try GrapeCity Documents for Word free for 30 days Download GrapeCity Documents for Word
Try GrapeCity Documents for Word free for 30 days
Download GrapeCity Documents for WordDownload Now!