-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Added the possibility to read FormFields from Word2007 documents #2282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added the possibility to read FormFields from Word2007 documents #2282
Conversation
… the develop branch won't stop the pull request.
2d9f999
to
e458249
Compare
I've tested this and can confirm it works, hoping this will get merged so I don't have to use a fork 🙂 For anyone who wants to get all the form fields in a document, I've came up with this recursive solution: /**
* @param PhpWord $phpWord
* @return FormField[]
*/
function getFormFields(PhpWord $phpWord)
{
$fields = [];
foreach($phpWord->getSections() as $section) {
$fields = array_merge($fields, getFormFieldsRecursive($section));
}
return $fields;
}
/**
* @param AbstractElement $element
* @return FormField[]
*/
function getFormFieldsRecursive(AbstractElement $element)
{
if ($element instanceof AbstractContainer) {
$fields = [];
foreach($element->getElements() as $nestedElement) {
$fields = array_merge($fields, getFormFieldsRecursive($nestedElement));
}
return $fields;
}
if ($element instanceof Table) {
$fields = [];
foreach ($element->getRows() as $row) {
foreach ($row->getCells() as $cell) {
$fields = array_merge($fields, getFormFieldsRecursive($cell));
}
}
return $fields;
}
if ($element instanceof FormField) {
return [
$element->getName() => $element,
];
}
return [];
}
$phpWord = IOFactory::createReader()->load('/path/to/file');
$fields = getFormFields($phpWord);
$fields['first_name']->setValue('Kuba');
$fields['date']->setValue(Carbon::now()->format('Y-m-d'));
IOFactory::createWriter($phpWord)->save('/path/to/generated-file'); |
Hi I was looking for something to ger the form fields from word, but the above @KKSzymanowski does not seem to work Attached word have a textbox, combo and a checkbox, but none is detected What did I miss ? |
After this comment I was messing around with these text fields and decided I'm gonna use PHPWord in a very limited scope. The issue was after I saved the file with fields filled out the formatting was all out of whack. I only use the XMLReader from PHPWord to open the Word file easier than manually. https://gist.github.com/KKSzymanowski/acf892616805aed9900289b9bb1ede6d Keep in mind I only used that for text form fields, may need extending to work for checkboxes or comboboxes. I use it like this: $fileName = (new FormFieldHelper($templatePath))
->setFieldValues($variables)
->save(storage_path($path));
To get the field names without setting the values I use: (new FormFieldHelper($templatePath))->getFieldNames(); |
Superseeded by #2653 |
Description
PHPWord had the option to create and write FormFields (checkbox, textinput and dropdown). This pull requests add the functionality to read those fields into the PHPWord model
Bugfixes
This Pull request fixes Bug #2281
Checklist:
composer run-script check --timeout=0
and no errors were reported