Click here to Skip to main content
15,867,686 members
Articles / All Topics

Fill PDF Form in Android

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
9 May 2015CPOL7 min read 20.2K   1   2
Fill PDF Form in Android

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 allow 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 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 an 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 access to an installed copy of Adobe Acrobat Pro, then it 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 values are supplied later and the text fields are the static texts. This is what you will receive if someone 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 if 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:

C#
// you only need a PdfStamper if you're going to change the existing PDF.
PdfReader reader = new PdfReader( pdfPath );

AcroFields fields = reader.getAcroFields();

Set<String> fldNames = fields.getFields().keySet();

for (String fldName : fldNames) {
  System.out.println( fldName + ": " + fields.getField( fldName ) );
}

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 in which 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 load add the iTextG jar to your Android Studio project.

C#
PdfReader reader = new PdfReader(getResources().openRawResource(R.raw.template_name));

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:

C#
public class Customer{
    private String mName;
    private String mEmailAddress;
    private String mPhoneNumber;
    private String mStreetAddress;
    private String mCity;
    private String mState;
    private String mPostalCode;
}

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

C#
Customer mCustomer = new Customer();

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:

C#
mCustomer.setName(mNameEditText.getText().toString());
mCustomer.setEmailAddress(mEmailEditText.getText().toString());
mCustomer.setPhoneNumber(mPhoneEditText.getText().toString());
mCustomer.setStreetAddress(mStreetEditText.getText().toString());
mCustomer.setCity(mCityEditText.getText().toString());
mCustomer.setState(mStateEditText.getText().toString());
mCustomer.setPostalCode(mZipCodeEditText.getText().toString());

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:

C#
OutputStream output = null;
PdfReader reader = new PdfReader(getResources().openRawResource(R.raw.template3));
PdfStamper stamper = new PdfStamper(reader, output);
AcroFields acroFields = stamper.getAcroFields();

acroFields.setField("name", customer.getName());
form.setField("address", customer.getStreetAddress());
form.setField("city", customer.getCity());

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.

C#
stamper.setFormFlattening(true);
stamper.close();

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 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!

The post Fill PDF Form in Android appeared first on Val Okafor.

This article was originally posted at http://valokafor.com/fill-pdf-form-in-android

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) ValOkafor.com
United States United States
My name is Val Okafor, I am a Senior Software Engineer with specialization in Android Development. Learning and problem solving is my passion and I share my Android development knowledge through my blog ValOkafor.com.

My Android courses are the courses I wish I had when I started. I teach Android development in the context of a fully developed Android app. I believe that new Android concepts are better understood if they are presented in the context of creating an app from scratch to finish.

I focus on creating Productivity Android apps and besides Android development I have 7 years’ experience as System Administrator supporting Enterprise applications and 2 years’ experience as a Web Developer building websites using PHP and ASP.Net.

I have worked for corporations such as The Home Depot, American Council on Exercise, Legend3D and HD Supply, Inc. I have a bachelor's degree in Information Technology from National University San Diego, California and a master's degree in Software Engineering from Regis University Denver, Colorado.

I enjoy sharing my extensive work experience through my blog, social media.

Comments and Discussions

 
QuestionHow can I save that form in pdf Pin
Member 1242367629-Mar-16 5:05
Member 1242367629-Mar-16 5:05 
QuestionHow to import iTextG ? Pin
Hi I am Kevin20-Oct-15 4:30
Hi I am Kevin20-Oct-15 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.