亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過Java添加Word文本框過程詳解

 更新時間:2019年07月09日 14:40:18   作者:Miaonly  
這篇文章主要介紹了通過Java添加Word文本框過程詳解,在Word中,文本框是指一種可移動、可調節(jié)大小的文字或圖形容器。我們可以向文本框中添加文字、圖片、表格等對象,下面,將通過Java編程來實現(xiàn)添加以上對象到Word文本框,需要的朋友可以參考下

前言

在Word中,文本框是指一種可移動、可調節(jié)大小的文字或圖形容器。我們可以向文本框中添加文字、圖片、表格等對象,下面,將通過Java編程來實現(xiàn)添加以上對象到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) {
    //創(chuàng)建文檔
    Document doc = new Document();

    //添加指定大小的文本框
    TextBox tb = doc.addSection().addParagraph().appendTextBox(380, 280);
    //設置文字環(huán)繞方式
    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("tp.png");
    picture.setHeight(120f);
    picture.setWidth(180f);
    para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
    para.getFormat().setAfterSpacing(13f);

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

    //添加表格到文本框
    //聲明數(shù)組內容
    String[][] data = new String[][]{
        new String[]{"國家", "年份", "出口額(美元)", "進口額(美元)"},
        new String[]{"中國", "2017", "125468", "101109"},
        new String[]{"美國", "2017", "86452", "124298"},
    };
    //添加表格
    Table table = tb.getBody().addTable();
    //指定表格行數(shù)、列數(shù)
    table.resetCells(3,4);
      //將數(shù)組內容填充到表格
      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);
        }
      }
      //應用表格樣式
      table.applyStyle(DefaultTableStyle.Colorful_Grid_Accent_3);

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

文本框添加效果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論