チュートリアル

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

チュートリアル»Java»Spire.PDF for Java»フォームフィールド»Java:PDF 文書に入力可能なフォームフィールドを作成、入力、削除する方法
2023-02-01

Java:PDF 文書に入力可能なフォームフィールドを作成、入力、削除する方法

入力可能な PDF フォームは、ユーザーからデータを収集するのに便利です。PDF はビジネスの中で最も人気のあるファイル形式の1つになっているので、インタラクティブな、記入可能な PDF フォームを作成することができることは重要です。この記事は、Spire.PDF for Java を使って、PDF に記入可能なフォームフィールドを作成、記入、削除する方法を示します。

Spire.PDF for Java は com.spire.pdf.fields 名前空間の下で一連の有用なクラスを提供します。これらのクラスは、開発者がテキスト ボックス、チェック ボックス、コンボ ボックス、リスト ボックス、ラジオ ボタンなどの様々な種類のフォームフィールドを作成したり編集したりすることを可能にします。下の表は、フォームフィールドに関連するいくつかの主要なクラスの一覧です。

クラス 説明
PdfForm PDF文書の対話型のフォームを表します。
PdfField PDF文書の対話型フォームのフィールドを表します。
PdfTextBoxField PDFフォームのテキスト ボックス フィールドを表します。
PdfCheckBoxField PDFフォームのチェック ボックス フィールドを表します。
PdfComboBoxField PDFフォームのコンボ ボックス フィールドを表します。
PdfListBoxField PDFフォームのリスト ボックス フィールドを表します。
PdfListFieldItem リスト フィールドの項目を表します。
PdfRadioButtonListField PDFフォームのラジオ ボタン フィールドを表します。
PdfRadioButtonListItem ラジオ ボタン リストの項目を表します。
PdfButtonField PDFフォームのボタン フィールドを表します。
PdfSignatureField PDFフォームの署名フィールドを表します。

Spire.PDF for Java をインストールします

まず、Spire. PDF 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.pdf</artifactId>
        <version>8.12.6</version>
    </dependency>
</dependencies>

PDF 文書に入力可能なフォームフィールドを作成する

PDF フォームを作成するには、対応するフィールドクラスのインスタンスを初期化します。setBounds() メソッドを使用してドキュメント内の特定のフィールドのサイズと位置を指定し、次に PdfFormFieldCollection.add() メソッドを使用して PDF にそれを追加します。以下は、Spire.PDF for Java を使用して PDF 文書内に様々な種類のフォームフィールドを作成する手順です。

  • PdfDocument クラスのオブジェクトを作成します。
  • PdfDocuemnt.getPages().add() メソッドを使用して、ページを追加します。
  • PdfTextBoxField クラスのオブジェクトを作成し、Bounds、Font、Text などのフィールドのプロパティを設定し、PdFormfFieldCollection.add() メソッドを使用してそれをドキュメントに追加します。
  • 手順3を繰り返して、チェック ボックス、コンボ ボックス、リスト ボックス、ラジオ ボタン、署名フィールド、ボタンをドキュメントに追加します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.actions.PdfSubmitAction;
import com.spire.pdf.fields.*;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

public class createFillableFormFields {

