How to automate PDF form-filling in Java
Solution 1
You can easily merge data into PDF's fields using the FDF(Form Data Format) technology.
Adobe provides a library to do that : Acrobat Forms Data Format (FDF) Toolkit
Also Apache PDFBox can be used to do that.
Solution 2
Please take a look at the chapter about interactive forms in the free ebook The Best iText Questions on StackOverflow. It bundles the answers to questions such as:
- How to fill out a pdf file programatically?
- How can I flatten a XFA PDF Form using iTextSharp?
- Checking off pdf checkbox with itextsharp
- How to continue field output on a second page?
- finding out required fields to fill in pdf file
- and so on...
Or you can watch this video where I explain how to use forms for reporting step by step.
See for instance:
public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields fields = stamper.getAcroFields();
fields.setField("name", "CALIFORNIA");
fields.setField("abbr", "CA");
fields.setField("capital", "Sacramento");
fields.setField("city", "Los Angeles");
fields.setField("population", "36,961,664");
fields.setField("surface", "163,707");
fields.setField("timezone1", "PT (UTC-8)");
fields.setField("timezone2", "-");
fields.setField("dst", "YES");
stamper.setFormFlattening(true);
stamper.close();
reader.close();
}
Related videos on Youtube
IAmYourFaja
my father is a principal at burgoyne intnl and got me this job programming lisp and development. I aspire to unittesting with a concentration in mobile platforms.
Updated on April 07, 2022Comments
-
IAmYourFaja about 2 years
I am doing some "pro bono" development for a food pantry near where I live. They are inundated with forms and paperwork, and I would like to develop a system that simply reads data from their MySQL server (which I set up for them on a previous project) and feeds data into PDF versions of all the forms they are required to fill out. This will help them out enormously and save them a lot of time, as well as get rid of a lot of human errors that are made when filling out these forms.
Not knowing anything about the internals of PDF files, I can foresee two avenues here:
- Harder Way: It is possible to scan a paper document, turn it into a PDF, and then have software that "fills out" the PDF simply by saying "add text except blah to the following (x,y) coordinates..."; or
- Easier Way: PDF specification already allows for the construct of "fields" that can be filled out; this way I just write code that says "add text excerpt blah to the field called *address_value*...", etc.
So my first question is: which of the two avenues am I facing? Does PDF have a concept of "fields" or do I need to "fill out" these documents by telling the PDF library the pixel coordinates of where to place data?
Second, I obviously need an open source (and Java) library to do this. iText seems to be a good start but I've heard it can be difficult to work with. Can anyone lend some ideas or general recommendations here? Thanks in advance!
-
IAmYourFaja almost 12 yearsThanks for the suggestion @RealHowTo, but the FDF seems to only process data that is submitted from forms. I'm looking to simply place data into a form so that it can then be printed and sent out via postal mail!
-
RealHowTo almost 12 yearsNo, FDF specify an XML format of the the data to be merged into a PDF document and then a new PDF is created with the data merged, see these PDFBox examples : kickjava.com/src/org/pdfbox/ImportFDF.java.htm and grepcode.com/file_/repo1.maven.org/maven2/org.apache.pdfbox/…
-
Dónal over 8 yearsFYI, a commercial license is required for iText unless the application you're using it in is open source
-
Bruno Lowagie about 8 years@TimKuehn Your comment isn't accurate. You claim "The links on this answer don't work anymore." I tested them all and they all worked but the last one. In the case of the last one, I got a Page Not Found page with a search box. When I entered "reporting" in the search box, the first hit pointed at the desired page.
-
Amedee Van Gasse about 8 yearsThe OP is doing pro bono development for a food pantry (food bank). I think commercial software development is really not the issue here.
-
user2965003 almost 4 yearsThis is from PDF Box. github.com/TomRoush/PdfBox-Android