0
0

More than 1 year has passed since last update.

JavaでWord文書にフォームフィールドを追加する方法

Posted at

Word文書をフォームとして使用して多数の対象者から情報を収集することは、しばしば厄介なことがあり、収集者は要件に合わない、あるいは完全に間違っている情報を大量に入手してしまうことがあります。 しかし、文書にフォームフィールドを追加することで、要件に合わない情報を収集することを避けることができます。Word文書におけるフォームフィールドは、ユーザーが指定した種類のデータを入力するための対話型の文書要素で、入力された情報が正しいことを確認しながら情報収集を大幅に容易にすることができます。 この記事では、無料のFree Spire.Doc for Javaを使用したプログラムを通じて、Word文書にフォームフィールドを作成する方法を説明します。

【依存関係の追加】

この方法は、無料のFree Spire.PDF 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.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

2. 公式サイトよりJarファイルをダウンロード

まず、Free Spire.PDF for Javaの公式サイトよりzipファイルをダウンロードします。zipファイルを解凍し、libフォルダの下にあるSpire.Pdf.jarファイルを依存関係としてプロジェクトにインポートしてください。

Word文書にフォームフィールドを含む表を作成する

一般的なフォームフィールドには、テキストフォームフィールド、チェックボックスフォームフィールド、ドロップダウンリストフォームフィールドがあります。Spire.Doc for Javaでは、これらを表すTextFormFieldCheckBoxFormFieldDropDownFormFieldクラスがそれぞれ用意されています。これらのクラスの下にあるメソッドを使用して、フォームフィールドのプロパティを設定することができます。フォームフィールドを追加するための詳細な手順は以下の通りです。

  • Documentクラスのオブジェクトを作成し、Document.addSection()メソッドでセクションを追加します。
  • Section.addTable() メソッドでセクションにテーブルを追加し、Table.resetCells()メソッドで行番号と列番号を設定します。
  • Tabel.getRows().get().getCells().get() メソッドで特定のセルを取得し、TableCell.addParagraph().appendTextFormField() メソッドでセル内にテキストフォームフィールドを追加、TextFormField.setTextFieldType() でテキストの種類を設定しています。
  • Tabel.getRows().get().getCells().get() メソッドで特定のセルを取得し、TableCell.addParagraph().appendCheckBox() メソッドでチェックボックスフォームフィールドをセルに追加しています。
  • Tabel.getRows().get().getCells().get() メソッドで特定のセルを取得し、 TableCell.addParagraph().appendDropDownFormField() メソッドでセルにドロップダウンリストフォームフィールドを追加し、 DropDownFormField.getDropDownItems().add() メソッドを使用してリストに項目を追加しています。
  • Document.saveToFile() メソッドでドキュメントを保存します。

Java

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import com.spire.doc.documents.VerticalAlignment;
import com.spire.doc.fields.DropDownFormField;
import com.spire.doc.fields.TextFormField;

public class addFormFields {

    public static void main(String[] args) {

        //Documentクラスのオブジェクトを作成し、セクションを追加する
        Document doc = new Document();
        Section section = doc.addSection();

        //セクションに段落を追加する
        Paragraph title = section.addParagraph();
        title.appendText("\n\nフォームフィールド\n");
        title.getStyle().getCharacterFormat().setBold(true);
        title.getStyle().getCharacterFormat().setFontName("Yu Mincho");
        title.getStyle().getCharacterFormat().setFontSize(18);

        //セクションに表を追加する
        Table table = section.addTable(true);
        table.resetCells(3,2);

        //1列目のセルにテキストを追加し、書式を設定する
        Paragraph paragraph = table.getRows().get(0).getCells().get(0).addParagraph();
        paragraph.appendText("テキストのフォームフィールド");
        paragraph = table.getRows().get(1).getCells().get(0).addParagraph();
        paragraph.appendText("チェックボックスのフォームフィールド");
        paragraph = table.getRows().get(2).getCells().get(0).addParagraph();
        paragraph.appendText("ドロップダウンリストのフォームフィールド");

        //指定されたセルにテキストのフォームフィールドを追加し、そのテキストタイプを設定する
        TableCell cell1 = table.getRows().get(0).getCells().get(1);
        TextFormField textField = cell1.addParagraph().appendTextFormField("テキスト", "");
        textField.setTextFieldType(TextFormFieldType.Regular_Text);

        //指定したセルにチェックボックスのフォームフィールドを追加する
        TableCell cell2 = table.getRows().get(1).getCells().get(1);
        cell2.addParagraph().appendCheckBox("チェックボックス", true);

        //指定したセルにドロップダウンリストのフォームフィールドを追加し、リストに項目を追加する
        TableCell cell3 = table.getRows().get(2).getCells().get(1);
        DropDownFormField dropdownField = cell3.addParagraph().appendDropDownFormField("ドロップダウン リスト");
        dropdownField.getDropDownItems().add("項目1");
        dropdownField.getDropDownItems().add("項目2");
        dropdownField.getDropDownItems().add("項目3");

        //ParagraphStyleオブジェクトを作成し、各行のセルをループして書式を設定する
        ParagraphStyle style = new ParagraphStyle(doc);
        style.setName("newStyle");
        style.getCharacterFormat().setFontName("Yu Mincho");
        style.getCharacterFormat().setFontSize(14);
        doc.getStyles().add(style);
        for (int i = 0; i < table.getRows().getCount(); i++) {
            //行の高さを設定する
            table.getRows().get(i).setHeight(30f);
            for (Object cell:table.getRows().get(i).getCells()){
                if (cell instanceof TableCell)
                {
                    //セルの水平方向のアライメントを中央に設定する
                    ((TableCell) cell).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                    //セルに段落書式を適用する
                    ((TableCell) cell).getParagraphs().get(0).applyStyle(style.getName());
                }
            }
        }

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

2023-02-17_155028.png

フォームフィールドは、現在ではフォームを作るために使うことはあまりありません。コンテンツコントロールは、Word文書でフォームを作成するための新しい方法です。もし、あなたがSpire.Doc for Javaを使ってWord文書にコンテンツコントロールを使ったフォームを作成する方法を知りたければ、この記事を読んでください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0