PeopleSoft Edit PDF Metadata Example Tutorial

We have discussed the procedure to add PDF Metadata through PeopleCode and read back the metadata information stamped into a PDF document. In this post, we will describe a method to update an existing PDF document's metadata using PeopleCode. It is assumed that the metadata available inside the PDF is not an XMP embedded stream. We will be covering how to embed XMP Metadata Stream into a PDF document using Peoplecode in a separate tutorial section.For now, you can assume that you have a PDF file with existing Metadata information like Title, Author, Subject, Keyword all written into an info dictionary. And you want to update that information using PeopleCode. We will get along with the step by step tutorial to do this now;

The example is a very straightforward one. We read the incoming PDF file into a PdfReader object and create a PdfStamper object that will hold the modified PDF file. This PdfStamper object will also hold the modified metadata in the updated PDF file. The code example is provided below;
/* Read incoming PDF file */
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Metadata.pdf");
/* Create PdfStamper object */
Local JavaObject &Obj_updatedPDF = CreateJavaObject("com.lowagie.text.pdf.PdfStamper", &Obj_read_existing_pdf_l, CreateJavaObject("java.io.FileOutputStream", "Metadata_Updated.pdf", True));
Once this is done,create a Hashmap object in PeopleCode, and use getInfo method of PdfReader class, to read the existing metadata into this hashmap.
/* Create Hashmap object */
Local JavaObject &Obj_hashmap_l = CreateJavaObject("java.util.HashMap");
/* Read Existing Metadata into Hashmap */
&Obj_hashmap_l = &Obj_read_existing_pdf_l.getInfo();
We can now use the "put" method of the Hashmap object to modify the "key" metadata fields and provide the new value.
/* Edit Title */
&Obj_hashmap_l.put("Title", "Edit PDF Metadata from PeopleCode");
/* Modify Subject */
&Obj_hashmap_l.put("Subject", "PeopleSoft Modify PDF Metadata Example");
/* Define new Keywords */
&Obj_hashmap_l.put("Keywords", "Extract PDF Metadata Using PeopleCode,Metadata Extraction in PeopleSoft,Remove Metadata from PDF through PeopleCode");
/* Update Creator */
&Obj_hashmap_l.put("Creator", "Test");
/* Edit Author Info */
&Obj_hashmap_l.put("Author", "Test");
We are almost done. We have to copy this updated Hashmap with new metadata information into the modified PDF file. To do this, we use setMoreInfo method of PdfStamper object and pass this hashmap as an input. This action will write the new metadata into the PDF file and the PdfStamper object can be closed following this step. The PeopleCode is provided below;
&Obj_updatedPDF.setMoreInfo(&Obj_hashmap_l);
&Obj_updatedPDF.close();
The complete PeopleCode example to update the metadata information of an existing PDF document is provided below;
Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Metadata.pdf");
Local JavaObject &Obj_updatedPDF = CreateJavaObject("com.lowagie.text.pdf.PdfStamper", &Obj_read_existing_pdf_l, CreateJavaObject("java.io.FileOutputStream", "Metadata_Updated.pdf", True));
Local JavaObject &Obj_hashmap_l = CreateJavaObject("java.util.HashMap");
&Obj_hashmap_l = &Obj_read_existing_pdf_l.getInfo();
&Obj_hashmap_l.put("Title", "Edit PDF Metadata from PeopleCode");
&Obj_hashmap_l.put("Subject", "PeopleSoft Modify PDF Metadata Example");
&Obj_hashmap_l.put("Keywords", "Extract PDF Metadata Using PeopleCode,Metadata Extraction in PeopleSoft,Remove Metadata from PDF through PeopleCode");
&Obj_hashmap_l.put("Creator", "Test");
&Obj_hashmap_l.put("Author", "Test");
&Obj_updatedPDF.setMoreInfo(&Obj_hashmap_l);
&Obj_updatedPDF.close();

You can compare the metadata output comparison between the two PDF files below. Note that we did not change the creation time stamp (marked in the image below) and hence it remains intact.
PDF Update Metadata
Metadata Output Comparison

1 comment:

  1. YellowRose, this is great! Do you by chance have any examples of how to open up an encrypted PDF, make the changes, then re-encrypt it with the same password? I've figured out how to open a PDF that is encrypted but when it saves the new file, it's not encrypted. The peopleCode I added to open the password protected document is as follows -

    Local JavaObject &passwordString = CreateJavaObject("java.lang.String", "PASSWORD");
    Local JavaObject &pdfPassword = &passwordString.getBytes();
    Local JavaObject &Obj_read_existing_pdf_l = CreateJavaObject("com.lowagie.text.pdf.PdfReader", "Metadata,pdf", &pdfPassword);

    ReplyDelete