Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- 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.