Visual FoxPro
Visual FoxPro
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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loPdf
LOCAL loXml
LOCAL loSbExisting
LOCAL lnHasMetadata
LOCAL loDt
LOCAL lcTs
LOCAL loSbDocId
LOCAL loSbInstanceId
LOCAL loSb
lnSuccess = 0
* This requires the Chilkat API to have been previously unlocked.
* See Global Unlock Sample for sample code.
loPdf = CreateObject('Chilkat.Pdf')
* Load a PDF file.
* If the PDF file already has metadata, we'll update it.
* Otherwise this example adds the metadata.
lnSuccess = loPdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf")
IF (lnSuccess = 0) THEN
? loPdf.LastErrorText
RELEASE loPdf
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
loSbExisting = CreateObject('Chilkat.StringBuilder')
lnHasMetadata = loPdf.GetMetadata(loSbExisting)
IF (lnHasMetadata = 1) THEN
loXml.LoadSb(loSbExisting,1)
ELSE
* Otherwise create the bare minimum XMP metadata.
loXml.Tag = "x:xmpmeta"
loXml.AddAttribute("xmlns:x","adobe:ns:meta/")
loXml.AddAttribute("x:xmptk","Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ")
loXml.UpdateAttrAt("rdf:RDF",1,"xmlns:rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"rdf:about","")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmp","http://ns.adobe.com/xap/1.0/")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:dc","http://purl.org/dc/elements/1.1/")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmpMM","http://ns.adobe.com/xap/1.0/mm/")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:pdf","http://ns.adobe.com/pdf/1.3/")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:xmpRights","http://ns.adobe.com/xap/1.0/rights/")
loDt = CreateObject('Chilkat.CkDateTime')
loDt.SetFromCurrentSystemTime()
lcTs = loDt.GetAsTimestamp(1)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:ModifyDate",lcTs)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:CreateDate",lcTs)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:MetadataDate",lcTs)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmp:CreatorTool","My Custom Application")
loXml.UpdateChildContent("rdf:RDF|rdf:Description|dc:format","application/pdf")
loSbDocId = CreateObject('Chilkat.StringBuilder')
loSbDocId.Append("uuid:")
loSbDocId.AppendUuid(1)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID",loSbDocId.GetAsString())
loSbInstanceId = CreateObject('Chilkat.StringBuilder')
loSbInstanceId.Append("uuid:")
loSbInstanceId.AppendUuid(1)
loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:InstanceID",loSbInstanceId.GetAsString())
ENDIF
* Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"xmlns:zf","urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#")
loXml.UpdateAttrAt("rdf:RDF|rdf:Description",1,"rdf:about"," ")
loXml.UpdateChildContent("rdf:RDF|rdf:Description|zf:ConformanceLevel","BASIC")
loXml.UpdateChildContent("rdf:RDF|rdf:Description|zf:DocumentFileName","ZZZZZZ-invoice.xml")
loXml.UpdateChildContent("rdf:RDF|rdf:Description|zf:DocumentType","INVOICE")
loXml.UpdateChildContent("rdf:RDF|rdf:Description|zf:Version","1.0")
* Create a new PDF with the updated metadata.
loSb = CreateObject('Chilkat.StringBuilder')
loXml.GetXmlSb(loSb)
lnSuccess = loPdf.UpdateMetadata(loSb,"c:/temp/qa_output/out.pdf")
IF (lnSuccess = 1) THEN
? "Success"
ELSE
? loPdf.LastErrorText
ENDIF
* 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.
? "OK"
RELEASE loPdf
RELEASE loXml
RELEASE loSbExisting
RELEASE loDt
RELEASE loSbDocId
RELEASE loSbInstanceId
RELEASE loSb