一、前言及环境搭建

在Word文档中,我们可以在录入文本信息时加入一些图形,来使内容更加生动形象且富有独特样式。前文中演示过如何在Excel工作表中添加、读取和删除形状,同时也介绍过怎样在PPT文档中添加和合并形状。本文则是通过使用Java程序来介绍为Word文档添加和旋转图形的方法。

通过官网下载获取免费版的jar包并解压,手动将lib文件夹下的Spire.Doc.jar导入Java程序。另外,也可通过maven仓库安装导入

二、Java代码示例

示例1 添加单个图形

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) {
    //创建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.cyan);
        rectangle.setStrokeColor(Color.cyan);
        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(200);

    //添加椭圆形
        ShapeObject circle = para.appendShape(220,80, ShapeType.Ellipse);
        circle.setFillColor(Color.blue);
        circle.setStrokeWeight(5);
        circle.setStrokeColor(Color.GRAY);
        circle.setVerticalPosition(50);
        circle.setHorizontalPosition((float)(220 + 160/Math.sqrt(3)));

    //保存文档
        doc.saveToFile("output/InsertShapes.docx", FileFormat.Docx);
    }
}

效果示例:

效果1.png

示例2 添加图形组合

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) {
        //创建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("output/InsertShapeGroup.docx", FileFormat.Docx_2010);
    }
}

效果示例:

效果2.png

示例3 旋转图形

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) {
        //加载示例文档.
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Test1\\Desktop\\InsertShapes.docx");

        //获取第一个section
        Section sec = doc.getSections().get(0);

        //遍历所有的段落并将段落里所有的图形旋转20度
        for ( Paragraph para: (Iterable<Paragraph>) sec.getParagraphs()) {
            for (DocumentObject obj : (Iterable<DocumentObject>) para.getChildObjects())   {

                if (obj instanceof ShapeObject) {
                    ((ShapeObject) obj).setRotation(20);
                }
            }
        }
        //保存文档
        doc.saveToFile("output/RotateShape.docx", FileFormat.Docx);
    }
}

效果示例:

效果3.png

(本文完)


Tina_Tang
298 声望9 粉丝