チュートリアル

簡単にライブラリーを使用するためのチュートリアルコレクション

チュートリアル»Java»Spire.Doc for Java»フォームフィールド»Java:Word ドキュメントに記入可能なフォームを作成する方法
2023-05-05

Java:Word ドキュメントに記入可能なフォームを作成する方法

Word ドキュメントでは、他の人が情報を入力するために使用できるフォームを作成することができます。記入可能なフォームは、さまざまな用途で使用されます。人事部では、従業員やコンサルタントの情報を収集するためにフォームを使用します。営業部門は、製品やサービスに対する顧客の満足度を調査するためにフォームを使用します。組織は、会員、学生、顧客を登録するためにフォームを使用します。フォームを作成する際に使用するツールには、以下のようなものがあります:

  • コンテンツコントロール:フォームでユーザーが情報を入力する部分です。
  • :表は、テキストフィールドとフォームフィールドを揃えたり、枠線とボックスを作成したりするために使用されます。
  • 保護:ユーザーがフィールドに入力することはできますが、ドキュメントの残りの部分には変更を加えることができません。

Word ドキュメントのコンテンツコントロールは、ユーザーが構造化されたドキュメントを構築できるようにするためのコンテンツのコンテナです。構造化されたドキュメントでは、ドキュメント内でコンテンツが表示される場所を制御します。Word 2013 では、基本的に10種類のコンテンツコントロールが利用可能です。この記事では、Spire.Doc for Java を使用して、以下の7つの一般的なコンテンツコントロールで構成される Word ドキュメントに記入可能なフォームを作成する方法を中心に説明します。

コンテンツ コントロール 説明
テキスト テキストフィールドはプレーンテキストに限定されるため、書式を含めることはできません。
リッチ テキスト テキストフィールドで、フォーマットされたテキストや、テーブル、画像、その他のコンテンツコントロールなどのアイテムを含むことができます。
画像 1枚の画像を含むことができます。
ドロップダウン リスト ドロップダウンリストは、ユーザーが選択できるように、あらかじめ定義されたアイテムのリストを表示します。
コンボ ボックス コンボボックスは、ユーザーがリストの中からあらかじめ定義された値を選択したり、コントロールのテキストボックスに独自の値を入力したりすることができるものです。
チェック ボックス チェックボックスは、ユーザーが「はい(チェック済み)」か「いいえ(チェックなし)」かの二者択一を行うことができるグラフィカルなウィジェットです。
日付選択 ユーザーが日付を選択できるカレンダーコントロールが含まれる。

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>11.4.2</version>
    </dependency>
</dependencies>

Word ドキュメントに記入可能なフォームを作成する

Spire.Doc for Java が提供する StructureDocumentTagInline クラスは、段落内のインラインレベルの構造(DrawingML オブジェクト、フィールドなど)に対する構造化文書タグを作成するために使用する。このクラスの下にある SDTProperties プロパティと SDTContent プロパティは、現在の構造化文書タグのプロパティとコンテンツを指定するために使用されるものとする。以下は、Word ドキュメントにコンテンツコントロールを備えた記入可能なフォームを作成するための詳細な手順です。

  • Document のオブジェクトを作成します。
  • Document.addSection() メソッドを使用してセクションを追加します。
  • Section.addTable() メソッドを使用して、表を追加します。
  • TableCell.addParagraph() メソッドを使用して、特定の表セルに段落を追加します。
  • StructureDocumentTagInline のインスタンスを作成し、Paragraph.getChildObjects().add() メソッドを使用して、子オブジェクトとして段落に追加します。
  • StructureDocumentTagInline オブジェクトの SDTProperties プロパティと SDTContent プロパティの下にあるメソッドを使用して、構造化文書タグのプロパティとコンテンツを指定します。構造化文書タグの種類は、SDTProperties.setSDTType() メソッドで設定します。
  • Document.protect() メソッドを使用して、ユーザーがフォームフィールドの外のコンテンツを編集できないようにします。
  • Document.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import com.spire.pdf.graphics.PdfTrueTypeFont;

import java.util.Date;

public class CreateFillableForm {

