テキストボックスは、Word ドキュメント内の任意の場所に挿入できるテキストや画像の入れ物です。 MS Word には、あらかじめいくつかのテキストボックスのスタイルが用意されていますが、独自のアイデアで読者を魅了するカスタム外観のテキストボックスを作成することも可能です。 この記事では、Spire.Doc for Java を使用して Word ドキュメントにテキストボックスを追加または削除する方法について説明します。 Word ドキュメントにテキストボックスを挿入する Word ドキュメントからテキストボックスを削除する Spire.Doc for Java をインストールします まず、Spire. Doc for Java を Java プロジェクトに追加する必要があります。JAR ファイルは、このリンクからダウンロードできます。Maven を使用する場合は、次のコードをプロジェクトの pom.xml ファイルに追加する必要があります。 <repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url> https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>10.7.10</version> </dependency> </dependencies> Word ドキュメントにテキストボックスを挿入する Spire.Doc for Java には、指定した段落にテキストボックスを挿入する Paragraph.appendTextBox(float width, float height) というメソッドが用意されています。 以下は、その詳細な手順です。 Document クラスのインスタンスを作成し、Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getSections().get() メソッドを使用して最初のセクションを取得し、Section.getParagraphs().get() メソッドを使用してセクション内の段落を取得します。 Paragraph.appendTextBox(float width, float height) メソッドを使用して、段落にテキストボックスを追加します。 TextBox.getFormat() メソッドを使用してテキストボックスのフォーマットを取得し、TextBoxFormat クラスのメソッドを使用して、テキストボックスのテキストの折り返しタイプ、位置、ボーダー色、塗りつぶし色を設定します。 TextBox.getBody().addParagraph() メソッドを使用してテキストボックスに段落を追加し、Paragraph.appendPicture() メソッドを使用して段落に画像を挿入しています。 テキストボックスに Paragraph.appendText() メソッドを使用してテキストを挿入し、テキストのフォントを設定します。 Document.saveToFile()を使用して、ドキュメントを保存します。 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 InsertTextBox { public static void main(String[] args) { //Document クラスのオブジェクトを作成する Document doc = new Document(); //Wordドキュメントを読み込む doc.loadFromFile("C:/例.docx"); //テキストボックスを作成し、テキスト折り返しを設定する TextBox tb = doc.getSections().get(0).getParagraphs().get(0).appendTextBox(120f, 230f); tb.getFormat().setTextWrappingStyle(TextWrappingStyle.Square); //テキストボックスの位置を設定する tb.getFormat().setHorizontalOrigin(HorizontalOrigin.Right_Margin_Area); tb.getFormat().setHorizontalPosition(-100f); tb.getFormat().setVerticalOrigin(VerticalOrigin.Page); tb.getFormat().setVerticalPosition(165f); //テキストボックスのボーダーの色と塗りつぶしの色を設定する tb.getFormat().setLineColor(Color.BLUE); tb.getFormat().setFillColor(new Color(203,234,253) ); //テキストボックスに画像を挿入する Paragraph para = tb.getBody().addParagraph(); DocPicture picture = para.appendPicture("C:/画像.jpg"); //段落の配置を設定する para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //画像サイズを設定する picture.setHeight(90f); picture.setWidth(90f); //テキストボックスに文字を挿入する para = tb.getBody().addParagraph(); TextRange textRange = para.appendText("考古学者が発掘にいそしむ一方で、イアンは「ポストプロセス」考古学というポストモダンなアプローチを構築しています。"); //段落の配置を設定する para.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //フォントを設定する textRange.getCharacterFormat().setFontName("Yu Mincho"); textRange.getCharacterFormat().setFontSize(9f); textRange.getCharacterFormat().setItalic(true); //ドキュメントを保存する doc.saveToFile("テキストボックスの挿入.docx", FileFormat.Docx_2013); } } Word ドキュメントからテキストボックスを削除する Spire.Doc for Java には、指定したテキストボックスを削除する Document.getTextBoxes().removeAt() メソッドが用意されています。 すべてのテキストボックスを削除したい場合は、Document.getTextBoxes().clear() メソッドで削除できます。 テキストボックスを削除する手順は、次のとおりです。 Document クラスのオブジェクトを作成します。 Document.loadFromFile() メソッドを使用して Word ドキュメントを読み込みます。 Document.getTextBoxes().removeAt() メソッドを使用して、最初のテキストボックスを削除します。 Document.saveToFile() メソッドを使用してドキュメントを保存します。 Java import com.spire.doc.Document; import com.spire.doc.FileFormat; public class RemoveTextBox { public static void main(String[] args) { //Documentクラスのオブジェクトを作成する Document doc = new Document(); //Wordドキュメントを読み込む doc.loadFromFile("C:/例.docx"); //テキストボックスをインデックスで削除する doc.getTextBoxes().removeAt(0); //すべてのテキストボックスを削除する //doc.getTextBoxes().clear(); //ドキュメントを保存する doc.saveToFile("テキストボックスの削除.docx", FileFormat.Docx); } } 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。