跳轉到

表單指南

AcroForm(Core)

文字欄位

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

$forms = $document->forms();

$forms->addTextField(
    field: TextField::create(
        name:        'customer_name',
        label:       'Customer Name',
        placeholder: 'Enter full name',
        required:    true,
        maxLength:   100,
    ),
    bounds: Rectangle::create(x: 25.0, y: 80.0, width: 120.0, height: 15.0),
);

核取方塊

use NextPDF\Core\Form\Fields\CheckboxField;

$forms->addCheckbox(
    field: CheckboxField::create(
        name:    'agree_terms',
        label:   'I agree to the terms',
        checked: false,
    ),
    bounds: Rectangle::create(x: 25.0, y: 110.0, width: 6.0, height: 6.0),
);

單選按鈕

use NextPDF\Core\Form\Fields\RadioGroup;
use NextPDF\Core\Form\Fields\RadioOption;

$forms->addRadioGroup(
    group: RadioGroup::create(
        name:    'payment_method',
        options: [
            RadioOption::create(value: 'credit_card', label: 'Credit Card'),
            RadioOption::create(value: 'bank_transfer', label: 'Bank Transfer'),
            RadioOption::create(value: 'cash', label: 'Cash'),
        ],
        selected: 'credit_card',
    ),
    positions: [
        Rectangle::create(x: 25.0, y: 140.0, width: 6.0, height: 6.0),
        Rectangle::create(x: 25.0, y: 155.0, width: 6.0, height: 6.0),
        Rectangle::create(x: 25.0, y: 170.0, width: 6.0, height: 6.0),
    ],
);

下拉選單

use NextPDF\Core\Form\Fields\ComboBoxField;

$forms->addComboBox(
    field: ComboBoxField::create(
        name:     'country',
        label:    'Country',
        options:  ['TW' => 'Taiwan', 'JP' => 'Japan', 'US' => 'United States'],
        selected: 'TW',
        editable: false,
    ),
    bounds: Rectangle::create(x: 25.0, y: 200.0, width: 80.0, height: 12.0),
);

數位簽章欄位

use NextPDF\Core\Form\Fields\SignatureField;

$forms->addSignatureField(
    field: SignatureField::create(
        name:  'customer_signature',
        label: 'Customer Signature',
    ),
    bounds: Rectangle::create(x: 25.0, y: 250.0, width: 80.0, height: 30.0),
);

表單欄位樣式

use NextPDF\Core\Form\ValueObjects\FieldAppearance;

$appearance = FieldAppearance::create()
    ->withBackgroundColor('#F9FAFB')
    ->withBorderColor('#D1D5DB')
    ->withBorderWidth(0.5)
    ->withFontName('Helvetica')
    ->withFontSize(10.0)
    ->withTextColor('#111827');

$forms->addTextField(
    field:      TextField::create(name: 'email')->withAppearance($appearance),
    bounds:     Rectangle::create(x: 25.0, y: 80.0, width: 120.0, height: 15.0),
);

表單壓平(Flatten)

use NextPDF\Core\Form\FormFieldManager;

$flatResult = $forms->flatten(
    values: [
        'customer_name'    => 'Wang Xiaoming',
        'agree_terms'      => true,
        'payment_method'   => 'credit_card',
        'country'          => 'TW',
    ],
);

$flatResult->document()->save('/output/filled.pdf');

Pro:XFA 動態表單

Pro:XFDF 資料匯入匯出

use NextPDF\Pro\Form\XfdfImporter;

$importer = XfdfImporter::create();
$filled   = $importer->importInto(
    document: $document,
    xfdf:     file_get_contents('/data/form-data.xfdf'),
);

表單驗證

延伸閱讀