Skip to content

フォームフィールド

フォームモジュール(FormFieldManagerFormField)は、PDF内にインタラクティブなAcroFormフィールドを作成します。フィールドタイプは FormFieldType 列挙型で定義されています:TEXTPASSWORDTEXTAREACHECKBOXRADIOLISTBOXCOMBOBOXBUTTON。すべてのメソッドは static を返すため、メソッドチェーンが可能です。

クイックリファレンス

メソッドフィールドタイプ
textField()単一行テキスト入力
checkboxField()チェックボックストグル
radioField()ラジオボタン(グループ化)
listboxField()スクロール可能なリスト
comboboxField()ドロップダウンセレクタ
buttonField()プッシュボタン(オプションのJavaScriptアクション付き)
flattenFields()すべてのインタラクティブフィールドを静的コンテンツに変換

基本的な使用例

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Registration Form', newLine: true)
    ->ln(5)

    // テキストフィールド
    ->cell(30, 8, 'Name:')
    ->textField('name', 45, null, 80, 8, [
        'border'    => ['width' => 1],
        'maxLength' => 100,
    ])
    ->ln(12)

    // メールフィールド
    ->cell(30, 8, 'Email:')
    ->textField('email', 45, null, 80, 8)
    ->ln(12)

    // チェックボックス
    ->checkboxField('agree', 15, null, 5, 5)
    ->cell(0, 5, '  I agree to the terms', newLine: true)
    ->ln(10)

    // ドロップダウン
    ->cell(30, 8, 'Country:')
    ->comboboxField('country', 45, null, 60, 8, ['US', 'UK', 'TW', 'JP', 'DE'])
    ->ln(12)

    // 送信ボタン
    ->buttonField('submit', 45, null, 40, 10, 'Submit', 'submitForm("https://example.com/submit")');

テキストフィールドとチェックボックスフィールド

php
$pdf->textField(string $name, float $x, float $y, float $w, float $h, array $prop = []);
$pdf->checkboxField(string $name, float $x, float $y, float $w, float $h, bool $checked = false);

$ynull を渡すと、現在の垂直カーソル位置が使用されます。

ラジオボタンフィールド

php
$pdf->radioField(string $name, float $x, float $y, float $w, float $h, array $prop = []);

同じ $name を持つラジオボタンは、相互に排他的なグループを形成します:

php
$pdf->cell(30, 8, 'Gender:')
    ->radioField('gender', 45, null, 5, 5, ['value' => 'male'])
    ->cell(10, 5, ' M')
    ->radioField('gender', 65, null, 5, 5, ['value' => 'female'])
    ->cell(10, 5, ' F');

リスト、コンボボックス、ボタンフィールド

php
$pdf->listboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->comboboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->buttonField(string $name, float $x, float $y, float $w, float $h, string $caption, string $action = '');

listboxField() はスクロール可能な複数行リストを描画します。comboboxField() は単一行のドロップダウンを描画します。buttonField() はオプションのJavaScriptアクション文字列付きのプッシュボタンを作成します。

php
$pdf->buttonField('reset', 15, null, 40, 10, 'Reset', 'this.resetForm()')
    ->buttonField('print', 60, null, 40, 10, 'Print', 'this.print()');

フィールドプロパティ

$prop 配列はフィールドの外観と動作を制御します:

キー説明
borderarraywidthcolorstyle キーを持つ枠線スタイル
bgcolorarray背景色を [r, g, b] で指定
fontstringフォントファミリー名
fontSizefloatフォントサイズ(ポイント)
alignmentstringテキスト配置:leftcenterright
maxLengthint最大文字数(テキストフィールド)
readonlyboolユーザー編集を防止
requiredboolフォームバリデーションで必須としてマーク
valuestringデフォルト/初期値

フォームのフラット化

フラット化は、すべてのインタラクティブフィールドを静的で編集不可能なコンテンツに変換します。これは、完了したフォームのアーカイブや、最終的な読み取り専用PDFの作成に便利です。

php
$pdf->flattenFields();  // すべてのフォームフィールドを静的コンテンツに変換

フラット化後、フィールドの値は永続的なテキストになります。PDFビューアでフィールドを編集することはできなくなります。

LGPL-3.0-or-later ライセンスの下で公開されています。