Skip to main content

Java Programming – Automatic form filling in PDF using JAVA codes

Java Development to Control Crucial Business Aspects

In the world of automation, who would like to take the pain of manually filling out redundant form fields? Especially when forms control critical aspects of your business and time challenged professional may find it real hard to cope with it? Besides, things can really get tedious when these forms are to be managed in PDF formats!!

Wouldn’t it be nice if we had a provision to dynamically fill PDF documents by pulling information right from the database? …that would certainly save a lot of manual efforts wouldn’t it?? That’s exactly one of our clients wanted and here’s how I used NITRO PDF and JAVA Code to automate much of the heavy lifting.

Here’s how it works

So let’s get started. Let me give a brief roundup on NITRO Reader. You might have used it to convert your word files to PDF. But there’s more you can do with the trial version to start with. You can actually create form fields in PDF documents. But that’s one part. The real value of this post comes when you combine the JAVA snippets that I have given below to automate the hard way out.

Here is the screenshot which have form fields that have been created in PDF document using NITRO

Form Fields Using NITRO
Creating Form Fields in PDF using NITRO

Java Programming Part

Now let’s get started with the coding part. The JAVA code mentioned below interacts with the database and can automatically pull out relevant information and fill the above form fields.

I have provided the code with comments using some dummy values to make it easily understandable even for a novice java programmer. Here’s how it works.

private ByteArrayOutputStream editPdfDocument() throws Exception {

        PdfReader reader = null;

        PdfStamper stamper = null;

        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();

        HashMap fieldsWithValues = new HashMap();

        int user_id = 1234; // unique key for the object

        String dirPath = “D: /abc”;/ / directory path

        String fileName = “def.pdf”; // name of the file

        try {

            reader = new PdfReader(dirPath + “/” + fileName);

                stamper = new PdfStamper(reader, baosPDF);

                AcroFields form = stamper.getAcroFields();

                HashMap fields = form.getFields();

                Set keys = fields.keySet(); // keys represents the names of all the form fields

                fieldsWithValues = fetchFieldValuesForObject(user_id, keys);
                // fetchFieldValuesForObject(user_id, keys) method will fetch the values of the fields from Database for object identified by user_id
                Iterator itr = keys.iterator();

                while (itr.hasNext()) {

                    String fieldName = (String);

                    String fieldValue = fieldsWithValues.get(fieldName) != null ? (String)(fieldsWithValues.get(fieldName)) : “”;

                    form.setField(fieldName, fieldValue);



            } catch (Exception dex) {

                if (stamper != null)


                if (reader != null)


                throw dex;


            return baosPDF;


The Output – Here you go !!

Java PDF automation
Automated Filled Form