Java iText View Metadata GetInfo Method Example Tutorial

In the last two posts, we were discussing how to view metadata that was added using XMP technique to a PDF document in Java iText. We also discussed how to add basic metadata information on your PDF document in iText. However, we got a null pointer exception when we tried using the method getMetaData method. I did some more research on this and found that, if you want to retrieve the metadata information on a PDF document that did not use XMP to add metadata, you have to use a different method of PdfReader object. This method is "getinfo" and this blog post explains how to access metadata on your blog using "getinfo" method.
Use getMetadata to access PDF information if the PDF used XMP, else use getinfo method
In this blog post, we will provide a working example that will explain how to read the structural information out of a PDF file using the "getinfo" method. We will get started with a step by step procedure as documented below; [Note: The PDF we are using as an input is generated out of the tutorial that explained how to add metadata to PDF ]

Step-1: Use PdfReader to read the incoming PDF document as shown below;
          PdfReader ReadInputPDF;
          ReadInputPDF = new PdfReader("sample.pdf");
Step-2:Define a hashmap in Java and assign the output of getinfo method to this hashmap. The code to do this is given below;
          HashMap hMap = new HashMap();
          hMap=ReadInputPDF.getInfo();
Step-3: With the hashmap now on hand, we define an Iterator object and then print the key and  value as a SOP in Java. The code to iterate a hashmap and print the values in Java is provided below;
          Iterator iterator = hMap.keySet().iterator();
          while (iterator.hasNext()) {
                  String key = (String) iterator.next();
                  System.out.println(key + ":" + hMap.get(key));
          }
That completes the explanation of the code snippet section, that explains how to read metadata using getinfo method from a hashmap in Java. The complete code with import declarations is given below;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Set;
public class ReadPDFCatalog{  
     public static void main(String[] args){
        try {
          PdfReader ReadInputPDF;
          ReadInputPDF = new PdfReader("sample.pdf");
          HashMap hMap = new HashMap();
          hMap=ReadInputPDF.getInfo();
          Iterator iterator = hMap.keySet().iterator();
          while (iterator.hasNext()) {
                  String key = (String) iterator.next();
                  System.out.println(key + ":" + hMap.get(key));
          }
          }
         
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
The output of this code in our case is shown below;
ModDate:D:20110512180811+10'00'
Subject:Metadata Addition to PDF Tutorial
Creator:Thinktibits
Author:Thinktibits
CreationDate:D:20110512180811+10'00'
Producer:iText 5.0.6 (c) 1T3XT BVBA
Keywords:Metadata, iText, PDF
Title:Append Metadata to PDF Example
This output tallies with the input we added to the PDF in the earlier tutorial. More on Metadata to follow;
Do you like these tutorials? Do you have a specific iText problem that you want us to help you with? Provide as your feedback in the comments section.

1 comment:

  1. Thanks for this tutorial..it helped me a lot in my project...

    ReplyDelete