    public static void main(String[] args) throws Exception {

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

        //ページを追加する
        PdfPageBase page = doc.getPages().add();

        //x、y座標を初期化する
        float baseX = 100;
        float baseY = 30;

        //2つのブラシのオブジェクトを作成する
        PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.blue));
        PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.black));

        //フォントを作成する
        PdfCjkStandardFont font = new PdfCjkStandardFont(PdfCjkFontFamily.Heisei_Mincho_W_3, 14f);

        //テキスト ボックスを追加する
        page.getCanvas().drawString("テキスト ボックス:", font, brush1, new Point2D.Float(10, baseY));
        Rectangle2D.Float tbxBounds = new Rectangle2D.Float(baseX+40, baseY, 150, 15);
        PdfTextBoxField textBox = new PdfTextBoxField(page, "テキスト ボックス");
        textBox.setBounds(tbxBounds);
        textBox.setText("Hello World");
        textBox.setFont(font);
        doc.getForm().getFields().add(textBox);
        baseY += 30;

        //チェック ボックスを2つ追加する
        page.getCanvas().drawString("チェック ボックス:", font, brush1, new Point2D.Float(10, baseY));
        Rectangle2D.Float checkboxBound1 = new Rectangle2D.Float(baseX+130, baseY, 15, 15);
        PdfCheckBoxField checkBoxField1 = new PdfCheckBoxField(page, "チェック ボックス 1");
        checkBoxField1.setBounds(checkboxBound1);
        checkBoxField1.setChecked(false);
        page.getCanvas().drawString("オプション 1", font, brush2, new Point2D.Float(baseX + 40, baseY));

        Rectangle2D.Float checkboxBound2 = new Rectangle2D.Float(baseX + 250, baseY, 15, 15);
        PdfCheckBoxField checkBoxField2 = new PdfCheckBoxField(page, "チェック ボックス 2");
        checkBoxField2.setBounds(checkboxBound2);
        checkBoxField2.setChecked(false);
        page.getCanvas().drawString("オプション 2", font, brush2, new Point2D.Float(baseX + 160, baseY));
        doc.getForm().getFields().add(checkBoxField1);
        doc.getForm().getFields().add(checkBoxField2);
        baseY += 30;

        //リスト ボックスを追加する
        page.getCanvas().drawString("リスト ボックス:", font, brush1, new Point2D.Float(10, baseY));
        Rectangle2D.Float listboxBound = new Rectangle2D.Float(baseX+30, baseY, 150, 50);
        PdfListBoxField listBoxField = new PdfListBoxField(page, "リスト ボックス");
        listBoxField.getItems().add(new PdfListFieldItem("項目 1", "項目1"));
        listBoxField.getItems().add(new PdfListFieldItem("項目 2", "項目2"));
        listBoxField.getItems().add(new PdfListFieldItem("項目 3", "項目3")); ;
        listBoxField.setBounds(listboxBound);
        listBoxField.setFont(font);
        listBoxField.setSelectedIndex(0);
        doc.getForm().getFields().add(listBoxField);
        baseY += 65;

        //ラジオ ボタンを2つ追加する
        page.getCanvas().drawString("ラジオ ボタン:", font, brush1, new Point2D.Float(10, baseY));
        PdfRadioButtonListField radioButtonListField = new PdfRadioButtonListField(page, "ラジオ");
        PdfRadioButtonListItem radioItem1 = new PdfRadioButtonListItem("オプション1");
        Rectangle2D.Float radioBound1 = new Rectangle2D.Float(baseX+20, baseY, 15, 15);
        radioItem1.setBounds(radioBound1);
        page.getCanvas().drawString("オプション 1", font, brush2, new Point2D.Float(baseX + 40, baseY));

        PdfRadioButtonListItem radioItem2 = new PdfRadioButtonListItem("オプション2");
        Rectangle2D.Float radioBound2 = new Rectangle2D.Float(baseX + 150, baseY, 15, 15);
        radioItem2.setBounds(radioBound2);
        page.getCanvas().drawString("オプション 2", font, brush2, new Point2D.Float(baseX + 170, baseY));
        radioButtonListField.getItems().add(radioItem1);
        radioButtonListField.getItems().add(radioItem2);
        radioButtonListField.setSelectedIndex(0);
        doc.getForm().getFields().add(radioButtonListField);
        baseY += 30;

        //コンボ ボックスを追加する
        page.getCanvas().drawString("コンボ ボックス:", font, brush1, new Point2D.Float(10, baseY));
        Rectangle2D.Float cmbBounds = new Rectangle2D.Float(baseX+30, baseY, 150, 15);
        PdfComboBoxField comboBoxField = new PdfComboBoxField(page, "コンボ ボックス");
        comboBoxField.setBounds(cmbBounds);
        comboBoxField.getItems().add(new PdfListFieldItem("項目 1", "項目1"));
        comboBoxField.getItems().add(new PdfListFieldItem("項目 2", "項目2"));
        comboBoxField.getItems().add(new PdfListFieldItem("項目 3", "項目3"));
        comboBoxField.getItems().add(new PdfListFieldItem("項目 4", "項目4"));
        comboBoxField.setSelectedIndex(0);
        comboBoxField.setFont(font);
        doc.getForm().getFields().add(comboBoxField);
        baseY += 30;

        //署名フィールドを追加する
        page.getCanvas().drawString("署名フィールド:", font, brush1, new Point2D.Float(10, baseY));
        PdfSignatureField sgnField = new PdfSignatureField(page, "署名フィールド");
        Rectangle2D.Float sgnBounds = new Rectangle2D.Float(baseX+30, baseY, 150, 80);
        sgnField.setBounds(sgnBounds);
        doc.getForm().getFields().add(sgnField);
        baseY += 95;

        //ボタンを追加する
        page.getCanvas().drawString("ボタン:", font, brush1, new Point2D.Float(10, baseY));
        Rectangle2D.Float btnBounds = new Rectangle2D.Float(baseX-25, baseY, 50, 15);
        PdfButtonField buttonField = new PdfButtonField(page, "ボタン");
        buttonField.setBounds(btnBounds);
        buttonField.setText("コミット");
        buttonField.setFont(font);
        PdfSubmitAction submitAction = new PdfSubmitAction("https://www.e-iceblue.com/getformvalues.php");
        buttonField.getActions().setMouseDown(submitAction);
        doc.getForm().getFields().add(buttonField);

        //ドキュメントを保存する
        doc.saveToFile("入力可能なフォーム.pdf", FileFormat.PDF);
    }
}

