Java
Chilkat2 Java
PDF Update or Add XML Metadata
Demonstrates how to add or update the XML metadata stored in a PDF.Note: This example requires Chilkat v10.1.0 or later.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Pdf;
import com.chilkatsoft.chilkat2.StringBuilder;
import com.chilkatsoft.chilkat2.Xml;
public class ChilkatExample {
public static void main(String[] args) {
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Pdf pdf = new Pdf();
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
try {
pdf.loadFile("qa_data/pdf/blank_with_metadata.pdf");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Xml xml = new Xml();
StringBuilder sbExisting = new StringBuilder();
boolean hasMetadata;
try {
pdf.getMetadata(sbExisting);
hasMetadata = true;
} catch (ChilkatException e) {
hasMetadata = false;
}
if (hasMetadata) {
xml.loadSb(sbExisting, true);
} else {
// Otherwise create the bare minimum XMP metadata.
xml.setTag("x:xmpmeta");
xml.addAttribute("xmlns:x", "adobe:ns:meta/");
xml.addAttribute("x:xmptk", "Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ");
xml.updateAttrAt("rdf:RDF", true, "xmlns:rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "rdf:about", "");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:xmp", "http://ns.adobe.com/xap/1.0/");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:dc", "http://purl.org/dc/elements/1.1/");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:xmpMM", "http://ns.adobe.com/xap/1.0/mm/");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:pdf", "http://ns.adobe.com/pdf/1.3/");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:xmpRights", "http://ns.adobe.com/xap/1.0/rights/");
DateTime dt = new DateTime();
dt.setFromCurrentSystemTime();
String ts = dt.getAsTimestamp(true);
xml.updateChildContent("rdf:RDF|rdf:Description|xmp:ModifyDate", ts);
xml.updateChildContent("rdf:RDF|rdf:Description|xmp:CreateDate", ts);
xml.updateChildContent("rdf:RDF|rdf:Description|xmp:MetadataDate", ts);
xml.updateChildContent("rdf:RDF|rdf:Description|xmp:CreatorTool", "My Custom Application");
xml.updateChildContent("rdf:RDF|rdf:Description|dc:format", "application/pdf");
StringBuilder sbDocId = new StringBuilder();
sbDocId.append("uuid:");
sbDocId.appendUuid(true);
xml.updateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID", sbDocId.getAsString());
StringBuilder sbInstanceId = new StringBuilder();
sbInstanceId.append("uuid:");
sbInstanceId.appendUuid(true);
xml.updateChildContent("rdf:RDF|rdf:Description|xmpMM:InstanceID", sbInstanceId.getAsString());
}
// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "xmlns:zf", "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#");
xml.updateAttrAt("rdf:RDF|rdf:Description", true, "rdf:about", " ");
xml.updateChildContent("rdf:RDF|rdf:Description|zf:ConformanceLevel", "BASIC");
xml.updateChildContent("rdf:RDF|rdf:Description|zf:DocumentFileName", "ZZZZZZ-invoice.xml");
xml.updateChildContent("rdf:RDF|rdf:Description|zf:DocumentType", "INVOICE");
xml.updateChildContent("rdf:RDF|rdf:Description|zf:Version", "1.0");
// Create a new PDF with the updated metadata.
StringBuilder sb = new StringBuilder();
xml.getXmlSb(sb);
try {
pdf.updateMetadata(sb, "c:/temp/qa_output/out.pdf");
System.out.println("Success");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
}
// To see the metadata in Adobe Acrobat DC,
// 1) Open the PDF.
// 2) CTRL-D to show the Document Properties dialog.
// 3) In the dialog, click on the "Additional Metadata" button.
// 4) In the Additional Data dialog, click on "Advanced"
// 5) Expand the namespace tree for the metadata you added.
System.out.println("OK");
}
}