Read XMP XML PDF Metadata Tutorial PeopleCode Example

At the end of the previous post, we learned the basics of adding metadata to a PDF file as an XMP stream. We were able to create a PDF file using Java iText and write some metadata information into it in XMP specification using PeopleCode. In this post, we will see how to read the XMP information back from a PDF file and look for specific tags we are interested upon. I have an existing PDF file [ Hello.pdf ] that contains an XMP stream embedded as metadata. I will be showing how to read that information back as a XML inside PeopleSoft and do some processing with it. The step by step guide to read the XMP stream from a PDF is provided below;

Create a PdfReader object that will point to the existing PDF file that contains the XMP stream. This is done using the code below;
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Hello.pdf");
Now, we will be using the getMetadata method of the PdfReader object to read the metadata from PDF file into a byte array in Java. This means, we have to process the byte array inside PeopleCode and write the information to an XML document, which can be used for further processing if required. Follow the procedure below to declare a byte array in PeopleCode and read the metadata information into the byte array;
Local JavaObject &obj_bytearray_l = CreateJavaObject("java.lang.Byte[]");
&obj_bytearray_l = &Obj_read_existing_pdf_l.getMetadata();
Now, we create a FileOutputStream object in PeopleSoft [java.io.FileOutputStream] and use the "write" method to convert this byte array information to a File. We can pass the byte array as an input to the write method and this method will write the contents of the byte array to the file name we will be passing in the constructor of the the FileOutputStream object. The output stream can be closed once this is done. The PeopleCode to convert the Java byte array to file is provided below;
Local JavaObject &obj_fileout_l = CreateJavaObject("java.io.FileOutputStream", "XMP_Metadata.xml");
&obj_fileout_l.write(&obj_bytearray_l);
&obj_fileout_l.close();
The complete PeopleCode to access the XMP XML Metadata stream from a PDF file to a separate XML file, using PeopleCode is provided below;
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Hello.pdf");
Local JavaObject &obj_bytearray_l = CreateJavaObject("java.lang.Byte[]");
&obj_bytearray_l = &Obj_read_existing_pdf_l.getMetadata();
Local JavaObject &obj_fileout_l = CreateJavaObject("java.io.FileOutputStream", "XMP_Metadata.xml");
&obj_fileout_l.write(&obj_bytearray_l);
&obj_fileout_l.close();
This code example extracts the PDF XMP Metadata into an XML, and the output is shown below;
<x:xmpmeta xmlns:x="adobe:ns:meta/">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:format>application/pdf</dc:format>
<dc:description>
<rdf:Alt>
<rdf:li> XMP Metadata Addition to PDF - PeopleSoft Example</rdf:li>
</rdf:Alt>
</dc:description>
<dc:subject>
<rdf:Bag>
<rdf:li> XMP Metadata Addition to PDF - PeopleSoft Example</rdf:li>
</rdf:Bag>
</dc:subject>
<dc:title>
<rdf:Alt>
<rdf:li>Insert XMP Metadata to PDF Example Tutorial</rdf:li>
</rdf:Alt>
</dc:title>
<dc:creator>
<rdf:Seq><rdf:li>PeopleSoft Tutorials</rdf:li></rdf:Seq>
</dc:creator>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
<pdf:Producer>iText 2.1.7 by 1T3XT</pdf:Producer>
<pdf:keywords>PDF XMP Metadata, iText, PeopleSoft, PDF</pdf:keywords>
</rdf:Description>
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
<xmp:CreateDate>2011-05-17T11:23:34+10:00</xmp:CreateDate>
<xmp:ModifyDate>2011-05-17T11:23:34+10:00</xmp:ModifyDate
<xmp:CreatorTool>Test</xmp:CreatorTool></rdf:Description>
</rdf:RDF></x:xmpmeta>                                                                       

No comments:

Post a Comment