Thursday, February 13, 2014

Reading fillable PDF form using iText APIs


 To follow this example you need to download itext libraries. You can do it from http://sourceforge.net/projects/itext/files/latest/download


Code:

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;

import java.io.IOException;

import java.util.Set;


public class PDFUtil {
    public PDFUtil() {
        super();
    }
   
    public static void main(String[] args) {
        String src = "D:\\Doc\\Filled_PDF_Form.pdf";
        PdfReader reader;
        try {
            reader = new PdfReader(src);
            AcroFields fields= reader.getAcroFields();
           
            Set<String> fieldKeys = fields.getFields().keySet();
            for(String itemKey : fieldKeys){
                System.out.println(itemKey + ": " + fields.getField(itemKey));
            }
            reader.close();
           
        } catch (IOException e) {
            System.out.println("IO Exception");
            e.printStackTrace();
        }

    }
}


This program will show key value pair of each field. For example

key1: value1
key2: value2





Reference: http://www.itextpdf.com/book/examples.php

NOTE: Before using iText libraries I would recommend to read  iText-licensing and pricing information.

Disclaimer: Any views or opinions presented in this blog are solely those of the author and do not necessarily represent those of the company.