跳轉到

AcroForm 互動式表單

NextPDF 完整實作 ISO 32000-2 §12.7 定義的 AcroForm 規範,提供 FormFieldManager 統一管理所有互動式表單欄位。生成的表單與 Adobe Acrobat、Foxit、瀏覽器內建 PDF 檢視器完全相容。

文字欄位

use NextPDF\Form\FormFieldManager;
use NextPDF\Form\TextField;
use NextPDF\ValueObjects\Rectangle;

$form = $document->form();

// 單行文字欄位
$form->addTextField(
    config: TextField::create(
        name: 'firstName',
        label: 'First Name',
        position: Rectangle::fromXY(x: 60.0, y: 200.0, width: 80.0, height: 8.0),
        pageNumber: 1,
        required: true,
        maxLength: 100,
        defaultValue: '',
    ),
);

// 多行文字欄位
$form->addTextField(
    config: TextField::create(
        name: 'comments',
        label: 'Comments',
        position: Rectangle::fromXY(x: 20.0, y: 150.0, width: 140.0, height: 30.0),
        pageNumber: 1,
        multiline: true,
        scrollable: true,
    ),
);

核取方塊

use NextPDF\Form\CheckboxField;

$form->addCheckbox(
    config: CheckboxField::create(
        name: 'agreeToTerms',
        label: 'I agree to the Terms of Service',
        position: Rectangle::fromXY(x: 20.0, y: 120.0, width: 8.0, height: 8.0),
        pageNumber: 1,
        checked: false,
        exportValue: 'Yes',
    ),
);

單選按鈕群組

use NextPDF\Form\RadioGroup;
use NextPDF\Form\RadioButton;

$radioGroup = RadioGroup::create(name: 'paymentMethod');

$form->addRadioGroup(
    group: $radioGroup,
    buttons: [
        RadioButton::create(
            label: 'Credit Card',
            value: 'credit_card',
            position: Rectangle::fromXY(x: 20.0, y: 100.0, width: 8.0, height: 8.0),
            pageNumber: 1,
        ),
        RadioButton::create(
            label: 'Bank Transfer',
            value: 'bank_transfer',
            position: Rectangle::fromXY(x: 20.0, y: 90.0, width: 8.0, height: 8.0),
            pageNumber: 1,
        ),
    ],
    defaultValue: 'credit_card',
);

下拉選單

use NextPDF\Form\ComboBoxField;
use NextPDF\Form\SelectOption;

$form->addComboBox(
    config: ComboBoxField::create(
        name: 'country',
        label: 'Country',
        position: Rectangle::fromXY(x: 60.0, y: 80.0, width: 80.0, height: 8.0),
        pageNumber: 1,
        options: [
            SelectOption::create(value: 'TW', label: 'Taiwan'),
            SelectOption::create(value: 'US', label: 'United States'),
            SelectOption::create(value: 'JP', label: 'Japan'),
        ],
        defaultValue: 'TW',
        editable: false,  // 允許用戶輸入自訂值
    ),
);

按鈕

use NextPDF\Form\PushButton;
use NextPDF\Form\ButtonAction;

// 提交按鈕
$form->addButton(
    config: PushButton::create(
        name: 'submitBtn',
        label: 'Submit Form',
        position: Rectangle::fromXY(x: 60.0, y: 20.0, width: 40.0, height: 10.0),
        pageNumber: 1,
        action: ButtonAction::submitForm(
            url: 'https://api.example.com/submit',
            format: SubmitFormat::FDF,  // FDF | HTML | XFDF | PDF
        ),
    ),
);

// 重置按鈕
$form->addButton(
    config: PushButton::create(
        name: 'resetBtn',
        label: 'Clear',
        position: Rectangle::fromXY(x: 110.0, y: 20.0, width: 30.0, height: 10.0),
        pageNumber: 1,
        action: ButtonAction::resetForm(),
    ),
);

數位簽章欄位

use NextPDF\Form\SignatureField;

// 預留數位簽章欄位(由 PAdES 模組填入)
$form->addSignatureField(
    config: SignatureField::create(
        name: 'authorSignature',
        label: 'Author Signature',
        position: Rectangle::fromXY(x: 20.0, y: 30.0, width: 100.0, height: 25.0),
        pageNumber: 1,
    ),
);

表單資料匯出

// 匯出當前表單資料為 FDF
$fdfBytes = $document->form()->exportFdf();

// 填入外部 FDF 資料
$document->form()->importFdf($fdfBytes);

參見