Cookbook

Generate DOCX with fillable forms

The addStructuredDocumentTag method, available in the phpdocx API, allows to generate DOCX with forms to be filled by the users.

In case of need of additional protection, phpdocx lets also protect these DOCX, so the user can only modify the form fields. The rest of the document would remain locked.

Generating the form

The addStructuredDocumentTag method allows to create five types of fields for the forms: checkboxes, combo boxes, dates, dropdown lists and rich texts.

As an example, let's see how to generate a small survey which the user has to fill with her or his name, email, age and an option with a checkbox: whether she or him has a car. The code is as follows:

Reading the information filled in the forms (Advanced and Premium licenses only)

Let's say that you need to parse all the users' information that the document form has gathered. You can do that easily with the DOCXPath class.

The following code returns an array with the values of the form, in the same order in which the fields are placed in the document.

Protecting the document to allow changes only in the document (Premium license only)

Documents created through forms can be modified freely. That is, a user could fill the form just as easily as modifying other document texts.

If you want to protect the document in order to allow only changes in the form while locking the rest of the text, you just have to call this code: