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
(SQL Server) 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.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @pdf int -- Use "Chilkat_9_5_0.Pdf" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END -- Load a PDF file. -- If the PDF file already has metadata, we'll update it. -- Otherwise this example adds the metadata. DECLARE @success int EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/blank_with_metadata.pdf' IF @success = 0 BEGIN EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @pdf RETURN END DECLARE @xml int -- Use "Chilkat_9_5_0.Xml" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT DECLARE @sbExisting int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbExisting OUT DECLARE @hasMetadata int EXEC sp_OAMethod @pdf, 'GetMetadata', @hasMetadata OUT, @sbExisting IF @hasMetadata = 1 BEGIN EXEC sp_OAMethod @xml, 'LoadSb', @success OUT, @sbExisting, 1 END ELSE BEGIN -- Otherwise create the bare minimum XMP metadata. EXEC sp_OASetProperty @xml, 'Tag', 'x:xmpmeta' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'xmlns:x', 'adobe:ns:meta/' EXEC sp_OAMethod @xml, 'AddAttribute', @success OUT, 'x:xmptk', 'Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 ' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF', 1, 'xmlns:rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'rdf:about', '' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:xmp', 'http://ns.adobe.com/xap/1.0/' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:dc', 'http://purl.org/dc/elements/1.1/' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:xmpMM', 'http://ns.adobe.com/xap/1.0/mm/' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:pdf', 'http://ns.adobe.com/pdf/1.3/' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:xmpRights', 'http://ns.adobe.com/xap/1.0/rights/' DECLARE @dt int -- Use "Chilkat_9_5_0.CkDateTime" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @dt OUT EXEC sp_OAMethod @dt, 'SetFromCurrentSystemTime', @success OUT DECLARE @ts nvarchar(4000) EXEC sp_OAMethod @dt, 'GetAsTimestamp', @ts OUT, 1 EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmp:ModifyDate', @ts EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmp:CreateDate', @ts EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmp:MetadataDate', @ts EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmp:CreatorTool', 'My Custom Application' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|dc:format', 'application/pdf' DECLARE @sbDocId int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbDocId OUT EXEC sp_OAMethod @sbDocId, 'Append', @success OUT, 'uuid:' EXEC sp_OAMethod @sbDocId, 'AppendUuid', @success OUT, 1 EXEC sp_OAMethod @sbDocId, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmpMM:DocumentID', @sTmp0 DECLARE @sbInstanceId int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbInstanceId OUT EXEC sp_OAMethod @sbInstanceId, 'Append', @success OUT, 'uuid:' EXEC sp_OAMethod @sbInstanceId, 'AppendUuid', @success OUT, 1 EXEC sp_OAMethod @sbInstanceId, 'GetAsString', @sTmp0 OUT EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|xmpMM:InstanceID', @sTmp0 END -- Add our custom metadata tags to either the existing XML metdata, or the newly created metadata. EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'xmlns:zf', 'urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#' EXEC sp_OAMethod @xml, 'UpdateAttrAt', @success OUT, 'rdf:RDF|rdf:Description', 1, 'rdf:about', ' ' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|zf:ConformanceLevel', 'BASIC' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|zf:DocumentFileName', 'ZZZZZZ-invoice.xml' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|zf:DocumentType', 'INVOICE' EXEC sp_OAMethod @xml, 'UpdateChildContent', NULL, 'rdf:RDF|rdf:Description|zf:Version', '1.0' -- Create a new PDF with the updated metadata. DECLARE @sb int -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT EXEC sp_OAMethod @xml, 'GetXmlSb', @success OUT, @sb EXEC sp_OAMethod @pdf, 'UpdateMetadata', @success OUT, @sb, 'c:/temp/qa_output/out.pdf' IF @success = 1 BEGIN PRINT 'Success' END ELSE BEGIN EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 END -- 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. PRINT 'OK' EXEC @hr = sp_OADestroy @pdf EXEC @hr = sp_OADestroy @xml EXEC @hr = sp_OADestroy @sbExisting EXEC @hr = sp_OADestroy @dt EXEC @hr = sp_OADestroy @sbDocId EXEC @hr = sp_OADestroy @sbInstanceId EXEC @hr = sp_OADestroy @sb END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.