Java:PDF 文書に入力可能なフォームフィールドを作成、入力、削除する方法

既存の PDF 文書内のフォームフィールドに入力する

フォームに入力するためには、まず PDF 文書から全てのフォームフィールドを取得し、あるフィールドのタイプを決定し、それから値を入力するか、またはあらかじめ定義されたリストから値を選択する必要があります。以下は、Spire.PDF for Java を使用して、既存の PDF 文書にフォームフィールドを入力する手順です。

  • PdfDocument クラスのオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • PdfDocument.getForm() メソッドを使用して、ドキュメントからフォームを取得します。
  • PdfFormWidget.getFieldsWidget() メソッドを使用して、フォームフィールドウィジェットのコレクションを取得します。
  • フィールドウィジェットのコレクションをループして、特定の PdfField オブジェクトを取得します。
  • PdfField オブジェクトがテキスト ボックスのような特定のフィールドの種類であるかどうかを判断します。もしそうなら、PdfTextBoxFieldWidget.setText() メソッドを使用して、テキストボックスのテキストを設定します。
  • この判断を繰り返して、ラジオ ボタン、チェック ボックス、コンボ ボックス、リスト ボックスを入力します。
  • PdfDocument.saveToFile() メソッドを使用して、ドキュメントを保存します。
  • Java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.*;

public class fillFormFields {

