Shares

How To Fill PDF Form in Android

Shares

This is the third post in my programmatic PDF series. In this post I will show how to fill out a PDF form in Android using the iTextG library. Just like any other form format (paper, html, etc) PDF forms allows you to fill in the blanks of a pre-formatted document. For an Android App this allows you or the users of your app to collect information from your device instead of a PC. A PDF form contains standard input text fields such as:

  1. Textbox field
  2. Checkboxes
  3. Radiobutton
  4. Comboboxes
  5. Listboxes
  6. Push buttons
  7. Signature fields

Steps to Filling a PDF Form in Android

I have divided the steps for filling a PDF form in Android into the following steps:

  1. Create the document template
  2. Convert this template into a fill-able form
  3. Record the ids of fill-able fields
  4. Load this file to Android
  5. Fill the form
  6. Flatten the form
  7. Save, export and share the form.

Now that we have identified the steps to fill PDF form in Android, let us then look at each of the above steps one by one.

Step 1: Create the document template

The title for this step is self explanatory, the first step is to create the template for the form that we want to fill just like you will do in any medium. The Adobe suite of products, word processing software are most ideal for creating this template. You can create the template with code, every PDF library iText included exposes APIs to do that however you will be better  served with a pre-made templates, because laying out a template with code is analogous to laying out an Android view with Java code instead of xml or laying out a html page with JavaScript instead of CSS. Besides, why re-create the wheel either you or your company/client may already have an existing form so you can simply use it.

Step 2: Convert PDF Template into Fill-able Form

Now that you have created your template the next step is to convert that template into a fill-able PDF. If your template was created in say a Word document then you need to convert that Word document to a PDF first and then convert the regular PDF to a fill-able PDF.  There are many online tools that offer to do this for you, but I found it is easiest if you have a access to an installed copy of Adobe Acrobat Pro, then is just a click of a button.

I should clarify however that when I say convert, it may not just be a matter converting the whole page to a fill-able form, for some cases it is actually more appropriate to say “create a fill-able form” instead of saying convert. The fill-able text fields has to be dragged and dropped one by one using if you are using Acrobat, and you have to align them up. If you are creating this yourself, you can take the opportunity to give your form fields meaningful names. Here is example about a hypothetical thank you form to be sent out attendants of a Fictitious event. First here is what the template looks like in Microsoft Word Document:

word_template_for_attendance_thank_you

 

To turn this into a fill-able PDF form, you have to import this Word document into Adobe Acrobat Pro, then for every replaceable text (the one with [] around) it, you have to insert a fill-able text field in its position.  And when you are done, this is how it may look like in Adobe Acrobat Pro showing the field ids of the fill-able text fields.

acrobat_form_fill

However when you view it with regular PDF form viewer the fill-able form field ids are hidden and this is what the form now looks like

fillable_pdf_form

 

This is the form that you upload to your Android app, this is the form that you fill out whether programmatically or on your computer. The fill-able text fields are the variables whose value are supplied later and the text field are the static texts. This is what you will receive if you some else created this form for you and there would be no way for you to know what the field Ids are unless you have a copy of Adobe Acrobat Pro, or you read it programattically.

Step 3: Record the ids of fill-able fields

This is a very important step, you have got to know the ids of the fields that you want to fill. Still with the First Name text field, you have got to know that the field id is “first_name” or something like that. Think of this as the findViewById(R.id.first_name) in Android. How do you know the Id? Well of this is a template that you created then of course you already knew the field name because you created them then there is nothing for you to do with this step. If the template was handed over to you by say a client or another department in your company, then ask them to supply the information so you. Similarly if you hired someone in say Fiverr.com to create the template for you, then ask them to provide you the field name or better still supply them with the field names.

If you have Adobe Acrobat Pro, it is very easy because as soon as you load the fill-able PDF form you will see the field names to the right and you can simply write them down. If none of the above options work for you then you can find out the field names programmatically. The below code from @Mark Storer from Stackoverlow.com will help

Step 4: Load this file to Android

Now that you have written down the names of your text fields, then it is time to load them to Android so we can fill the form. There are many ways that you can do this, but my preferred way is to just add the PDF file to my res/raw folder and you are done. Then you can just load up the file like this. Before you can attempt the code below please follow my post here to add the iTextG jar to your Android Studio project .

Step 5: Fill the form

Filling the form in Android does not mean that there is a PdfView in Android where you will load the fill-able PDF and the user will start typing in, you will still actually need to create a standard Android view that collects the information from the user. Then when the user clicks on save, you collect the entered information and apply normal validation like check to make sure all required fields are entered. The approach I use is to use all the information that the user entered to create a Java object to hold the information. Let’s take the example of Customer, so I will create a Customer object like this

Then when the user click on save I will create an instance of the above object like this

Then I will use the information that was entered into the Android EditText boxes to fill the properties of the new customer object like this

You can then create a method that accepts a Customer object and pass it this customer object and inside that method you fill out your form like this

Step 6: Flatten the form

This is a simple but very important step. Flattening makes the form a read only form, what that does is that it removes the fill-able fields from the form. Without this step the form can still be fill-able again and when you save or print the form you will see the blueish looking background colors of the fill-able areas of the form.  With the code below, you make form back to regular read only nice looking PDF.

 

Step 7: Save, and share the form

Time to share your hard work with the rest of the work. You can export it, email it or print it. Follow the last step in my post to see how to accomplish that.

Summary

There you have, 7 simple steps to on how to fill pdf form in Android. If any of these feels complicated to you, I will be glad to help you out, please use the contact us button above to request a quote from me.

Happy coding

 

About the Author valokafor

I am a Software Engineer with expertise in Android Development. I am available for Android development projects.

follow me on:

Leave a Comment: