|
| 1 | +--- |
| 2 | +title: Working with Shapes | Syncfusion |
| 3 | +description: This section describes how to work with the shapes and groupshape in Word document using Syncfusion Java Word library (Essential DocIO) |
| 4 | +platform: java-file-formats |
| 5 | +control: Word Library |
| 6 | +documentation: UG |
| 7 | +keywords: |
| 8 | +--- |
| 9 | +# Working with Shapes |
| 10 | + |
| 11 | +Shapes are drawing objects that include lines, curves, circles, rectangles, etc. It can be preset or custom geometry. You can create and manipulate the pre-defined shape in DOCX and WordML format documents. |
| 12 | + |
| 13 | +## Adding shapes |
| 14 | + |
| 15 | +The following code example shows how to add pre-defined shape to the document. |
| 16 | + |
| 17 | +{% tabs %} |
| 18 | + |
| 19 | +{% highlight JAVA %} |
| 20 | +//Create a new Word document. |
| 21 | +WordDocument document = new WordDocument(); |
| 22 | +//Add new section to the document. |
| 23 | +IWSection section = document.addSection(); |
| 24 | +//Add new paragraph to the section. |
| 25 | +WParagraph paragraph =(WParagraph) section.addParagraph(); |
| 26 | +//Add new shape to the document. |
| 27 | +Shape rectangle = paragraph.appendShape(AutoShapeType.RoundedRectangle, 150, 100); |
| 28 | +//Set position for shape. |
| 29 | +rectangle.setVerticalPosition(72); |
| 30 | +rectangle.setHorizontalPosition(72); |
| 31 | +paragraph = (WParagraph) section.addParagraph(); |
| 32 | +//Add textbody contents to the shape. |
| 33 | +paragraph = (WParagraph) rectangle.getTextBody().addParagraph(); |
| 34 | +IWTextRange text = paragraph.appendText("This text is in rounded rectangle shape"); |
| 35 | +text.getCharacterFormat().setTextColor(ColorSupport.getGreen()); |
| 36 | +text.getCharacterFormat().setBold(true); |
| 37 | +//Add another shape to the document. |
| 38 | +paragraph = (WParagraph) section.addParagraph(); |
| 39 | +paragraph.appendBreak(BreakType.LineBreak); |
| 40 | +Shape pentagon = paragraph.appendShape(AutoShapeType.Pentagon, 100, 100); |
| 41 | +paragraph = (WParagraph) pentagon.getTextBody().addParagraph(); |
| 42 | +paragraph.appendText("This text is in pentagon shape"); |
| 43 | +//Set position for shape. |
| 44 | +pentagon.setHorizontalPosition(72); |
| 45 | +pentagon.setVerticalPosition(200); |
| 46 | +//Save and close the Word document instance. |
| 47 | +document.save("Result.docx", FormatType.Docx); |
| 48 | +document.close(); |
| 49 | +{% endhighlight %} |
| 50 | + |
| 51 | +{% endtabs %} |
| 52 | + |
| 53 | +### Format shapes |
| 54 | + |
| 55 | +Shape can have formatting such as line color, fill color, positioning, wrap formats, etc. The following code example illustrates how to apply formatting options for shape. |
| 56 | + |
| 57 | +{% tabs %} |
| 58 | + |
| 59 | +{% highlight JAVA %} |
| 60 | +//Create a new Word document. |
| 61 | +WordDocument document = new WordDocument(); |
| 62 | +//Add new section to the document. |
| 63 | +IWSection section = document.addSection(); |
| 64 | +//Add new paragraph to the section. |
| 65 | +IWParagraph paragraph = (WParagraph)section.addParagraph(); |
| 66 | +//Append shape to the paragraph. |
| 67 | +Shape rectangle = paragraph.appendShape(AutoShapeType.RoundedRectangle, 150, 100); |
| 68 | +rectangle.setVerticalPosition(72); |
| 69 | +rectangle.setHorizontalPosition(72); |
| 70 | +paragraph = (WParagraph)section.addParagraph(); |
| 71 | +paragraph = (WParagraph)rectangle.getTextBody().addParagraph(); |
| 72 | +IWTextRange text = paragraph.appendText("This text is in rounded rectangle shape"); |
| 73 | +//Apply format to the text. |
| 74 | +text.getCharacterFormat().setTextColor(ColorSupport.getGreen()); |
| 75 | +text.getCharacterFormat().setBold(true); |
| 76 | +//Apply fill color for shape. |
| 77 | +rectangle.getFillFormat().setFill(true); |
| 78 | +rectangle.getFillFormat().setColor(ColorSupport.getLightGray()); |
| 79 | +//Apply wrap formats. |
| 80 | +rectangle.getWrapFormat().setTextWrappingStyle(TextWrappingStyle.Square); |
| 81 | +rectangle.getWrapFormat().setTextWrappingType(TextWrappingType.Right); |
| 82 | +//Set horizontal and vertical origin. |
| 83 | +rectangle.setHorizontalOrigin(HorizontalOrigin.Margin); |
| 84 | +rectangle.setVerticalOrigin(VerticalOrigin.Page); |
| 85 | +//Set line format. |
| 86 | +rectangle.getLineFormat().setDashStyle(LineDashing.Dot); |
| 87 | +rectangle.getLineFormat().setColor(ColorSupport.getDarkGray()); |
| 88 | +//Save and close the Word document instance. |
| 89 | +document.save("Result.docx", FormatType.Docx); |
| 90 | +document.close(); |
| 91 | +{% endhighlight %} |
| 92 | + |
| 93 | +{% endtabs %} |
| 94 | + |
| 95 | +### Rotate shapes |
| 96 | + |
| 97 | +You can rotate the shape and also apply flipping (horizontal and vertical) to it. The following code example explains how to rotate and flip the shape. |
| 98 | + |
| 99 | +{% tabs %} |
| 100 | + |
| 101 | +{% highlight JAVA %} |
| 102 | +//Create a new Word document. |
| 103 | +WordDocument document = new WordDocument(); |
| 104 | +//Add new section to the document. |
| 105 | +IWSection section = document.addSection(); |
| 106 | +//Add new paragraph to the section. |
| 107 | +WParagraph paragraph = (WParagraph)section.addParagraph(); |
| 108 | +Shape rectangle = paragraph.appendShape(AutoShapeType.RoundedRectangle, 150, 100); |
| 109 | +//Set position for shape. |
| 110 | +rectangle.setVerticalPosition(72); |
| 111 | +rectangle.setHorizontalPosition(72); |
| 112 | +//Set 90 degree rotation. |
| 113 | +rectangle.setRotation(90); |
| 114 | +//Set horizontal flip. |
| 115 | +rectangle.setFlipHorizontal(true); |
| 116 | +paragraph = (WParagraph)section.addParagraph(); |
| 117 | +paragraph = (WParagraph)rectangle.getTextBody().addParagraph(); |
| 118 | +IWTextRange text = paragraph.appendText("This text is in rounded rectangle shape"); |
| 119 | +//Save the Word document. |
| 120 | +document.save("Result.docx", FormatType.Docx); |
| 121 | +//Close the document. |
| 122 | +document.close(); |
| 123 | +{% endhighlight %} |
| 124 | + |
| 125 | +{% endtabs %} |
| 126 | + |
| 127 | +## Grouping shapes |
| 128 | + |
| 129 | +Word library now allows you to create or group multiple shapes, pictures, text boxes as a group shape in Word document (DOCX) and preserve it as in DOCX and WordML format conversions. |
| 130 | + |
| 131 | +You can create a document with group shapes by using Microsoft Word. It provides an option to group a set of shapes and images as a single shape and a group shape as individual item. |
| 132 | + |
| 133 | + |
| 134 | +**Key Features:** |
| 135 | + |
| 136 | +1. You can easily manage group of shapes, pictures, text boxes as a group shape. |
| 137 | +2. You can move several shapes or images simultaneously and apply the same formatting properties for children of group shapes. |
| 138 | + |
| 139 | +N> 1. While grouping the shapes or other objects, the shapes should be positioned relative to the “Page”. |
| 140 | +N> 2. While grouping the shapes or other objects, the wrapping style should not be "In Line with Text". |
| 141 | + |
| 142 | +The following code example shows how to create group shape in Word document. |
| 143 | + |
| 144 | +{% tabs %} |
| 145 | + |
| 146 | +{% highlight JAVA %} |
| 147 | +//Create a new Word document. |
| 148 | +WordDocument document = new WordDocument(); |
| 149 | +//Add new section to the document. |
| 150 | +IWSection section = document.addSection(); |
| 151 | +//Add new paragraph to the section. |
| 152 | +WParagraph paragraph = (WParagraph)section.addParagraph(); |
| 153 | +//Create new group shape. |
| 154 | +GroupShape groupShape = new GroupShape(document); |
| 155 | +//Add group shape to the paragraph. |
| 156 | +paragraph.getChildEntities().add(groupShape); |
| 157 | +//Create new shape. |
| 158 | +Shape shape = new Shape(document, AutoShapeType.RoundedRectangle); |
| 159 | +//Set height and width for shape. |
| 160 | +shape.setHeight(100); |
| 161 | +shape.setWidth(150); |
| 162 | +//Set horizontal and vertical position. |
| 163 | +shape.setHorizontalPosition(72); |
| 164 | +shape.setVerticalPosition(72); |
| 165 | +//Set wrapping style for shape. |
| 166 | +shape.getWrapFormat().setTextWrappingStyle(TextWrappingStyle.InFrontOfText); |
| 167 | +//Set horizontal and vertical origin. |
| 168 | +shape.setHorizontalOrigin(HorizontalOrigin.Page); |
| 169 | +shape.setVerticalOrigin(VerticalOrigin.Page); |
| 170 | +//Add the specified shape to group shape. |
| 171 | +groupShape.add(shape); |
| 172 | +//Create new picture. |
| 173 | +WPicture picture = new WPicture(document); |
| 174 | +FileStreamSupport imageStream = new FileStreamSupport("Image.png", FileMode.Open, FileAccess.ReadWrite); |
| 175 | +picture.loadImage(imageStream.toArray()); |
| 176 | +//Set wrapping style for picture. |
| 177 | +picture.setTextWrappingStyle(TextWrappingStyle.InFrontOfText); |
| 178 | +//Set height and width for the image. |
| 179 | +picture.setHeight(100); |
| 180 | +picture.setWidth(100); |
| 181 | +//Set horizontal and vertical position. |
| 182 | +picture.setHorizontalPosition(400); |
| 183 | +picture.setVerticalPosition(150); |
| 184 | +//Set horizontal and vertical origin. |
| 185 | +picture.setHorizontalOrigin(HorizontalOrigin.Page); |
| 186 | +picture.setVerticalOrigin(VerticalOrigin.Page); |
| 187 | +//Add the specified picture to group shape. |
| 188 | +groupShape.add(picture); |
| 189 | +//Create new textbox. |
| 190 | +WTextBox textbox = new WTextBox(document); |
| 191 | +textbox.getTextBoxFormat().setWidth(150); |
| 192 | +textbox.getTextBoxFormat().setHeight(75); |
| 193 | +//Add new text to the textbox body. |
| 194 | +IWParagraph textboxParagraph = textbox.getTextBoxBody().addParagraph(); |
| 195 | +textboxParagraph.appendText("Text inside text box"); |
| 196 | +//Set wrapping style for textbox. |
| 197 | +textbox.getTextBoxFormat().setTextWrappingStyle(TextWrappingStyle.Behind); |
| 198 | +//Set horizontal and vertical position. |
| 199 | +textbox.getTextBoxFormat().setHorizontalPosition(200); |
| 200 | +textbox.getTextBoxFormat().setVerticalPosition(200); |
| 201 | +//Set horizontal and vertical origin. |
| 202 | +textbox.getTextBoxFormat().setVerticalOrigin(VerticalOrigin.Page); |
| 203 | +textbox.getTextBoxFormat().setHorizontalOrigin(HorizontalOrigin.Page); |
| 204 | +//Add the specified textbox to group shape. |
| 205 | +groupShape.add(textbox); |
| 206 | +//Save and close the Word document instance. |
| 207 | +document.save("Result.docx", FormatType.Docx); |
| 208 | +document.close(); |
| 209 | +{% endhighlight %} |
| 210 | + |
| 211 | +{% endtabs %} |
| 212 | + |
| 213 | + |
| 214 | +### Nested group shapes |
| 215 | + |
| 216 | +The following code example illustrates how to group the nested group shapes as a group shape in Word document. |
| 217 | + |
| 218 | +{% tabs %} |
| 219 | + |
| 220 | +{% highlight JAVA %} |
| 221 | +//Create a new Word document. |
| 222 | +WordDocument document = new WordDocument(); |
| 223 | +//Add new section to the document. |
| 224 | +IWSection section = document.addSection(); |
| 225 | +//Add new paragraph to the section. |
| 226 | +WParagraph paragraph = (WParagraph)section.addParagraph(); |
| 227 | +//Create new group shape. |
| 228 | +GroupShape groupShape = new GroupShape(document); |
| 229 | +//Add group shape to the paragraph. |
| 230 | +paragraph.getChildEntities().add(groupShape); |
| 231 | +//Append new shape to the document. |
| 232 | +Shape shape = new Shape(document, AutoShapeType.RoundedRectangle); |
| 233 | +//Set height and width for shape. |
| 234 | +shape.setHeight(100); |
| 235 | +shape.setWidth(150); |
| 236 | +//Set Wrapping style for shape. |
| 237 | +shape.getWrapFormat().setTextWrappingStyle(TextWrappingStyle.InFrontOfText); |
| 238 | +//Set horizontal and vertical position for shape. |
| 239 | +shape.setHorizontalPosition(72); |
| 240 | +shape.setVerticalPosition(72); |
| 241 | +//Set horizontal and vertical origin for shape. |
| 242 | +shape.setHorizontalOrigin(HorizontalOrigin.Page); |
| 243 | +shape.setVerticalOrigin(VerticalOrigin.Page); |
| 244 | +//Add the specified shape to group shape. |
| 245 | +groupShape.add(shape); |
| 246 | +//Append new picture to the document. |
| 247 | +WPicture picture = new WPicture(document); |
| 248 | +//Load image from the file. |
| 249 | +FileStreamSupport imageStream = new FileStreamSupport("Image.png", FileMode.Open, FileAccess.ReadWrite); |
| 250 | +picture.loadImage(imageStream.toArray()); |
| 251 | +//Set wrapping style for picture. |
| 252 | +picture.setTextWrappingStyle(TextWrappingStyle.InFrontOfText); |
| 253 | +//Set height and width for the picture. |
| 254 | +picture.setHeight(100); |
| 255 | +picture.setWidth(100); |
| 256 | +//Set horizontal and vertical position for the picture. |
| 257 | +picture.setHorizontalPosition(400); |
| 258 | +picture.setVerticalPosition(150); |
| 259 | +//Set horizontal and vertical origin for the picture. |
| 260 | +picture.setHorizontalOrigin(HorizontalOrigin.Page); |
| 261 | +picture.setVerticalOrigin(VerticalOrigin.Page); |
| 262 | +//Add specified picture to the group shape. |
| 263 | +groupShape.add(picture); |
| 264 | +//Create new nested group shape. |
| 265 | +GroupShape nestedGroupShape = new GroupShape(document); |
| 266 | +//Append new textbox to the document. |
| 267 | +WTextBox textbox = new WTextBox(document); |
| 268 | +//Set width and height for the textbox. |
| 269 | +textbox.getTextBoxFormat().setWidth(150); |
| 270 | +textbox.getTextBoxFormat().setHeight(75); |
| 271 | +//Add new text to the textbox body. |
| 272 | +IWParagraph textboxParagraph = textbox.getTextBoxBody().addParagraph(); |
| 273 | +//Add new text to the textbox paragraph. |
| 274 | +textboxParagraph.appendText("Text inside text box"); |
| 275 | +//Set wrapping style for the textbox. |
| 276 | +textbox.getTextBoxFormat().setTextWrappingStyle(TextWrappingStyle.Behind); |
| 277 | +//Set horizontal and vertical position for the textbox. |
| 278 | +textbox.getTextBoxFormat().setHorizontalPosition(200); |
| 279 | +textbox.getTextBoxFormat().setVerticalPosition(200); |
| 280 | +//Set horizontal and vertical origin for the textbox. |
| 281 | +textbox.getTextBoxFormat().setVerticalOrigin(VerticalOrigin.Page); |
| 282 | +textbox.getTextBoxFormat().setHorizontalOrigin(HorizontalOrigin.Page); |
| 283 | +//Add specified textbox to the nested group shape. |
| 284 | +nestedGroupShape.add(textbox); |
| 285 | +//Append new shape to the document. |
| 286 | +shape = new Shape(document, AutoShapeType.Oval); |
| 287 | +//Set height and width for the new shape. |
| 288 | +shape.setHeight(100); |
| 289 | +shape.setWidth(150); |
| 290 | +//Set horizontal and vertical position for the shape. |
| 291 | +shape.setHorizontalPosition(200); |
| 292 | +shape.setVerticalPosition(72); |
| 293 | +//Set horizontal and vertical origin for the shape. |
| 294 | +shape.setHorizontalOrigin(HorizontalOrigin.Page); |
| 295 | +shape.setVerticalOrigin(VerticalOrigin.Page); |
| 296 | +//Set horizontal and vertical position for the nested group shape. |
| 297 | +nestedGroupShape.setHorizontalPosition(72); |
| 298 | +nestedGroupShape.setVerticalPosition(72); |
| 299 | +//Add specified shape to the nested group shape. |
| 300 | +nestedGroupShape.add(shape); |
| 301 | +//Add nested group shape to the group shape of the paragraph. |
| 302 | +groupShape.add(nestedGroupShape); |
| 303 | +//Save and close the Word document instance. |
| 304 | +document.save("Result.docx", FormatType.Docx); |
| 305 | +document.close(); |
| 306 | +{% endhighlight %} |
| 307 | + |
| 308 | +{% endtabs %} |
| 309 | + |
| 310 | +## Ungrouping shapes |
| 311 | + |
| 312 | +You can ungroup the group shapes in the Word document to preserve each shape as individual item. |
| 313 | + |
| 314 | +The following code example shows how to ungroup the group shape in Word document. |
| 315 | + |
| 316 | +{% tabs %} |
| 317 | + |
| 318 | +{% highlight JAVA %} |
| 319 | +//Load the template document. |
| 320 | +WordDocument document = new WordDocument("Template.docx", FormatType.Automatic); |
| 321 | +//Get the last paragraph. |
| 322 | +WParagraph lastParagraph = document.getLastParagraph(); |
| 323 | +//Iterate through the paragraph items to get the group shape. |
| 324 | +for (int i = 0; i < lastParagraph.getChildEntities().getCount(); i++) |
| 325 | +{ |
| 326 | +if (lastParagraph.getChildEntities().get(i) instanceof GroupShape) |
| 327 | +{ |
| 328 | + GroupShape groupShape = (GroupShape)lastParagraph.getChildEntities().get(i); |
| 329 | + //Ungroup the child shapes in the group shape. |
| 330 | + groupShape.ungroup(); |
| 331 | + break; |
| 332 | +} |
| 333 | +} |
| 334 | +//Save and closes the Word document instance. |
| 335 | +document.save("Result.docx", FormatType.Docx); |
| 336 | +document.close(); |
| 337 | +{% endhighlight %} |
| 338 | + |
| 339 | +{% endtabs %} |
0 commit comments