A canvas shape acts as a container for multiple drawing objects and can be used to organize them all at once.
DsWord allows you to draw a canvas shape and add multiple drawing objects to it. A canvas shape can be added by using Add and Insert methods of CanvasShapeCollection class. You can also access the previous or next canvas shapes and apply fill format or line format properties on them by using various methods provided by CanvasShape class.
To add canvas shape in a document:
C# |
Copy Code |
---|---|
var doc = new GcWordDocument(); //Add Run element var run = doc.Body.Paragraphs.Add().GetRange().Runs.Add(); //Create canvas shape with initial width 500 and height 500 var canvas_shape = run.GetRange().CanvasShapes.Add(500, 500); //Create two shapes inside canvas shape var firstShape = canvas_shape.GetRange().Shapes.Add(202, 250, "First shape"); var secondshape = canvas_shape.GetRange().Shapes.Add(300, 400, "Second shape"); //Move second shape 100 points right and 100 points lower secondshape.Position.Horizontal.Offset = 100; secondshape.Position.Vertical.Offset = 100; //Save document doc.Save("CanvasShape.docx"); |
To delete canvas shape in a document:
C# |
Copy Code |
---|---|
//Load document doc.Load("CanvasShape.docx"); //Access first canvas CanvasShape cs = doc.Body.CanvasShapes.First; //Delete canvas cs.Delete(); doc.Save("CanvasDelete.docx"); |
Limitations