PHPWord/samples/Sample_33_FormField.php

30 lines
1.0 KiB
PHP
Raw Normal View History

<?php
2022-09-16 11:45:45 +02:00
include_once 'Sample_Header.php';
// New Word document
2015-01-01 20:41:42 +04:00
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
2022-09-16 14:45:25 +02:00
$phpWord->getSettings()->getDocumentProtection()->setEditing('forms');
2016-06-04 20:06:37 +04:00
// New section
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
2016-06-04 20:06:37 +04:00
$textrun->addText('Form fields can be added in a text run and can be in form of textinput ');
$textrun->addFormField('textinput')->setName('MyTextBox');
2016-06-04 20:06:37 +04:00
$textrun->addText(', checkbox ');
$textrun->addFormField('checkbox')->setDefault(true);
2016-06-04 20:06:37 +04:00
$textrun->addText(', or dropdown ');
2022-09-16 11:45:45 +02:00
$textrun->addFormField('dropdown')->setEntries(['Choice 1', 'Choice 2', 'Choice 3']);
2016-06-04 20:06:37 +04:00
$textrun->addText('. You have to set document protection to "forms" to enable dropdown.');
2016-06-04 20:06:37 +04:00
$section->addText('They can also be added as a stand alone paragraph.');
$section->addFormField('textinput')->setValue('Your name');
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}