PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Pdf
oleobject loo_Xml
oleobject loo_SbExisting
integer li_HasMetadata
oleobject loo_Dt
string ls_Ts
oleobject loo_SbDocId
oleobject loo_SbInstanceId
oleobject loo_Sb
li_Success = 0
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
destroy loo_Pdf
MessageBox("Error","Connecting to COM object failed")
return
end if
// Load a PDF file.
// If the PDF file already has metadata, we'll update it.
// Otherwise this example adds the metadata.
li_Success = loo_Pdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf")
if li_Success = 0 then
Write-Debug loo_Pdf.LastErrorText
destroy loo_Pdf
return
end if
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_SbExisting = create oleobject
li_rc = loo_SbExisting.ConnectToNewObject("Chilkat.StringBuilder")
li_HasMetadata = loo_Pdf.GetMetadata(loo_SbExisting)
if li_HasMetadata = 1 then
loo_Xml.LoadSb(loo_SbExisting,1)
else
// Otherwise create the bare minimum XMP metadata.
loo_Xml.Tag = "x:xmpmeta"
loo_Xml.AddAttribute("xmlns:x","adobe:ns:meta/")
loo_Xml.AddAttribute("x:xmptk","Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ")
loo_Xml.UpdateAttrAt("rdf:RDF",1,"xmlns:rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"rdf:about","")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmp","http://ns.adobe.com/xap/1.0/")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:dc","http://purl.org/dc/elements/1.1/")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmpMM","http://ns.adobe.com/xap/1.0/mm/")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:pdf","http://ns.adobe.com/pdf/1.3/")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmpRights","http://ns.adobe.com/xap/1.0/rights/")
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")
loo_Dt.SetFromCurrentSystemTime()
ls_Ts = loo_Dt.GetAsTimestamp(1)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:ModifyDate",ls_Ts)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:CreateDate",ls_Ts)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:MetadataDate",ls_Ts)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:CreatorTool","My Custom Application")
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|dc:format","application/pdf")
loo_SbDocId = create oleobject
li_rc = loo_SbDocId.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbDocId.Append("uuid:")
loo_SbDocId.AppendUuid(1)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID",loo_SbDocId.GetAsString())
loo_SbInstanceId = create oleobject
li_rc = loo_SbInstanceId.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbInstanceId.Append("uuid:")
loo_SbInstanceId.AppendUuid(1)
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:InstanceID",loo_SbInstanceId.GetAsString())
end if
// Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:zf","urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#")
loo_Xml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"rdf:about"," ")
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|zf:ConformanceLevel","BASIC")
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|zf:DocumentFileName","ZZZZZZ-invoice.xml")
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|zf:DocumentType","INVOICE")
loo_Xml.UpdateChildContent("rdf:RDF|rdf:Description|zf:Version","1.0")
// Create a new PDF with the updated metadata.
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
loo_Xml.GetXmlSb(loo_Sb)
li_Success = loo_Pdf.UpdateMetadata(loo_Sb,"c:/temp/qa_output/out.pdf")
if li_Success = 1 then
Write-Debug "Success"
else
Write-Debug loo_Pdf.LastErrorText
end if
// 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.
Write-Debug "OK"
destroy loo_Pdf
destroy loo_Xml
destroy loo_SbExisting
destroy loo_Dt
destroy loo_SbDocId
destroy loo_SbInstanceId
destroy loo_Sb