天天看點

Java 添加Word形狀或圖形

本文将介紹通過java程式設計在Word文檔中添加形狀(圖形),包括添加單個圖形、組合圖形,以及格式化圖形樣式,如設定形狀填充色、大小、位置、邊框樣式、邊框顔色、邊框粗細、圖形旋轉角度、圖形文本環繞方式等。

使用工具:Free Spire.Doc for Java(免費版)

Jar擷取及導入

方法1:通過官網下載下傳jar包。下載下傳後,解壓檔案,并将lib檔案夾下的Spire.Doc.jar檔案導入到java程式。參考如下導入效果:

Java 添加Word形狀或圖形

方法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();
    }
}      

形狀添加效果:

Java 添加Word形狀或圖形

(本文完)