Java iText Add XMP Metadata Example Tutorial

We have so far discussed how to read, modify,add and view XMP metadata into a PDF document using Java itext library.The latest approach to add Metadata to a PDF document is through the XMP technique. When we saw the XMP XML format in a PDF file, we saw that the XML that was wrapped inside the PDF file as metadata was a really big one and if we are to create metadata following XML fashion, we may have to be very careful in constructing the XML. Fortunately, iText provides a very simple way to add metadata as an XML stream inside a PDF document. This post describes how to add XMP stream to PDF document in iText with a simple example. 

Step-1: Just refer to our Add PDF Metadata example tutorial.We create a Document object, PdfWriter object and add metadata as followed in the tutorial earlier. Refer to the code below;
            Document PDF_XMP = new Document();
            PdfWriter writer=PdfWriter.getInstance(PDF_XMP, new FileOutputStream("Sample.pdf"));
            PDF_XMP.open();
            PDF_XMP.add(new Paragraph("Adding XMP Stream Metadata to PDF using iText Example"));
            PDF_XMP.addTitle("PDF XMP Metadata Example");
            PDF_XMP.addAuthor("Thinktibits");
            PDF_XMP.addSubject("XMP PDF Metadata Creation Example");
            PDF_XMP.addKeywords("Metadata, iText, XMP, PDF");
            PDF_XMP.addCreator("Thinktibits");
            PDF_XMP.addCreationDate();
Step-2: Now, to convert this to a XMP stream, we just add one more line of code to this code, which is provided below.
            /* Convert this metadata as an XMP Stream */
            writer.createXmpMetadata();             
Now, If you recollect what we discussed during Add Metadata example; we found that if you try to read the metadata from a PDF using getMetaData method, on a PDF that does not have XMP Stream embedded, you get a Null Pointer Exception at run time. Now, let us try to do the same thing on this code that converts the standard code to XMP using createXmpMetadata method.

Step-3: After creating the XMP stream into the PDF, I'm going to read it back using the getMetaData method and it works!. Great, iText rocks and it gives a simpler approach to embed metadata as XMP stream in PDF. The code to read the metadata back is provided below;
            /* Try to read the XMP stream which was embedded into the PDF */
            PdfReader ReadInputPDF;
            ReadInputPDF = new PdfReader("sample.pdf");
            byte XMP_MetaData[]=ReadInputPDF.getMetadata();
            /* writing metadata into an xml file */
            FileOutputStream fos = new FileOutputStream("test.xml");
            fos.write(XMP_MetaData);
            fos.close();            
The XML that I got for this example is provided below;
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<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 PDF Metadata Creation Example</rdf:li>
                                </rdf:Alt>
                        </dc:description>
                        <dc:title>
                                <rdf:Alt>
                                        <rdf:li>PDF XMP Metadata Example</rdf:li>
                                </rdf:Alt>
                        </dc:title>
                        <dc:subject>
                                <rdf:Bag>
                                        <rdf:li>XMP PDF Metadata Creation Example</rdf:li>
                                </rdf:Bag>
                        </dc:subject>
                        <dc:creator>
                                <rdf:Seq>
                                        <rdf:li>Thinktibits</rdf:li>
                                </rdf:Seq>
                        </dc:creator>
                </rdf:Description>
                <rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
                        <pdf:Producer>iText 5.0.6 (c) 1T3XT BVBA</pdf:Producer>
                        <pdf:Keywords>Metadata, iText, XMP, PDF</pdf:Keywords>
                </rdf:Description>
                <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
                        <xmp:CreateDate>2011-05-14T18:22:56+10:00</xmp:CreateDate>
                        <xmp:ModifyDate>2011-05-14T18:22:56+10:00</xmp:ModifyDate>
                        <xmp:CreatorTool>Thinktibits</xmp:CreatorTool>
                </rdf:Description>
        </rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
The complete code for this tutorial is provided below. Starting from creating the PDF to adding the XMP Metadata Stream and reading it back..
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class AddXMPMetadata {  
     public static void main(String[] args){
        try {
            Document PDF_XMP = new Document();
            PdfWriter writer=PdfWriter.getInstance(PDF_XMP, new FileOutputStream("Sample.pdf"));
            PDF_XMP.open();
            PDF_XMP.add(new Paragraph("Adding XMP Stream Metadata to PDF using iText Example"));
            PDF_XMP.addTitle("PDF XMP Metadata Example");
            PDF_XMP.addAuthor("Thinktibits");
            PDF_XMP.addSubject("XMP PDF Metadata Creation Example");
            PDF_XMP.addKeywords("Metadata, iText, XMP, PDF");
            PDF_XMP.addCreator("Thinktibits");
            PDF_XMP.addCreationDate();
            /* Convert this metadata as an XMP Stream */
            writer.createXmpMetadata();             
            PDF_XMP.close();
            /* Try to read the XMP stream which was embedded into the PDF */
            PdfReader ReadInputPDF;
            ReadInputPDF = new PdfReader("sample.pdf");
            byte XMP_MetaData[]=ReadInputPDF.getMetadata();
            /* writing metadata into an xml file */
            FileOutputStream fos = new FileOutputStream("test.xml");
            fos.write(XMP_MetaData);
            fos.close();            
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}

No comments:

Post a Comment