##背景
Word文書の中で、テキストボックスとは、移動することが可能で、サイズの調整も可能なテキスト、またはグラフィックコンテナというものを指します。テキストボックスにテキスト、画像、テーブル、およびその他のオブジェクトを追加できます。以下では、Javaプログラミングを使用して上記のオブジェクトをWordテキストボックスに追加する方法をご紹介致します。
###使用ツール:Free Spire.Doc for Java(無料版)
JARファイルの取得とインポート:
公式Webサイトからjarパッケージを**ダウンロード**します。ダウンロードした後、ファイルを解凍し、libフォルダー内のSpire.Doc.jarファイルをJavaプログラムにインポートします。(以下の画像の示すように)
##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("C:\\Users\\Administrator\\Pictures\\image.png");
picture.setHeight(120f);
picture.setWidth(180f);
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
para.getFormat().setAfterSpacing(13f);
//テキストボックスにテキストを挿入する
para = tb.getBody().addParagraph();
TextRange textRange = para.appendText("C#(シーシャープ)は、アンダース・ヘルスバーグが設計したプログラミング言語であり、"
+ "構文はその名前にもある通りC系言語(C言語、C++やJavaなど)の影響があるが、"
+ "構文以外の言語機能などについてはヘルスバーグが以前の所属であるボーランドで設計したDelphiからの影響がある。");
textRange.getCharacterFormat().setFontName("Yu Mincho");
textRange.getCharacterFormat().setFontSize(11f);
para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//テキストボックスにテーブルを追加する
//配列の内容を宣言する
String[][] data = new String[][]{
new String[]{"例"},
new String[]{"名前", "年齢", "出身", "誕生日"},
new String[]{"田中光一", "21", "鳥取", "2000/7/15"},
new String[]{"木村直哉", "22", "福岡", "1999/2/13"},
};
//テーブルを追加する
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("Yu Mincho");
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();
}
}
以上はJavaを通してWordにテキストボックスを追加する方法でした、最後まで読んでいただき、ありがとうございます。