本文介绍如何使用Spire.Doc for Java在Word文档中添加各种预定义的图形以及图形组合。
添加图形
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 InsertShapes {
public static void main(String[] args) throws Exception {
//创建Document对象
Document doc = new Document();
//添加Section和段落
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//添加矩形
ShapeObject rectangle = para.appendShape(130, 80, ShapeType.Rectangle);
rectangle.setFillColor(Color.darkGray);
rectangle.setStrokeColor(Color.darkGray);
rectangle.setVerticalPosition(50);
//添加三角形
ShapeObject triangle = para.appendShape((float)(160/Math.sqrt(3)),80, ShapeType.Triangle);
triangle.setStrokeColor(Color.red);
triangle.setFillColor(Color.orange);
triangle.setVerticalPosition(50);
triangle.setHorizontalPosition(230);
//添加圆形
ShapeObject circle = para.appendShape(80,80, ShapeType.Ellipse);
circle.setFillColor(Color.RED);
circle.setStrokeWeight(15);
circle.setVerticalPosition(50);
circle.setHorizontalPosition((float)(300 + 160/Math.sqrt(3)));
//保存文档
doc.saveToFile("InsertShapes.docx", FileFormat.Docx);
}
}
添加图形组合
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 InsertShapeGroup {
public static void main(String[] args) {
//创建Document对象
Document doc = new Document();
//添加Section和段落
Section sec = doc.addSection();
Paragraph para = sec.addParagraph();
//获取页面内容区域的宽
float pageWidth = sec.getPageSetup().getClientWidth();
//添加图形组合到段落,指定其大小和水平位置
ShapeGroup shapegroup = para.appendShapeGroup(200, 150);
shapegroup.setHorizontalPosition((pageWidth - 200) / 2);
//计算缩放比率
float X = (shapegroup.getWidth() / 1000.0f);
float Y = (shapegroup.getHeight() / 1000.0f);
//创建一个圆形
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);//设置其相对于图形组合的水平位置
//将圆形添加到图形组合
shapegroup.getChildObjects().add(circle_1);
//添加另外两个圆形到图形组合
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);
//保存文档
doc.saveToFile("InsertShapeGroup.docx", FileFormat.Docx_2010);
}
}