|
| 1 | +package autoshapes; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import com.syncfusion.docio.*; |
| 5 | +import com.syncfusion.javahelper.system.drawing.*; |
| 6 | + |
| 7 | +public class AutoShapes { |
| 8 | + public static void main(String[] args) throws Exception { |
| 9 | + // Initialize Word document. |
| 10 | + WordDocument doc = new WordDocument(); |
| 11 | + // Ensure Minimum. |
| 12 | + doc.ensureMinimal(); |
| 13 | + // Create AutoShape. |
| 14 | + Shape shape = createShape(doc, AutoShapeType.RoundedRectangle, (float) 130, (float) 45, (float) 50, true, |
| 15 | + ColorSupport.getBlue(), VerticalAlignment.Middle); |
| 16 | + // Add Texbody contents to Shape. |
| 17 | + IWParagraph para = addParagraph(shape); |
| 18 | + IWTextRange textRange = appendText(para, "Requirement"); |
| 19 | + shape = createDownArrow(doc, AutoShapeType.DownArrow, (float) 45, (float) 45, (float) 95, true); |
| 20 | + shape = createShape(doc, AutoShapeType.RoundedRectangle, (float) 130, (float) 45, (float) 140, true, |
| 21 | + ColorSupport.getOrange(), VerticalAlignment.Middle); |
| 22 | + // Add Texbody contents to Shape. |
| 23 | + para = addParagraph(shape); |
| 24 | + textRange = appendText(para, "Design"); |
| 25 | + shape = createDownArrow(doc, AutoShapeType.DownArrow, (float) 45, (float) 45, (float) 185, true); |
| 26 | + shape = createShape(doc, AutoShapeType.RoundedRectangle, (float) 130, (float) 45, (float) 230, true, |
| 27 | + ColorSupport.getBlue(), VerticalAlignment.Middle); |
| 28 | + // Add Texbody contents to Shape. |
| 29 | + para = addParagraph(shape); |
| 30 | + textRange = appendText(para, "Execution"); |
| 31 | + shape = createDownArrow(doc, AutoShapeType.DownArrow, (float) 45, (float) 45, (float) 275, true); |
| 32 | + shape = createShape(doc, AutoShapeType.RoundedRectangle, (float) 130, (float) 45, (float) 320, true, |
| 33 | + ColorSupport.getViolet(), VerticalAlignment.Middle); |
| 34 | + // Add Texbody contents to Shape. |
| 35 | + para = addParagraph(shape); |
| 36 | + textRange = appendText(para, "Testing"); |
| 37 | + shape = createDownArrow(doc, AutoShapeType.DownArrow, (float) 45, (float) 45, (float) 365, true); |
| 38 | + shape = createShape(doc, AutoShapeType.RoundedRectangle, (float) 130, (float) 45, (float) 410, true, |
| 39 | + ColorSupport.getPaleVioletRed(), VerticalAlignment.Middle); |
| 40 | + // Add Texbody contents to Shape. |
| 41 | + para = addParagraph(shape); |
| 42 | + textRange = appendText(para, "Release"); |
| 43 | + // Save and close the document. |
| 44 | + doc.save("Auto Shapes.docx"); |
| 45 | + doc.close(); |
| 46 | + System.out.println("Word document generated successfully."); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Create AutoShape. |
| 51 | + * |
| 52 | + * @param autoShapeType Represents the shape type. |
| 53 | + * @param width Represents the width of the shape. |
| 54 | + * @param height Represents the height of the shape. |
| 55 | + * @param verticalPosition Represents vertical position of the shape. |
| 56 | + * @param isAllowOverlap Represents given shape can overlap other shapes. |
| 57 | + * @param color Represents color to fill. |
| 58 | + * @param verticalAlignment Represents color to fill. |
| 59 | + */ |
| 60 | + |
| 61 | + private static Shape createShape(WordDocument doc, AutoShapeType autoShapeType, float width, float height, |
| 62 | + float verticalPosition, boolean isAllowOverlap, ColorSupport color, VerticalAlignment verticalAlignment) |
| 63 | + throws Exception { |
| 64 | + // Append AutoShape. |
| 65 | + Shape shape = doc.getLastParagraph().appendShape(autoShapeType, width, height); |
| 66 | + // Set horizontal alignment. |
| 67 | + shape.setHorizontalAlignment(ShapeHorizontalAlignment.Center); |
| 68 | + // Set horizontal origin. |
| 69 | + shape.setHorizontalOrigin(HorizontalOrigin.Page); |
| 70 | + // Set vertical origin. |
| 71 | + shape.setVerticalOrigin(VerticalOrigin.Page); |
| 72 | + // Set vertical position. |
| 73 | + shape.setVerticalPosition(verticalPosition); |
| 74 | + // Set AllowOverlap to true for overlapping shapes. |
| 75 | + shape.getWrapFormat().setAllowOverlap(isAllowOverlap); |
| 76 | + // Set Fill Color. |
| 77 | + shape.getFillFormat().setColor(color); |
| 78 | + // Set Content vertical alignment. |
| 79 | + shape.getTextFrame().setTextVerticalAlignment(verticalAlignment); |
| 80 | + return shape; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Create AutoShape. |
| 85 | + * |
| 86 | + * @param autoShapeType Represents the shape type. |
| 87 | + * @param width Represents the width of the shape. |
| 88 | + * @param height Represents the height of the shape. |
| 89 | + * @param verticalPosition Represents vertical position of the shape. |
| 90 | + * @param isAllowOverlap Represents given shape can overlap other shapes. |
| 91 | + * |
| 92 | + */ |
| 93 | + private static Shape createDownArrow(WordDocument doc, AutoShapeType autoShapeType, float width, float height, |
| 94 | + float verticalPosition, boolean isAllowOverlap) throws Exception { |
| 95 | + // Append AutoShape. |
| 96 | + Shape shape = doc.getLastParagraph().appendShape(autoShapeType, width, height); |
| 97 | + // Set horizontal alignment. |
| 98 | + shape.setHorizontalAlignment(ShapeHorizontalAlignment.Center); |
| 99 | + // Set horizontal origin. |
| 100 | + shape.setHorizontalOrigin(HorizontalOrigin.Page); |
| 101 | + // Set vertical origin. |
| 102 | + shape.setVerticalOrigin(VerticalOrigin.Page); |
| 103 | + // Set vertical position. |
| 104 | + shape.setVerticalPosition(verticalPosition); |
| 105 | + // Set AllowOverlap to true for overlapping shapes. |
| 106 | + shape.getWrapFormat().setAllowOverlap(isAllowOverlap); |
| 107 | + return shape; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * |
| 112 | + * Add the paragraph |
| 113 | + * |
| 114 | + * @param shape Represents the Shape |
| 115 | + * |
| 116 | + */ |
| 117 | + |
| 118 | + private static IWParagraph addParagraph(Shape shape) throws Exception { |
| 119 | + IWParagraph para = shape.getTextBody().addParagraph(); |
| 120 | + para.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Center); |
| 121 | + return para; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * |
| 126 | + * Add texbody contents to Shape. |
| 127 | + * |
| 128 | + * @param para specifies the paragraph in which the text is appended. |
| 129 | + * @param text specifies the textrange to be added |
| 130 | + * |
| 131 | + */ |
| 132 | + private static IWTextRange appendText(IWParagraph para, String text) throws Exception { |
| 133 | + // Add text contents to paragraph. |
| 134 | + IWTextRange textRange = para.appendText(text); |
| 135 | + // Apply the character formats. |
| 136 | + WCharacterFormat characterFormat = textRange.getCharacterFormat(); |
| 137 | + characterFormat.setBold(true); |
| 138 | + characterFormat.setTextColor(ColorSupport.getWhite()); |
| 139 | + characterFormat.setFontSize(12f); |
| 140 | + characterFormat.setFontName("Verdana"); |
| 141 | + return textRange; |
| 142 | + } |
| 143 | +} |
0 commit comments