Java iText Update PDF Metadata Example Tutorial

We now know how to add and view metadata stored in a PDF document using Java iText API. What we have not discussed is a method that describes how to update Metadata in a PDF document using iText. This post aims to bridge that gap, and will provide a step by step tutorial to modify a PDF file to edit its metadata contents and add new one on top of it. I will not be using the XMP approach to add metadata for this tutorial; this will be covered in a different post at a different time. We will write a simple Java code that will read a PDF document and construct a Hashmap to update the metadata inside the PDF. Note that you also use this method to remove an existing metadata from PDF, by setting the relevant attribute to blank values. As this post deals with updating PDF documents in Java, we will be using a PdfStamper class.

Step-1: We begin by creating a PdfReader object that will read the input PDF that needs to have its metadata edited. We also create a PdfStamper object that will hold the modified PDF document with new metadata information. The code example to achieve this is provided below;
          PdfReader ReadInputPDF;
          ReadInputPDF = new PdfReader("sample.pdf");
          PdfStamper stamper =new PdfStamper(ReadInputPDF, new FileOutputStream("PDF_Update_Metadata.pdf"));
Step-2:From Step-1 code, you can understand that "sample.pdf" would be read and a new file "PDF_Update_Metadata.pdf" would be created that will have the metadata stored in sample.pdf modified.To modify the metadata information, we use getinfo method on the PdfReader object to read the existing metadata into a hashmap in Java. We then use hashmap.put and provide new values for all the key fields [ Title, Author etc] inside the hashmap. The code to do this is provided below;
          HashMap<String, String> hMap = ReadInputPDF.getInfo();          
          hMap.put("Title", "Edit PDF Metadata");
          hMap.put("Subject", "Modify PDF Metadata Example");
          hMap.put("Keywords", "Extract PDF Metadata,Metadata Extraction,Remove Metadata from PDF");
          hMap.put("Creator", "Thinktibits");
          hMap.put("Author", "Thinktibits");
Step-3: To stamp the modified metadata created in Step2, we use PdfStamper. setMoreInfo method. This method accepts a hashmap and we pass the hashmap with required metadata created earlier, to this method. Once this is done, the new modified PDF document will contain the updated metadata information. We can close the PdfStamper object. The code that does this piece is provided below for reference:
          stamper.setMoreInfo(hMap);
          stamper.close();          
And, if you want to update metadata information in a PDF document, these are the only steps that needs to be done. Very easy and straightforward when done through iText. The complete code example that tells you how to update metadata in a PDF file is provided below;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.util.HashMap;
public class UpdatePDFMetadata{  
     public static void main(String[] args){
        try {
          PdfReader ReadInputPDF;
          ReadInputPDF = new PdfReader("sample.pdf");
          PdfStamper stamper =new PdfStamper(ReadInputPDF, new FileOutputStream("PDF_Update_Metadata.pdf"));
          HashMap<String, String> hMap = ReadInputPDF.getInfo();          
          hMap.put("Title", "Edit PDF Metadata");
          hMap.put("Subject", "Modify PDF Metadata Example");
          hMap.put("Keywords", "Extract PDF Metadata,Metadata Extraction,Remove Metadata from PDF");
          hMap.put("Creator", "Thinktibits");
          hMap.put("Author", "Thinktibits");
          stamper.setMoreInfo(hMap);
          stamper.close();          
          }         
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
Give a try and if you are stuck, let us know in the comments section.

1 comment: