Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Using iText to retrieve values in PDF File

 
Ranch Hand
Posts: 224
1
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • 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: 224
1
  • Mark post as helpful
  • send pies
  • 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: 43011
76
  • Mark post as helpful
  • send pies
  • 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: 224
1
  • Mark post as helpful
  • send pies
  • 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: 43011
76
  • Mark post as helpful
  • send pies
  • 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.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!