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

通過(guò)Java添加Word文本框過(guò)程詳解

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

前言

在Word中,文本框是指一種可移動(dòng)、可調(diào)節(jié)大小的文字或圖形容器。我們可以向文本框中添加文字、圖片、表格等對(duì)象,下面,將通過(guò)Java編程來(lái)實(shí)現(xiàn)添加以上對(duì)象到Word文本框。

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

Jar文件獲取及導(dǎo)入:

方法1:通過(guò)官網(wǎng)下載獲取jar包。下載后,解壓文件,并將lib文件夾下的Spire.Doc.jar文件導(dǎo)入Java程序。(如下圖)

方法2:通過(guò)maven倉(cāng)庫(kù)安裝導(dǎo)入。

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);
    //設(shè)置文字環(huán)繞方式
    tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square);
    //設(shè)置文本框的相對(duì)位置
    tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Left_Margin_Area);
    tb.getFormat().setHorizontalPosition(120f);
    tb.getFormat().setVerticalOrigin(VerticalOrigin.Page);
    tb.getFormat().setVerticalPosition(100f);
    //設(shè)置文本框邊框樣式
    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("中美貿(mào)易爭(zhēng)端,又稱中美貿(mào)易戰(zhàn),也叫中美貿(mào)易摩擦,是中美經(jīng)濟(jì)關(guān)系中的重要問(wèn)題。 "
        + "貿(mào)易爭(zhēng)端主要發(fā)生在兩個(gè)方面:一是中國(guó)具有比較優(yōu)勢(shì)的出口領(lǐng)域;"
        + "二是中國(guó)沒(méi)有優(yōu)勢(shì)的進(jìn)口和技術(shù)知識(shí)領(lǐng)域。");
    textRange.getCharacterFormat().setFontName("楷體");
    textRange.getCharacterFormat().setFontSize(11f);
    para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

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

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

文本框添加效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論