Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

vincentKool
Copy link

@vincentKool vincentKool commented Aug 20, 2022

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:

  • I have run composer run-script check --timeout=0 and no errors were reported
  • The new code is covered by unit tests (check build/coverage for coverage report)
  • I have updated the documentation to describe the changes

@PowerKiKi PowerKiKi changed the base branch from develop to master November 16, 2022 21:08
@Progi1984 Progi1984 force-pushed the master branch 3 times, most recently from 2d9f999 to e458249 Compare August 30, 2023 11:56
@Progi1984 Progi1984 added this to the 2.0.0 milestone Nov 30, 2023
@KKSzymanowski
Copy link

KKSzymanowski commented Dec 10, 2023

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');

@rabol
Copy link

rabol commented Jan 29, 2024

Hi

I was looking for something to ger the form fields from word, but the above @KKSzymanowski does not seem to work
doc_with_form.docx

Attached word have a textbox, combo and a checkbox, but none is detected

What did I miss ?

@KKSzymanowski
Copy link

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.
Here's the complete class used to read all available form fields and then fill them out with values. Not the cleanest thing I wrote but I'm glad it worked at all because I was under time pressure to deliver this.

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));

$variables is an array where keys are the form field names and values are the actual values we want assigned to them.

To get the field names without setting the values I use:

(new FormFieldHelper($templatePath))->getFieldNames();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants