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
(Node.js) 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.
var os = require('os'); if (os.platform() == 'win32') { if (os.arch() == 'ia32') { var chilkat = require('@chilkat/ck-node21-win-ia32'); } else { var chilkat = require('@chilkat/ck-node21-win64'); } } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node21-arm'); } else if (os.arch() == 'x86') { var chilkat = require('@chilkat/ck-node21-linux32'); } else { var chilkat = require('@chilkat/ck-node21-linux64'); } } else if (os.platform() == 'darwin') { if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node21-mac-m1'); } else { var chilkat = require('@chilkat/ck-node21-macosx'); } } function chilkatExample() { // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. var pdf = new chilkat.Pdf(); // Load a PDF file. // If the PDF file already has metadata, we'll update it. // Otherwise this example adds the metadata. var success = pdf.LoadFile("qa_data/pdf/blank_with_metadata.pdf"); if (success == false) { console.log(pdf.LastErrorText); return; } var xml = new chilkat.Xml(); var sbExisting = new chilkat.StringBuilder(); var hasMetadata = pdf.GetMetadata(sbExisting); if (hasMetadata == true) { xml.LoadSb(sbExisting,true); } else { // Otherwise create the bare minimum XMP metadata. xml.Tag = "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/"); var dt = new chilkat.CkDateTime(); dt.SetFromCurrentSystemTime(); var 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"); var sbDocId = new chilkat.StringBuilder(); sbDocId.Append("uuid:"); sbDocId.AppendUuid(true); xml.UpdateChildContent("rdf:RDF|rdf:Description|xmpMM:DocumentID",sbDocId.GetAsString()); var sbInstanceId = new chilkat.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. var sb = new chilkat.StringBuilder(); xml.GetXmlSb(sb); success = pdf.UpdateMetadata(sb,"c:/temp/qa_output/out.pdf"); if (success == true) { console.log("Success"); } else { console.log(pdf.LastErrorText); } // 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. console.log("OK"); } chilkatExample(); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.