SQL Server
SQL Server
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 SQL Server Downloads
-- 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)
DECLARE @success int
SELECT @success = 0
-- This requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @pdf int
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.
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
EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT
DECLARE @sbExisting int
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
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
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
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
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