In the previous blog,we described how to add metadata into a PDF document through Peoplecode (info dictionary) approach, using the iText Java API. We will extend that post in this tutorial and describe how to read the metadata added into the info dictionary from PeopleCode. I would like to read all the metadata information that is available inside the PDF document iteratively and print them in PeopleSoft application using a Message Box. Normally, you may have to do some additional processing on this metadata depending on your requirement; but for you to get started you need to know how to fetch them in first place. We will run through a step by step procedure shortly that will explain the basics of accessing metadata stored inside a PDF file from PeopleCode.
To view the metadata stored inside a PDF file through Peoplecode, start with creating a PdfReader object that will access the document from where we have to read the metadata. We also create two other Java objects using CreateJavaObject, one of type java.util.HashMap [ we need this to fetch the metadata into a hashmap ] and the other of type java.lang.String [ we need this to iterate through the hashmap in PeopleCode and fetch the key values ]. I also declare a standard Java Object [ java.lang.Object] so that we can use it to retrieve the value from hashmap by passing a key. The Peoplecode statements are provided below;
/* PdfReader to read the PDF and access the metadata */
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Hello.pdf");
/* To get the PDF metadata into a hashmap in Java */
Local JavaObject &Obj_hashmap_l = CreateJavaObject("java.util.HashMap");
/* To get the "key" in hashmap when used with iterator object */
Local JavaObject &obj_string_l = CreateJavaObject("java.lang.String");
/* To get the value of the "key" from hashmap through PeopleCode */
Local JavaObject &objhashmap;
Now,we use the getInfo method of PdfReader object through PeopleCode to pull the metadata information out of the PDF into a hashmap object. For us to read the values from Java Hashmap, I will declare a Iterator object [java.util.Iterator] and use a while loop inside PeopleCode. /* Fetch the Metadata from PDF file */
&Obj_hashmap_l = &Obj_read_existing_pdf_l.getInfo();
/* Iterator Object to loop through the hashmap */
Local JavaObject &Obj_iterator_l = GetJavaClass("java.util.Iterator");
/* Get the iterator object from Hashmap */
&Obj_iterator_l = &Obj_hashmap_l.keySet().iterator();
The while loop we will be using to read the hashmap will keep executing until it finds an entry in the Iterator object. To do this, we use &Obj_iterator_l.hasNext() as the condition for the loop. Inside the loop, we use Iterator.next() to get access a Key Object and get its value. The Key object is then sent to Hashmap.get() to get the value stored. Finally, these values are printed to the output using MessageBox. The code with comments is provided below;Local string &St_mykey;
While (&Obj_iterator_l.hasNext())
/* Get key object as java.lang.String */
&obj_string_l = &Obj_iterator_l.next();
/* Get the value stored in Key Object */
&St_mykey = &obj_string_l.toString();
/* Hashmap.get to read the value of Key, Note we are passing key object */
&objhashmap = &Obj_hashmap_l.get(&St_mykey);
/* Print the Key and Value stored in Hashmap */
MessageBox(0, "", 0, 0, "" | &St_mykey | &objhashmap.toString());
End-While;
You can use this "While loop" PeopleCode example to read any Java Hashmap object from PeopleCode. This method is generic for any hashmap
The values printed for my PDF are shown below;
Subject:- Metadata Addition to PDF - PeopleSoft Example
ModDate:- D:20110516102848+10'00'
Author:- PeopleSoft Tutorials
Keywords:- Metadata, iText, PeopleSoft,
PDFProducer:- iText 2.1.7 by 1T3XT
Title:- Add Metadata to PDF Example Tutorial
Creator:- Test
CreationDate:- D:20110516102848+10'00'
Subject:- Metadata Addition to PDF - PeopleSoft Example
ModDate:- D:20110516102848+10'00'
Author:- PeopleSoft Tutorials
Keywords:- Metadata, iText, PeopleSoft,
PDFProducer:- iText 2.1.7 by 1T3XT
Title:- Add Metadata to PDF Example Tutorial
Creator:- Test
CreationDate:- D:20110516102848+10'00'
The complete PeopleCode example to read metadata from a PDF document is shown below;
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Hello.pdf");
Local JavaObject &Obj_hashmap_l = CreateJavaObject("java.util.HashMap");
Local JavaObject &obj_string_l = CreateJavaObject("java.lang.String");
Local JavaObject &objhashmap;
&Obj_hashmap_l = &Obj_read_existing_pdf_l.getInfo();
Local JavaObject &Obj_iterator_l = GetJavaClass("java.util.Iterator");
&Obj_iterator_l = &Obj_hashmap_l.keySet().iterator();
Local string &St_mykey;
While (&Obj_iterator_l.hasNext())
&obj_string_l = &Obj_iterator_l.next();
&St_mykey = &obj_string_l.toString();
&objhashmap = &Obj_hashmap_l.get(&St_mykey);
MessageBox(0, "", 0, 0, "" | &St_mykey | &objhashmap.toString());
End-While;
hi iam trying to extract the contents from a pdf file and writ1ing the contents to other file
ReplyDeletei used com.itextpdf.text.pdf.parser.pdftextextractor class method getTextFromPage is throwing no overload matchces error
How to overcome this problem, tried to use the reflection concept i was unable to understand how to implement it can any one help me
is there any way that i can extract the specific content from a pdf file