Word文書におけるコンテンツ コントロールは、日付、リスト、書式付きテキストなど、特定のコンテンツのための容器です。これらは、特定の種類のコンテンツの入力や表示を容易にするため、テンプレートやフォームなどの文書を作成する際によく使用されます。例えば、日付選択コンテンツ コントロールは、ポップアップ ウィンドウで日付を選択するだけで入力でき、既定の書式またはカスタマイズされた書式で日付を表示することができます。この記事では、無料のFree Spire.Doc for Javaの助けを借りて、Word文書にコンテンツ コントロールを追加する方法を紹介します。
【依存関係の追加】
この方法は、無料のFree Spire.Doc for Javaが必要ですので、先にjarファイルをインポートしてください。
1. Maven
Maven を使用している場合、プロジェクトの pom.xml ファイルに以下のコードを追加することで、簡単にアプリケーションに JAR ファイルをインポートすることができます。
<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.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
2. 公式サイトよりJarファイルをダウンロード
まず、Free Spire.Doc for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Doc.jarファイルを依存関係としてプロジェクトにインポートしてください。
Word文書に日付選択コンテンツ コントロールを追加する
ドロップダウン リスト型のコンテンツ コントロールは、編集する内容を制限し、リスト内のデータのみを選択して入力できるため、記入が容易で、入力データの正確性を確保することができます。
Word文書にドロップダウン リストを追加する手順は、以下のとおりです。
- Document クラスのオブジェクトを作成します。
- Document.loadFromFile() メソッドを使用して、Word文書をディスクから読み込みます。
- 文書に段落を追加します。
- コンテンツコントロールを作成します。
- 作成した段落に、Paragraph.getChildObjects().add() メソッドを用いて、コンテンツコントロールを挿入します。
- StructuredDocumentTagInline.getSDTProperties().setSDTType() メソッドを用いて、コンテンツ コントロールをドロップダウン リストに設定します。
- StructuredDocumentTagInline.getSDTProperties().setControlProperties() メソッドを使用してドロップダウン リストのオプションを追加します。
- StructuredDocumentTagInline.getSDTContent().getChildObjects().add() メソッドを使用して、ドロップダウン リストに表示されるオプションを設定します。
- Document.saveToFIle() メソッドを使用して、ドキュメントを保存します。
Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class addDropDownList {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Word文書の読み込み
document.loadFromFile("コンテンツ コントロール.docx");
//文書に段落を追加する
Section section = document.getSections().get(0);
Paragraph paragraph = section.addParagraph();
TextRange textRange = paragraph.appendText("ドロップダウン リスト:");
textRange.getCharacterFormat().setFontSize(14);
textRange.getCharacterFormat().setFontName("Yu Mincho");
//コンテンツ コントロールを作成する
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
//追加された段落にコンテンツ コントロールを挿入する
paragraph.getChildObjects().add(sd);
//コンテンツ コントロールをドロップダウン リストとして設定する
sd.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
//ドロップダウン リストにオプションを追加する
SdtDropDownList sdd1 = new SdtDropDownList();
sdd1.getListItems().add(new SdtListItem("オプション1"));
sdd1.getListItems().add(new SdtListItem("オプション2"));
sdd1.getListItems().add(new SdtListItem("オプション3"));
sd.getSDTProperties().setControlProperties(sdd1);
//ドロップダウン リストに表示するオプションを設定する
TextRange textRange1 = new TextRange(document);
textRange1.setText(sdd1.getListItems().get(1).getDisplayText());
sd.getSDTContent().getChildObjects().add(textRange1);
//文書を保存する
document.saveToFile("ドロップダウン リスト.docx");
}
}
【結果のWord文書】
Word文書にコンボ ボックス コンテンツ コントロールを追加する
コンボ ボックス型のコンテンツ コントロールは、テキスト ボックスとドロップダウン リストを組み合わせたもので、ユーザーは表示する項目の選択とリストへのコンテンツの入力の両方を行うことができます。
Word文書にコンボ ボックスを追加する手順は、以下の通りです。
- Document クラスのオブジェクトを作成します。
- Document.loadFromFile() メソッドを使用して、Word文書をディスクから読み込みます。
- 文書に段落を追加します。
- コンテンツコントロールを作成します。
- 作成した段落に、Paragraph.getChildObjects().add() メソッドを用いて、コンテンツコントロールを挿入します。
- StructuredDocumentTagInline.getSDTProperties().setSDTType() メソッドを用いて、コンテンツ コントロールをコンボ ボックスに設定します。
- SdtComboBox クラスのオブジェクトを作成します。
- SdtComboBox. getListItems().add() メソッドを使用して、コンボ ボックスへのオプションを設定します。
- StructureDocumentTagInline.getSDTProperties().setControlProperties() メソッドを使用して、コンボ ボックスにオプションを挿入します。
- StructureDocumentTagInline.getSDTContent().getChildObjects().add() メソッドを使用して、コンボ ボックスに表示するオプションを設定します。
- Document.saveToFile() メソッドを使用して文書を保存します。
Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
public class addComboBox {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Word文書の読み込み
document.loadFromFile("コンテンツ コントロール.docx");
//文書に段落を追加する
Section section = document.getSections().get(0);
Paragraph paragraph = section.addParagraph();
TextRange textRange = paragraph.appendText("コンボ ボックス:");
textRange.getCharacterFormat().setFontSize(14);
textRange.getCharacterFormat().setFontName("Yu Mincho");
//コンテンツ コントロールを作成する
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
//追加された段落にコンテンツ コントロールを挿入する
paragraph.getChildObjects().add(sd);
//コンテンツ コントロールをドロップダウン リストとして設定する
sd.getSDTProperties().setSDTType(SdtType.Combo_Box);
//SdtComboBox クラスのオブジェクトを作成する
SdtComboBox sc = new SdtComboBox();
//コンボ ボックスへのオプションを設定する
sc.getListItems().add(new SdtListItem("00001"));
sc.getListItems().add(new SdtListItem("00002"));
sc.getListItems().add(new SdtListItem("00003"));
//コンボ ボックスにオプションを挿入する
sd.getSDTProperties().setControlProperties(sc);
//コンボ ボックスに表示するオプションを設定する
TextRange textRange1 = new TextRange(document);
textRange1.setText(sc.getListItems().get(1).getDisplayText());
sd.getSDTContent().getChildObjects().add(textRange1);
//文書を保存する
document.saveToFile("コンボ ボックス.docx");
}
}
【結果のWord文書】
Word文書に日付選択コンテンツ コントロールを追加する
日付選択コンテンツ コントロールにより、日付データの記入や表示が簡単にできます。
以下は、日付選択コントロールを追加する手順です。
- Document クラスのオブジェクトを作成します。
- Document.loadFromFile() メソッドを使用して、Word文書をディスクから読み込みます。
- 文書に段落を追加します。
- コンテンツコントロールを作成します。
- 作成した段落に、Paragraph.getChildObjects().add() メソッドを用いて、コンテンツコントロールを挿入します。
- StructuredDocumentTagInline.getSDTProperties().setSDTType() メソッドを用いて、コンテンツ コントロールを日付選択コントロールに設定します。
- SdtDate.setCalendarType() メソッドを使用して、カレンダーの種類を設定します。
- SdtDate.setDateFormat() メソッドを使用して、日付の書式を設定します。
- StructureDocumentTagInline.getSDTProperties().setControlProperties() メソッドを使用して、日付選択コントロールに日付を挿入します。
- StructureDocumentTagInline.getSDTContent().getChildObjects().add() メソッドを使用して、表示する日付を設定します。
- Document.saveToFIle() メソッドを使用して、ドキュメントを保存します。
Java
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import com.spire.ms.System.DateTime;
import java.util.Date;
public class addDatePicker {
public static void main(String[] args) {
//Documentクラスのオブジェクトを作成する
Document document = new Document();
//Word文書の読み込み
document.loadFromFile("コンテンツ コントロール.docx");
//文書に段落を追加する
Section section = document.getSections().get(0);
Paragraph paragraph = section.addParagraph();
TextRange textRange = paragraph.appendText("日付選択コントロール:");
textRange.getCharacterFormat().setFontSize(14);
textRange.getCharacterFormat().setFontName("Yu Mincho");
//コンテンツ コントロールを作成する
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
//追加された段落にコンテンツ コントロールを挿入する
paragraph.getChildObjects().add(sd);
//コンテンツ コントロールを日付選択コントロールとして設定する
sd.getSDTProperties().setSDTType(SdtType.Date_Picker);
//カレンダーの種類を設定する
SdtDate date = new SdtDate();
date.setCalendarType(CalendarType.Japan);
//日付の書式を設定する
date.setDateFormat("yyyy.MM.dd");
//日付選択コントロールに日付を挿入する
date.setFullDate(new Date());
sd.getSDTProperties().setControlProperties(date);
//表示する日付を設定する
TextRange textRange1 = new TextRange(document);
textRange1.setText("2022.12.16");
sd.getSDTContent().getChildObjects().add(textRange1);
//ドキュメントを保存する
document.saveToFile("日付選択コントロール.docx");
}
}
【結果のWord文書】