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