業務システムの開発において、PDF文書の処理はよくある課題の一つであり、特に入力可能なPDFフォームの生成は非常に一般的なニーズです。たとえば、従業員情報登録フォーム、ユーザー登録フォーム、アンケート調査、同意確認書などが該当します。
静的なPDFとは異なり、フォームフィールド(Form Field) が含まれたPDFは、ユーザーが文書内で直接入力・選択・チェックなどの操作を行うことができ、使い勝手が大きく向上します。
本記事では、C#を使用してPDFにさまざまなタイプのフォームフィールドを追加する方法について解説します。対象となるフィールドは、テキストボックス、ドロップダウンリスト、チェックボックス、ラジオボタン、リストボックス、ボタンなどで、すべてを組み合わせた実用的なフォームページの作成例も紹介します。
なお、この記事では Free Spire.PDF for .NET を使用します。NuGet で以下のコマンドを実行してインストールしてください:
PM> Install-Package FreeSpire.PDF
PdfTextBoxField を使用してテキスト入力フィールドを追加する
PdfTextBoxField
はテキスト入力用のフィールドです。氏名、住所、日付など自由記述が必要な項目に適しています。
PdfTextBoxField textBox = new PdfTextBoxField(page, "textBox");
textBox.Bounds = new RectangleF(100, 50, 150, 20);
textBox.Text = "Enter your name";
textBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f);
doc.Form.Fields.Add(textBox);
PdfComboBoxField を使用してドロップダウンリストを追加する
PdfComboBoxField
は選択肢の一覧を表示するドロップダウンフィールドです。性別、部署、国籍など、限定された選択肢の入力に適しています。
PdfComboBoxField comboBox = new PdfComboBoxField(page, "comboBox");
comboBox.Bounds = new RectangleF(100, 110, 150, 20);
comboBox.Items.Add(new PdfListFieldItem("Option A", "A"));
comboBox.Items.Add(new PdfListFieldItem("Option B", "B"));
comboBox.Items.Add(new PdfListFieldItem("Option C", "C"));
comboBox.SelectedIndex = 0;
comboBox.Font = new PdfFont(PdfFontFamily.Helvetica, 12f);
doc.Form.Fields.Add(comboBox);
PdfCheckBoxField を使用してチェックボックスを追加する
PdfCheckBoxField
はチェックボックスフィールドです。「同意する」「通知を受け取る」など、はい/いいえの二択を求める場面で使用します。
PdfCheckBoxField checkBox = new PdfCheckBoxField(page, "checkBox");
checkBox.Bounds = new RectangleF(100, 80, 15, 15);
checkBox.Checked = false;
doc.Form.Fields.Add(checkBox);
総合サンプル:全タイプのフォームフィールドを含むPDFフォームの作成
以下のコードでは、「ユーザー情報登録フォーム」を作成しています。テキストボックス、チェックボックス、ドロップダウン、リストボックス、ラジオボタン、ボタンなど、一般的なすべてのフォーム要素を網羅しています。
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;
class Program
{
static void Main(string[] args)
{
// ドキュメントとページを作成
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
// 座標とスタイルの初期化
float baseX = 100;
float baseY = 30;
PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.Blue));
PdfSolidBrush labelBrush = new PdfSolidBrush(new PdfRGBColor(Color.Black));
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
// テキストボックス
page.Canvas.DrawString("TextBox:", font, titleBrush, new PointF(10, baseY));
RectangleF textBoxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfTextBoxField textBox = new PdfTextBoxField(page, "textbox");
textBox.Bounds = textBoxBounds;
textBox.Text = "Hello World";
textBox.Font = font;
doc.Form.Fields.Add(textBox);
baseY += 25;
// チェックボックス
page.Canvas.DrawString("CheckBox:", font, titleBrush, new PointF(10, baseY));
RectangleF checkBox1Bounds = new RectangleF(baseX, baseY, 15, 15);
PdfCheckBoxField checkBox1 = new PdfCheckBoxField(page, "checkbox1");
checkBox1.Bounds = checkBox1Bounds;
checkBox1.Checked = false;
page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));
RectangleF checkBox2Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
PdfCheckBoxField checkBox2 = new PdfCheckBoxField(page, "checkbox2");
checkBox2.Bounds = checkBox2Bounds;
checkBox2.Checked = false;
page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));
doc.Form.Fields.Add(checkBox1);
doc.Form.Fields.Add(checkBox2);
baseY += 25;
// コンボボックス(ドロップダウンリスト)
page.Canvas.DrawString("ComboBox:", font, titleBrush, new PointF(10, baseY));
RectangleF comboBoxBounds = new RectangleF(baseX, baseY, 150, 15);
PdfComboBoxField comboBox = new PdfComboBoxField(page, "combobox");
comboBox.Bounds = comboBoxBounds;
comboBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
comboBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
comboBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
comboBox.SelectedIndex = 0;
comboBox.Font = font;
doc.Form.Fields.Add(comboBox);
baseY += 25;
// リストボックス
page.Canvas.DrawString("ListBox:", font, titleBrush, new PointF(10, baseY));
RectangleF listBoxBounds = new RectangleF(baseX, baseY, 150, 50);
PdfListBoxField listBox = new PdfListBoxField(page, "listbox");
listBox.Bounds = listBoxBounds;
listBox.Items.Add(new PdfListFieldItem("Item 1", "item1"));
listBox.Items.Add(new PdfListFieldItem("Item 2", "item2"));
listBox.Items.Add(new PdfListFieldItem("Item 3", "item3"));
listBox.SelectedIndex = 0;
listBox.Font = font;
doc.Form.Fields.Add(listBox);
baseY += 60;
// ラジオボタン
page.Canvas.DrawString("RadioButton:", font, titleBrush, new PointF(10, baseY));
PdfRadioButtonListField radioGroup = new PdfRadioButtonListField(page, "radioGroup");
PdfRadioButtonListItem radio1 = new PdfRadioButtonListItem("Option1");
radio1.Bounds = new RectangleF(baseX, baseY, 15, 15);
page.Canvas.DrawString("Option 1", font, labelBrush, new PointF(baseX + 20, baseY));
PdfRadioButtonListItem radio2 = new PdfRadioButtonListItem("Option2");
radio2.Bounds = new RectangleF(baseX + 70, baseY, 15, 15);
page.Canvas.DrawString("Option 2", font, labelBrush, new PointF(baseX + 90, baseY));
radioGroup.Items.Add(radio1);
radioGroup.Items.Add(radio2);
radioGroup.SelectedIndex = 0;
doc.Form.Fields.Add(radioGroup);
baseY += 25;
// 署名フィールド
page.Canvas.DrawString("Signature Field:", font, titleBrush, new PointF(10, baseY));
RectangleF signatureBounds = new RectangleF(baseX, baseY, 150, 80);
PdfSignatureField signatureField = new PdfSignatureField(page, "signatureField");
signatureField.Bounds = signatureBounds;
doc.Form.Fields.Add(signatureField);
baseY += 90;
// ボタン
page.Canvas.DrawString("Button:", font, titleBrush, new PointF(10, baseY));
RectangleF buttonBounds = new RectangleF(baseX, baseY, 50, 15);
PdfButtonField button = new PdfButtonField(page, "submitButton");
button.Bounds = buttonBounds;
button.Text = "Submit";
button.Font = font;
PdfSubmitAction submitAction = new PdfSubmitAction("https://www.google.com/");
submitAction.DataFormat = SubmitDataFormat.Html;
button.Actions.MouseDown = submitAction;
doc.Form.Fields.Add(button);
// ドキュメントを保存
doc.SaveToFile("FillableForm.pdf", FileFormat.PDF);
doc.Close();
}
}
フォームフィールドの種類一覧
フィールドの種類 | 説明 |
---|---|
PdfTextBoxField |
テキストを自由に入力できるフィールド |
PdfCheckBoxField |
チェックボックス、2択選択に最適 |
PdfComboBoxField |
ドロップダウンメニュー、選択肢を提供 |
PdfListBoxField |
リスト形式の選択肢、複数選択も可能 |
PdfRadioButtonListField |
ラジオボタン、一つだけ選択可能 |
PdfButtonField |
ボタン、クリック時に指定アクションを実行 |
以上のようにして、開発者は機能豊富で構造の整ったPDFフォームを迅速に構築することが可能です。これにより、ユーザー情報の収集や文書の自動化処理など、さまざまな業務シーンに対応できます。
📖 詳しくは Spire.PDF 公式ドキュメントをご覧ください。