From past couple of days I'm working on a prototype project in which pdf form fields should be accessed using Domino Java.

There are couple of tasks involved in this project ,

* Designing user interface in pdf and
* Reading fields data using Java

I have struggle a lot to accomplish these tasks so I thought to blog it for my other fellow LN developers.

Designing user interface in pdf


To design user interface in pdf , You may choose any of these following options,

1- Using iText Java API: IText for Java API provides handful of methods to design user interface in pdf.It's like a old Java AWT methods where you need to calculate coordinates to place your objects.It's not very precise and takes lots of time to design simple interface.Don't go with it if you want to design complex form.

2- Adobe LiveCycle Designer: I've tried with trial version of this tool and found it allows you to design user interface and save it.But,You can't edit it in Adobe reader. For that, You must "Distribute" the form after saving.Distribution option is disabled in trial version and this option remain disabled if you are using stand alone version of this tool ( which I got from net).Adobe LiveCycle designer is integrated with Adobe Acrobat Pro.

3- Adobe InDesign: This can be used to design acro pages but you must use Adobe Acrobat Pro to convert designed page into Acrobat form.You can refer following link to design form in InDesign,

InDesign CS3, In Good Form

4- Adobe Acrobat Pro: I'm using this and it fits perfectly to my need. Here is my sample form designed using Adobe Pro where information can be added and saved.,





Reading fields data using Java


To accomplish this,I've used iText for Java API which seems only way to communicate easily with pdf objects.

Since I'm not using iText to design pdf interface so accessing existing pdf using iText is pretty straight forward.


First, Add following two jars in your "Archive" folder of your agent ,



Here is rest of code,

import lotus.domino.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

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

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();

try {
PdfReader reader = new PdfReader("C:\\My Documents\\SampleForm.pdf");
AcroFields form = reader.getAcroFields();
HashMap fields = (HashMap) form.getFields();
String key;

for (Iterator i = fields.keySet().iterator(); i.hasNext(); ) {
key = (String) i.next();
System.out.println(form.getField(key));

}
}
catch (IOException ioe) {
System.err.println(ioe.getMessage());
}

} catch(Exception e) {
e.printStackTrace();
}
}
}

Note: If you know the fields name of your pdf form then you don't need for loop to scan entire form.

Here is the output ,





As this project goes, I will write more about it.

10 Responses to "Accessing pdf form fields using Domino Java"

  1. gravatar NotesSensei Says:

    You also could have a look at Apache pdfbox for accessing all the PDF stuff in Java including the XML that represent the meta-data.

  2. gravatar Rishi Says:

    Thanks Stephan , I will have a look.

  3. gravatar Anonymous Says:

    Just one small point: you have a nested try... catch block. You can lose that :-)

  4. gravatar Rishi Says:

    Thanks Ben. One for NotesSession and other for iText.If I remove any of them they throw an error.

  5. gravatar Lemmy Caution Says:

    Agree strongly on your itext choice.
    I've allways designed my Lotus Agents that way that the agent itself was quite small and only for Domino related stuff.
    I put IText-code, JDBC-code, Business logic, jakarta-commons-webclient code, etc in its own classes. So it is much easier to write junit integration tests for them.
    Even case you don't like junit, a class like this:

    class PdfService {

    public static void main (String[] args ) {

    PdfService pdfService = new PdfService();
    pdfService.printFields("C:/My Documents/SampleForm.pdf");
    }

    void printFields (String filePath) {
    PdfReader reader = new PdfReader(filePath);
    AcroFields form = reader.getAcroFields();
    HashMap fields = (HashMap) form.getFields();
    String key;

    for (Iterator i = fields.keySet().iterator(); i.hasNext(); ) {
    key = (String) i.next();
    System.out.println(form.getField(key));

    }
    }
    catch (IOException ioe) {
    System.err.println(ioe.getMessage());
    }

    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    Can be run directly in Eclipse AND from a notes agent.


    regards

    Axel

  6. gravatar Rishi Says:

    @Axel,

    Useful indeed.However, Notes agent must use NotesMain() as a start up method ?

  7. gravatar Lemmy Caution Says:

    @Rishi: Yes, you can use the PdfService class from the NotesClass.

    NotesMain() {
    PdfService pdfService = new PdfService();
    pdfService.printFields(yourFilePath);
    }

    The idea is just separate the "subsystems" like the one that does PDF-stuff from the notes agent. I've found that such separation allways brought gains in better testability and maintainability.

  8. gravatar Rishi Says:

    @Axel,

    Correct..Putting business logic in separate classes and using them in main class is really neat way to code.I'm following this methodology.

  9. gravatar Suneela Says:

    Hi All,

    I'm new to Lotus notes. I have to pull data from Lotus Notes documents and also some data from Oracle and need to create PDF file by combining the data. Please suggest me some best appraoch to do that...

    Thank you

  10. gravatar Rishi Says:

    Suneela,

    You should look at iText for Java API. It provides plenty of methods to read and write pdf document.

    Rishi

Leave a Reply

preload preload preload