    public static void main(String[] args) {

        //Documentのオブジェクトを作成する
        Document doc = new Document();

        //セクションを追加する
        Section section = doc.addSection();

        //表を追加する
        Table table = section.addTable(true);
        table.resetCells(7, 2);

        //1列目のセルにテキストを追加する
        Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
        paragraph.getStyle().getCharacterFormat().setFontName("Yu Gothic UI");
        paragraph.getStyle().getCharacterFormat().setFontSize(12);
        paragraph.appendText("テキスト コンテンツ コントロール");
        paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
        paragraph.appendText("リッチ テキスト コンテンツ コントロール");
        paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
        paragraph.appendText("画像コンテンツ コントロール");
        paragraph = table.getRows().get(3).getCells().get(0).addParagraph();
        paragraph.appendText("ドロップダウン リスト コンテンツ コントロール");
        paragraph = table.getRows().get(4).getCells().get(0).addParagraph();
        paragraph.appendText("チェック ボックス コンテンツ コントロール");
        paragraph = table.getRows().get(5).getCells().get(0).addParagraph();
        paragraph.appendText("コンボ ボックス コンテンツ コントロール");
        paragraph = table.getRows().get(6).getCells().get(0).addParagraph();
        paragraph.appendText("日付選択コンテンツ コントロール");

        //セル(0,1)にテキストコンテンツコントロールを追加する
        paragraph = table.getRows().get(0).getCells().get(1).addParagraph();
        StructureDocumentTagInline sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Text);
        sdt.getSDTProperties().setAlias("テキスト");
        sdt.getSDTProperties().setTag("テキスト");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        SdtText text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        TextRange tr = new TextRange(doc);
        tr.setText("ここをクリックまたはタップしてテキストを入力してください。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //セル(1,1)にリッチテキストコンテンツコントロールを追加する
        paragraph = table.getRows().get(1).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Rich_Text);
        sdt.getSDTProperties().setAlias("リッチ テキスト");
        sdt.getSDTProperties().setTag("リッチ テキスト");
        sdt.getSDTProperties().isShowingPlaceHolder(true);
        text = new SdtText(true);
        text.isMultiline(false);
        sdt.getSDTProperties().setControlProperties(text);
        tr = new TextRange(doc);
        tr.setText("ここをクリックまたはタップしてテキストを入力してください。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //セル(2,1)に画像コンテンツコントロールを追加する
        paragraph = table.getRows().get(2).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Picture);
        sdt.getSDTProperties().setAlias("画像");
        sdt.getSDTProperties().setTag("画像");
        SdtPicture sdtPicture = new SdtPicture();
        sdt.getSDTProperties().setControlProperties(sdtPicture);
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("C:/画像を選択.png");
        sdt.getSDTContent().getChildObjects().add(pic);

        //セル(3,1)にドロップダウンリストコンテンツコントロールを追加する
        paragraph = table.getRows().get(3).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        sdt.getSDTProperties().setSDTType(SdtType.Drop_Down_List);
        sdt.getSDTProperties().setAlias("ドロップダウン リスト");
        sdt.getSDTProperties().setTag("ドロップダウン リスト");
        paragraph.getChildObjects().add(sdt);
        SdtDropDownList sddl = new SdtDropDownList();
        sddl.getListItems().add(new SdtListItem("アイテムを選択してください。", "1"));
        sddl.getListItems().add(new SdtListItem("アイテム2", "2"));
        sddl.getListItems().add(new SdtListItem("アイテム3", "3"));
        sddl.getListItems().add(new SdtListItem("アイテム4", "4"));
        sdt.getSDTProperties().setControlProperties(sddl);
        tr = new TextRange(doc);
        tr.setText(sddl.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);

        //セル(4,1)にチェックボックスのコンテンツコントロールを2つ追加する
        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        SdtCheckBox scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText("オプション1");

        paragraph = table.getRows().get(4).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Check_Box);
        scb = new SdtCheckBox();
        sdt.getSDTProperties().setControlProperties(scb);
        tr = new TextRange(doc);
        sdt.getChildObjects().add(tr);
        scb.setChecked(false);
        paragraph.appendText("オプション2");

        //セル(5,1)にコンボボックスのコンテンツコントロールを追加する
        paragraph = table.getRows().get(5).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Combo_Box);
        sdt.getSDTProperties().setAlias("コンボ ボックス");
        sdt.getSDTProperties().setTag("コンボ ボックス");
        SdtComboBox cb = new SdtComboBox();
        cb.getListItems().add(new SdtListItem("アイテムを選択してください。"));
        cb.getListItems().add(new SdtListItem("アイテム2"));
        cb.getListItems().add(new SdtListItem("アイテム3"));
        sdt.getSDTProperties().setControlProperties(cb);
        tr = new TextRange(doc);
        tr.setText(cb.getListItems().get(0).getDisplayText());
        sdt.getSDTContent().getChildObjects().add(tr);

        //セル(6,1)に日付ピッカーコンテンツコントロールを追加する
        paragraph = table.getRows().get(6).getCells().get(1).addParagraph();
        sdt = new StructureDocumentTagInline(doc);
        paragraph.getChildObjects().add(sdt);
        sdt.getSDTProperties().setSDTType(SdtType.Date_Picker);
        sdt.getSDTProperties().setAlias("日付選択");
        sdt.getSDTProperties().setTag("日付選択");
        SdtDate date = new SdtDate();
        date.setCalendarType(CalendarType.Default);
        date.setDateFormat("yyyy.MM.dd");
        date.setFullDate(new Date());
        sdt.getSDTProperties().setControlProperties(date);
        tr = new TextRange(doc);
        tr.setText("クリックまたはタップして日付を入力してください。");
        sdt.getSDTContent().getChildObjects().add(tr);

        //ユーザーがフォームフィールドを編集することのみを許可する
        doc.protect(ProtectionType.Allow_Only_Form_Fields, "password");

        //ドキュメントを保存する
        doc.saveToFile("記入用フォーム.docx", FileFormat.Docx_2013);
    }
}

Java:Word ドキュメントに記入可能なフォームを作成する方法

一時ライセンスを申請する

結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。

Read 367 times