tags:

views:

125

answers:

2

Hi,

I've a requirement where user are going to fill lots of fields ( text field, check box, radio button ) on pdf form and they will mail us. I need to read each fields on pdf form and insert into oracle table.

Edit1: I'm trying following code, It generates pdf but when I double click it says "invalid format". What's wrong ?

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;

public class pdfGentest{
    public static void main(String[] args) {

        Document document = new Document();

        try {

            PdfWriter.getInstance(document, new FileOutputStream("c:\\HelloWorld.pdf"));
            document.open();
            document.add(new Paragraph("Hello World"));
            }
        catch (DocumentException de) {
            System.err.println(de.getMessage());
            } catch (IOException ioe) {
                System.err.println(ioe.getMessage());
            }       
    }
}

Fixed: Due to I've not closed the document..Adding document.close(); fixed the problem

+1  A: 

You can use IText library to do that. Link =>http://itextpdf.com/

Sadly I don't have java code example for that as I am using iTextSharp library for C#.NET buts pretty straightforward.

You might want to check itextpdf.com/book/examples.php for examples. Also check the following link for some example on reading field values, http://itext-general.2136553.n4.nabble.com/Problem-Reading-Interactive-Form-Values-Acro-Fields-from-PDF-using-iText-td2171900.html

MSI
Thanks MSI, I don't see any documentation for java. They have listed only jars. Do you know if they provide documentation ?
Rishi
Thanks MSI..Looks great..
Rishi
Hey MSI, I've a question..Let's say client is creating text boxes , check box and other fields on pdf and sending us. How do we know the field name ? I assume while coding we need to know field name to get handle of those fields ?
Rishi
You need to create form fields on pdf form first using Acrobat Professional or other softwares. Unless there are named fields available for the fields you want to work on I don't think its possible to read/write to pdf fields. So far you have the named fields, you can use those names to refer to the respective field items and grab their values/write to them.
MSI
Thanks MSI. Can we see a field name on pdf forms ? I got a sample form which has field "Account Number" ..like,Account Number: <Text box>How do I know the textbox id/name so that I can refer in my code or iText provides some methods to find field names on the form.Thanks for your insights .
Rishi
Got it..It's beautifully mentioned in the "Manning iText in Action" book.
Rishi
ah cool was going to point you to the following link (added in the answer) for some example but if you got the book already then fine:)
MSI
Anybody having sample code ?
Rishi
A: 

You can use PDF Box api, which will support to extract the fields information more clearly.

Naimur