Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Lianja) PDF Update or Add XML MetadataDemonstrates how to add or update the XML metadata stored in a PDF. Note: This example requires Chilkat v10.1.0 or later.
// This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loPdf = createobject("CkPdf") // Load a PDF file. // If the PDF file already has metadata, we'll update it. // Otherwise this example adds the metadata. llSuccess = loPdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf") if (llSuccess = .F.) then ? loPdf.LastErrorText release loPdf return endif loXml = createobject("CkXml") loSbExisting = createobject("CkStringBuilder") llHasMetadata = loPdf.GetMetadata(loSbExisting) if (llHasMetadata = .T.) then loXml.LoadSb(loSbExisting,.T.) 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",.T.,"xmlns:rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"rdf:about","") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"xmlns:xmp","http://ns.adobe.com/xap/1.0/") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"xmlns:dc","http://purl.org/dc/elements/1.1/") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"xmlns:xmpMM","http://ns.adobe.com/xap/1.0/mm/") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"xmlns:pdf","http://ns.adobe.com/pdf/1.3/") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"xmlns:xmpRights","http://ns.adobe.com/xap/1.0/rights/") loDt = createobject("CkDateTime") loDt.SetFromCurrentSystemTime() lcTs = loDt.GetAsTimestamp(.T.) 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("CkStringBuilder") loSbDocId.Append("uuid:") loSbDocId.AppendUuid(.T.) loXml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID",loSbDocId.GetAsString()) loSbInstanceId = createobject("CkStringBuilder") loSbInstanceId.Append("uuid:") loSbInstanceId.AppendUuid(.T.) 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",.T.,"xmlns:zf","urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#") loXml.UpdateAttrAt("rdf:RDF|rdf:Description",.T.,"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("CkStringBuilder") loXml.GetXmlSb(loSb) llSuccess = loPdf.UpdateMetadata(loSb,"c:/temp/qa_output/out.pdf") if (llSuccess = .T.) 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 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.