    public static void main(String[] args) {

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

        //フォームを含むPDF文書を読み込む
        doc.loadFromFile("フォーム.pdf");

        //文書からフォームを取得する
        PdfFormWidget form = (PdfFormWidget)doc.getForm();

        //フォーム ウィジェットのコレクションを取得する
        PdfFormFieldWidgetCollection formWidgetCollection = form.getFieldsWidget();

        //ウィジェットをループする
        for (int i = 0; i < formWidgetCollection.getCount(); i++)
        {
            //指定のフィールドを取得する
            PdfField field = formWidgetCollection.get(i);

            //フィールドがテキスト ボックスかどうかを判断する
            if (field instanceof PdfTextBoxFieldWidget)
            {
                if (field.getName().equals("名前"))
                {
                    //テキスト ボックスのテキストを設定する
                    PdfTextBoxFieldWidget textBoxField = (PdfTextBoxFieldWidget)field;
                    textBoxField.setText("ジョン・スミス");
                }
            }

            //フィールドがラジオ ボタンかどうかを判断する
            if (field instanceof PdfRadioButtonListFieldWidget)
            {
                if (field.getName().equals("性別"))
                {
                    //ラジオ ボタンの選択したインデックスを設定する
                    PdfRadioButtonListFieldWidget radioButtonListField = (PdfRadioButtonListFieldWidget)field;
                    radioButtonListField.setSelectedIndex(0);
                }
            }

            //フィールドがコンボ ボックスかどうかを判断する
            if (field instanceof PdfComboBoxWidgetFieldWidget)
            {
                if (field.getName().equals("国名"))
                {
                    //コンボ ボックスの選択したインデックスを設定する
                    PdfComboBoxWidgetFieldWidget comboBoxField = (PdfComboBoxWidgetFieldWidget)field;
                    comboBoxField.setSelectedIndex(1);
                }
            }

            //フィールドがチェック ボックスかどうか判断する
            if (field instanceof PdfCheckBoxWidgetFieldWidget)
            {
                //チェックボックスを「チェック済み」に設定する
                PdfCheckBoxWidgetFieldWidget checkBoxField = (PdfCheckBoxWidgetFieldWidget)field;
                switch (checkBoxField.getName())
                {
                    case "音楽":
                    case "映画":
                        checkBoxField.setChecked(true);
                        break;
                }
            }

            //フィールドがリスト ボックスかどうかを判断する
            if (field instanceof PdfListBoxWidgetFieldWidget)
            {
                if (field.getName().equals("学位"))
                {
                    //リスト ボックスの選択したインデックスを設定する
                    PdfListBoxWidgetFieldWidget listBox = (PdfListBoxWidgetFieldWidget)field;
                    listBox.setSelectedIndex(1);
                }
            }
        }

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

Java:PDF 文書に入力可能なフォームフィールドを作成、入力、削除する方法

既存の PDF 文書内の特定のフィールド/すべてのフィールドを削除する

PDF 文書内のフォームフィールドは、そのインデックスまたは名前によってアクセスすることができ、PdfFieldCollection.remove() メソッドによって削除することができます。以下は、Sprie.PDF for Java を使用して、既存の PDF 文書から特定のフィールドまたはすべてのフィールドを削除する手順です。

  • PdfDocument クラスのオブジェクトを作成します。
  • PdfDocument.loadFromFile() メソッドを使用して PDF ドキュメントを読み込みます。
  • PdfDocument.getForm() メソッドを使用して、ドキュメントからフォームを取得します。
  • PdfFormWidget.getFieldsWidget() メソッドを使用して、フォーム フィールド ウィジェットのコレクションを取得します。
  • ウィジェットのコレクションをループして、特定の PdfField オブジェクトを取得します。PdfFieldCollection.remove() メソッドを使用して、フィールドを順に削除します。
  • 特定のフォームフィールドを削除するには、PdfFormFieldWidgetCollection.get() メソッドを使用してドキュメントからそれを取得し、PdfFieldCollection.remove() メソッドを呼び出してそれを削除していきます。
  • PdfDocument.saveToFile() メソッドを用いてドキュメントを保存します。
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormFieldWidgetCollection;
import com.spire.pdf.widget.PdfFormWidget;

public class deleteFormFields {

    public static void main(String[] args) {

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

        //PDFファイルを読み込む
        doc.loadFromFile("入力可能なフォーム.pdf");

        //ドキュメントからフォームを取得する
        PdfFormWidget form= (PdfFormWidget)doc.getForm();

        //フォームからフォーム ウィジェットを取得する
        PdfFormFieldWidgetCollection widgets = form.getFieldsWidget();

        //ウィジェットをループする
        for (int i = widgets.getCount() - 1; i >= 0; i--)
        {
            //指定のフィールドを取得する
            PdfField field = (PdfField)widgets.getList().get(i) ;

            //フィールドを削除する
            widgets.remove(field);
        }

        //特定のフィールドをその名前で取得する
        //PdfField field = widgets.get("name");
        //フィールドを削除する
        //widgets.remove(field);

        //ドキュメントを保存する
        doc.saveToFile("全フィールドの削除.pdf");
    }
}

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

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

Read 692 times