Java 添加Word文本框


在Word中,文本框是指一種可移動、可調節大小的文字或圖形容器。我們可以向文本框中添加文字、圖片、表格等對象,下面,將通過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.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextBox;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class AddTextbox {
    public static void main(String[]args){
        //創建文檔
        Document doc = new Document();

        //添加指定大小的文本框
        TextBox tb = doc.addSection().addParagraph().appendTextBox(380, 275);
        //設置文字環繞方式
        tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);

        //設置文本框的相對位置
        tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);
        tb.getFormat().setHorizontalPosition(120f);
        tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
        tb.getFormat().setVerticalPosition(100f);

        //設置文本框邊框樣式
        tb.getFormat().setLineStyle(TextBoxLineStyle.Thin_Thick);
        tb.getFormat().setLineColor(Color.gray);

        //插入圖片到文本框
        Paragraph para = tb.getBody().addParagraph();
        DocPicture picture = para.appendPicture("5G.png");
        picture.setHeight(120f);
        picture.setWidth(180f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
        para.getFormat().setAfterSpacing(13f);

        //插入文字到文本框
        para = tb.getBody().addParagraph();
        TextRange textRange = para.appendText("中美貿易爭端,又稱中美貿易戰,也叫中美貿易摩擦,是中美經濟關系中的重要問題。 "
                + "貿易爭端主要發生在兩個方面:一是中國具有比較優勢的出口領域;"
                + "二是中國沒有優勢的進口和技術知識領域。");
        textRange.getCharacterFormat().setFontName("楷體");
        textRange.getCharacterFormat().setFontSize(11f);
        para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //添加表格到文本框
        //聲明數組內容
        String[][] data = new String[][]{
                new String[]{"中美進出口差額"},
                new String[]{"國家", "年份", "出口額(美元)", "進口額(美元)"},
                new String[]{"中國", "2017", "125468", "101109"},
                new String[]{"美國", "2017", "86452", "124298"},
        };
        //添加表格
        Table table = tb.getBody().addTable();
        //指定表格行數、列數
        table.resetCells(4,4);
        //將數組內容填充到表格
        for (int i = 0; i < data.length; i++) {
            TableRow dataRow = table.getRows().get(i);
            dataRow.getCells().get(i).setWidth(70);
            dataRow.setHeight(22);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            for (int j = 0; j < data[i].length; j++) {
                dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range2 = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                range2.getCharacterFormat().setFontName("楷體");
                range2.getCharacterFormat().setFontSize(11f);
                range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
                range2.getCharacterFormat().setBold(true);
            }
        }
        TableRow row = table.getRows().get(1);
        for (int z = 0; z < row.getCells().getCount(); z++) {
            row.getCells().get(z).getCellFormat().setBackColor(new Color(176,224,238));
        }
        //橫向合並單元格
        table.applyHorizontalMerge(0,0,3);
        //應用表格樣式
        table.applyStyle(DefaultTableStyle.Table_Grid_5);

        //保存文檔
        doc.saveToFile("AddTextbox.docx", FileFormat.Docx_2013);
        doc.dispose();
    }
}

文本框添加效果:

 

 

(本文完)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM