Issue to modify metadata dc:subject in an existing pdf file

Hello,


I have an input.pdf. The value of dc:subject is sub.
I want to change its value from sub into something else using the following code.
Before saving it to the output.pdf file, the dc:subject has the new values sub1 and sub2.
But after saving it to output.pdf, the dc:subject becomes sub, sub1, sub2.
So instead of replacing the value, it simply add the new values to dc:subject.
Did I do something wrong? What is the right way to modify the metadata in a pdf file?
Thanks.


Document doc = new Document(“C:/temp/pdf/utilities/metadata/input.pdf”);
PdfXmpMetadata xmpMeta = new PdfXmpMetadata();
xmpMeta.bindPdf(doc);

xmpMeta.removeItem(“dc:subject”);
Object[] newArr = new Object[] { “sub1”, “sub2” };
xmpMeta.set_Item(“dc:subject”, newArr);

Object[] arr = (Object[]) xmpMeta.get_Item(“dc:subject”);
System.out.println(arr.length);
System.out.println(arr[0]);

doc.save(“C:/temp/pdf/utilities/metadata/output.pdf”);
doc.close();
xmpMeta.close();

Hi there,


Thanks for your inquriy. We will appreciate it if you please share your sample source document here as well. We will look into it and will guide you accordingly.

We are sorry for the inconvenience.

Best Regards,

Hi Tilal,



Thank you for your response!

Attached is the input.pdf which is just a simple normal pdf file.



Please take a look.

Thanks again!



Jeff

Hi Jeff,


Thanks for sharing the input file.

I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFJAVA-36144 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Cool. Thanks.

@jeffguo

We would like to share with you that we have resolved the earlier logged ticket. “dc:subject” is a container for keywords so for updating it the code snippet below could be used:

Document doc = new Document("C:/pdftest/input (2).pdf");    
doc.getInfo().setKeywords("sub1");
doc.getMetadata().set_Item("dc:subject", new XmpValue("sub2"));
doc.save("C:/pdftest/MetaData_Updated_output.pdf");