Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using iText to retrieve values in PDF File

 
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to create a Java application that takes a PDF file as input and extracts the values that are in each field. If the PDF file has fields for the name, address, phone number, and so on, I would like the application to retrieve the values of those fields. I would like to use iText to do this. How can I do this?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
iText would have the ability to 'read' the contents. It cannot identify a particular content as 'name' You will have to build your own logic to do that.
What does your typical pdf content look like?
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:iText would have the ability to 'read' the contents. It cannot identify a particular content as 'name' You will have to build your own logic to do that.
What does your typical pdf content look like?



It is a form that contains a person's name, address, phone number, license plate number, year of vehicle, make of vehicle, and a check box indicating whether the car title was lost or stolen.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This should get you started: http://stackoverflow.com/questions/3093093/using-java-and-itext-how-to-extract-acroform-field-names-from-a-pdf. Searching for "itext acroform" or some such phrase should find more information and code examples.

If you're serious about using iText you should get the iText in Action book (1st or 2nd edition, depending on whether you're using iText 2 or 5). It really is the missing documentation that iText does not have, and will save you a lot of time.
 
Fred Victa
Ranch Hand
Posts: 226
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:This should get you started: http://stackoverflow.com/questions/3093093/using-java-and-itext-how-to-extract-acroform-field-names-from-a-pdf. Searching for "itext acroform" or some such phrase should find more information and code examples.

If you're serious about using iText you should get the iText in Action book (1st or 2nd edition, depending on whether you're using iText 2 or 5). It really is the missing documentation that iText does not have, and will save you a lot of time.



The following code will print out the keys that look like this: F[0].P1[0].FFField1[18]

Moreover, the following code will print out the values that look like this: com.itextpdf.text.pdf.AcroFields$Item@37c7ee

How can I get the code to print out something like this?
Name
John Doe

public static void main(String[] args)
{

PdfReader reader;
PdfReader.unethicalreading = true;
PdfStamper stamper;
AcroFields form;

try{

reader = new PdfReader(pdfFile);
form = reader.getAcroFields();
Map<String,AcroFields.Item> fields = form.getFields();

Set<Map.Entry<String, AcroFields.Item>> entrySet = fields.entrySet();
for (Map.Entry<String, AcroFields.Item> entry : entrySet) {
String key = entry.getKey();
System.out.println(key);
System.out.println(entry.getValue());


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

}



}
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Moreover, the following code will print out the values that look like this: com.itextpdf.text.pdf.AcroFields$Item@37c7ee


Printing out the AcroFields.Item object (which calls its toString() method) isn't likely to produce something useful. Check what methods it has; it's through those that you would interact with it.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic