本文介绍如何使用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);
}
}