Monday, November 02, 2020

Create, Read, Fill and Flatten PDF Forms with APEX Office Print 20.3

In the last three years, we've put a lot of effort into the PDF capabilities of APEX Office Print (AOP).

With AOP 20.3 we are releasing an additional sample app focussed just on PDF generation and manipulation. Here's an overview of the main page, highlighting our many features:

In this blog post, I want to cover PDF Forms. PDF Forms have been around for a long time. My guess is that everybody has had to fill in at least one PDF Form already. An example we as a company have to fill in a lot is the W-8BEN form, another example is Form 941 Federal Tax Return. 

Before I go into how you can fill such a PDF Form automatically, let's first look at how you can create a PDF Form yourself with AOP! It looks like COVID increased the requirement to create such forms directly from the database, at least we received many more requests for such a feature lately.

First, you create a template in Word (docx), and anywhere you want a form item (text box, radio group, checkbox) you specify the tag {?form name}.

Next, for every form item you need to specify how it should look in the data you send with the template.
You can specify the item details in AOP by defining the JSON, a SQL, or PL/SQL statement for example. Here's how the definition looks in a SQL Statement and in JSON:

You define your template and data source in the AOP APEX Plug-in (or PL/SQL API).

That's it, AOP will do the rest and create your beautiful PDF Form automagically! :)

Now let's see what AOP can do with PDF Forms we received? E.g. the W-8BEN Form.
Our dream was to read the PDF Form, identify the fields, and fill it automatically and get a filled-in PDF back... and we nailed it! 😀

Use the AOP plug-in to tell which PDF you want to read, and set the global variable g_identify_form_fields to true. AOP generates a new PDF that identifies every field and shows what the name is.

If you want all the fields in a JSON, use the AOP PL/SQL API and specify FORM_FIELDS  as output type:

You get back a nice JSON with all the different form fields, the type, and the value, in case it's filled in.

To fill the items, you call AOP and specify in "aop_pdf_form_data" the data to fill in the PDF Form.

Finally, in case you filled in the PDF Form and now want to make sure other people can't change the fields anymore, you would flatten the PDF. Again, use the AOP PL/SQL API or APEX Plug-in, set the global variable g_output_lock_form to true and you are done!

In the AOP PDF Sample App you will see that the PDF on the left you can still edit, while on the right, you can't:

It's not going to be any easier than this!

In case you need to generate PDF Forms, or you need to fill in forms or you want to process and read those filled-in PDF Forms, AOP can really help. It was a tough nut to crack, but I'm super proud the team was able to pull it together.

Be on the lookout for APEX Office Print 20.3, it's coming to you very soon!


APEX Rocks said...

This looks great. One off topic question. Does AOP supports generating excel with multiple sheets? If yes, would it be possible to share an example on this?

Dimitri Gielis said...

Yes, AOP can generate Multiple Sheets :) you find an example in the AOP Sample Application.

Daniel W. said...

Definitely the right direction!

Just a quick question, the PDF form look rather static than dynamic. Is this true? We had recently received the request to build a PDF completely out of the database.

Where you first define the question(s) including all the options and out of this generate an editable form.

Is this something you would target in one of your next releases? Or will it ever be rather "static".

Would be good to know, if can move our "custom" development to AOP.

Dimitri Gielis said...

Hi Daniel,

I'm not sure what you mean by static vs dynamic.
You can create a complete template in Word and define any PDF Form elements.
AOP also supports conditions, expressions etc. so in my view, you have full control of how your PDF Form looks like.

Feel free to contact our AOP Support if you want to discuss it in person.
Our Cloud is already running AOP 20.3, so you can already try it. On-Premise is available shortly.


Daniel W. said...

Hi Dimitri,

Thank you for your answer.

I was pointing in the direction that it would great to have the possibility to create a placeholder where a form should be placed and there a loop would generate as many form items as defined in the database, as in the example it looks like that you need to define each item at a specific position.

Once the on premise version is available we will have a deeper look and see what is actually possible.


Ævar said...

Hi Dimtri
I don't get it. Why create a pdf form if you can provide a html form directly in APEX? Where is that useful?

Dimitri Gielis said...

Many governments have papers and want PDFs

Ævar said...

Hi Dimitri. Thanks for your answer. I can see the utility of using pdf e.g. to prepare a document/form to be signed electronically. But if you are already connected to the Internet and on a computer...