本文将介绍通过java编程在Word文档中添加形状(图形),包括添加单个图形、组合图形,以及格式化图形样式,如设置形状填充色、大小、位置、边框样式、边框颜色、边框粗细、图形旋转角度、图形文本环绕方式等。
使用工具:Free Spire.Doc for Java(免费版)
Jar获取及导入
方法1:通过官网下载jar包。下载后,解压文件,并将lib文件夹下的Spire.Doc.jar文件导入到java程序。参考如下导入效果:
方法2:可通过maven仓库安装导入。
Java代码示例
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ShapeLineStyle; import com.spire.doc.documents.ShapeType; import com.spire.doc.documents.TextWrappingStyle; import com.spire.doc.fields.ShapeGroup; import com.spire.doc.fields.ShapeObject; import java.awt.*; public class DrawShape { public static void main(String[]args){ //创建文档,添加段落 Document doc = new Document(); Paragraph para = doc.addSection().addParagraph(); //添加指定大小的矩形到文档中的指定位置 ShapeObject rectangle = para.appendShape(60,60, ShapeType.Rectangle); rectangle.setFillColor(Color.MAGENTA); rectangle.setStrokeColor(Color.GREEN); rectangle.setStrokeWeight(5); rectangle.setLineStyle(ShapeLineStyle.Double); rectangle.setVerticalPosition(50); rectangle.setHorizontalPosition(70); rectangle.setRotation(10); rectangle.setAlternativeText("矩形"); //添加三角形 ShapeObject triangle = para.appendShape(60,60,ShapeType.Triangle); triangle.setStrokeColor(Color.pink); triangle.setFillColor(Color.orange); triangle.setVerticalPosition(50); triangle.setHorizontalPosition(170); triangle.setRotation(-30); triangle.setTextWrappingStyle(TextWrappingStyle.Through); //添加圆形 ShapeObject circle = para.appendShape(60,60, ShapeType.Ellipse); circle.setFillColor(Color.cyan); circle.setStrokeWeight(7); circle.setStrokeColor(Color.BLUE); circle.setVerticalPosition(50); circle.setHorizontalPosition(270); //添加波浪图形 ShapeObject wave = para.appendShape(80,60, ShapeType.Double_Wave); wave.setFillColor(new Color(255,228,196)); wave.setStrokeWeight(3); wave.setStrokeColor(Color.ORANGE); wave.setVerticalPosition(50); wave.setHorizontalPosition(370); //添加图形组合到段落,指定其大小和水平位置 ShapeGroup shapegroup = para.appendShapeGroup(200, 150); shapegroup.setHorizontalPosition(150); shapegroup.setVerticalPosition(150); //计算缩放比率 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(144,238,144)); circle_1.setStrokeColor(new Color(144,238,144)); 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(255,192,203)); circle_2.setStrokeColor(new Color(255,192,203)); 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(255,239,213)); circle_3.setStrokeColor(new Color(255,239,213)); circle_3.setHorizontalPosition(90 / X); circle_3.setVerticalPosition(50 / Y); shapegroup.getChildObjects().add(circle_3); //保存文档 doc.saveToFile("AddShape.docx",FileFormat.Docx_2013); doc.dispose(); } }