When
creating a Word document, you can add many types of shapes, such as triangle,
rectangle and circle, to highlight important items. Bring attention to those
items helps readers to better understand the content of the document. Here I’ll
show you how to add or rotate shapes in a Word document by using Free Spire.Doc
for Java.
BEFORE
START
Please
download the latest version of Free Spire.Doc for Java from the link, unzip it
and manually add Spire.Doc.jar located in the lib folder to your JAVA project
as a dependency.
Besides, if you are creating a Maven project, you can
easily add the jar dependency by adding the following configurations to the
pom.xml.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue
</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
Code Snippets
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class AddShapes {
public static void main(String[] args) {
//create a Word document.
Document doc = new Document();
//add a section and a paragraph
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//insert a rectangle
ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
rectangle.setFillColor(Color.green);
rectangle.setStrokeColor(Color.white);
rectangle.setVerticalPosition(50);
//insert a triangle
ShapeObject triangle = para.appendShape((float)(160/Math.sqrt(3)),80, ShapeType.Triangle);
triangle.setStrokeColor(Color.blue);
triangle.setFillColor(Color.red);
triangle.setVerticalPosition(50);
triangle.setHorizontalPosition(200);
//insert a circle
ShapeObject circle = para.appendShape(220,80, ShapeType.Ellipse);
circle.setFillColor(Color.yellow);
circle.setStrokeWeight(5);
circle.setStrokeColor(Color.lightGray);
circle.setVerticalPosition(50);
circle.setHorizontalPosition((float)(220 + 160/Math.sqrt(3)));
//save to file
doc.saveToFile("output/InsertShapes.docx", FileFormat.Docx);
}
}
Output
Add Shape Groups
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeGroup;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class AddShapeGroup {
public static void main(String[] args) {
//create a Word document
Document doc = new Document();
//add a section and a paragraph
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//get page width
float pageWidth = sec.getPageSetup().getClientWidth();
//add a shape group, specifying width, height and horizontal position
ShapeGroup shapegroup = para.appendShapeGroup(200, 150);
shapegroup.setHorizontalPosition((pageWidth - 200) / 2);
//calculate the scale ratio
float X = (shapegroup.getWidth() / 1000.0f);
float Y = (shapegroup.getHeight() / 1000.0f);
//create a circle
ShapeObject circle_1 = new ShapeObject(doc, ShapeType.Ellipse);
circle_1.setWidth(80 / X);
circle_1.setHeight(80 / Y);
circle_1.setFillColor(new Color(176, 196, 222));
circle_1.setStrokeColor(new Color(176, 196, 222));
circle_1.setHorizontalPosition(60 / X);//set its horizontal position relative to shape group
//add the circle to shape group
shapegroup.getChildObjects().add(circle_1);
//add two more circles to shape group
ShapeObject circle_2 = new ShapeObject(doc, ShapeType.Ellipse);
circle_2.setWidth(80 / X);
circle_2.setHeight(80 / Y);
circle_2.setFillColor(new Color(0, 128, 128));
circle_2.setStrokeColor(new Color(0, 128, 128));
circle_2.setHorizontalPosition(30 / X);
circle_2.setVerticalPosition(50 / Y);
shapegroup.getChildObjects().add(circle_2);
ShapeObject circle_3 = new ShapeObject(doc, ShapeType.Ellipse);
circle_3.setWidth(80 / X);
circle_3.setHeight(80 / Y);
circle_3.setFillColor(new Color(72, 61, 139));
circle_3.setStrokeColor(new Color(72, 61, 139));
circle_3.setHorizontalPosition(90 / X);
circle_3.setVerticalPosition(50 / Y);
shapegroup.getChildObjects().add(circle_3);
//save the document
doc.saveToFile("output/InsertShapeGroup.docx", FileFormat.Docx_2010);
}
}Output
Rotate Shapes
import com.spire.doc.Document;
import com.spire.doc.DocumentObject;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ShapeObject;
public class RotateShapes {
public static void main(String[] args) {
//Load the Sample Word document
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\InsertShapes.docx");
//Get the first section
Section sec = doc.getSections().get(0);
//Traverse every paragraphs to get the shapes and rotate them
for ( Paragraph para: (Iterable<Paragraph>) sec.getParagraphs()) {
for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects()) {
if (obj instanceof ShapeObject) {
((ShapeObject) obj).setRotation(20);
}
}
}
//Save to file
doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);
}
}Output
No comments:
Post a Comment