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

DataFlex 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
Async
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
Secrets
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

 

 

 

(DataFlex) 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

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoPdf
    Boolean iSuccess
    Handle hoXml
    Variant vSbExisting
    Handle hoSbExisting
    Boolean iHasMetadata
    Handle hoDt
    String sTs
    Handle hoSbDocId
    Handle hoSbInstanceId
    Variant vSb
    Handle hoSb
    String sTemp1

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End

    // Load a PDF file.
    // If the PDF file already has metadata, we'll update it.
    // Otherwise this example adds the metadata.
    Get ComLoadFile Of hoPdf "qa_data/pdf/blank_with_metadata.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbExisting
    If (Not(IsComObjectCreated(hoSbExisting))) Begin
        Send CreateComObject of hoSbExisting
    End

    Get pvComObject of hoSbExisting to vSbExisting
    Get ComGetMetadata Of hoPdf vSbExisting To iHasMetadata
    If (iHasMetadata = True) Begin
        Get pvComObject of hoSbExisting to vSbExisting
        Get ComLoadSb Of hoXml vSbExisting True To iSuccess
    End
    Else Begin

        // Otherwise create the bare minimum XMP metadata.
        Set ComTag Of hoXml To "x:xmpmeta"
        Get ComAddAttribute Of hoXml "xmlns:x" "adobe:ns:meta/" To iSuccess
        Get ComAddAttribute Of hoXml "x:xmptk" "Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16 " To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF" True "xmlns:rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "rdf:about" "" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmp" "http://ns.adobe.com/xap/1.0/" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:dc" "http://purl.org/dc/elements/1.1/" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmpMM" "http://ns.adobe.com/xap/1.0/mm/" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:pdf" "http://ns.adobe.com/pdf/1.3/" To iSuccess
        Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:xmpRights" "http://ns.adobe.com/xap/1.0/rights/" To iSuccess

        Get Create (RefClass(cComCkDateTime)) To hoDt
        If (Not(IsComObjectCreated(hoDt))) Begin
            Send CreateComObject of hoDt
        End
        Get ComSetFromCurrentSystemTime Of hoDt To iSuccess
        Get ComGetAsTimestamp Of hoDt True To sTs

        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:ModifyDate" sTs
        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:CreateDate" sTs
        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:MetadataDate" sTs

        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmp:CreatorTool" "My Custom Application"
        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|dc:format" "application/pdf"

        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbDocId
        If (Not(IsComObjectCreated(hoSbDocId))) Begin
            Send CreateComObject of hoSbDocId
        End
        Get ComAppend Of hoSbDocId "uuid:" To iSuccess
        Get ComAppendUuid Of hoSbDocId True To iSuccess
        Get ComGetAsString Of hoSbDocId To sTemp1
        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmpMM:DocumentID" sTemp1

        Get Create (RefClass(cComChilkatStringBuilder)) To hoSbInstanceId
        If (Not(IsComObjectCreated(hoSbInstanceId))) Begin
            Send CreateComObject of hoSbInstanceId
        End
        Get ComAppend Of hoSbInstanceId "uuid:" To iSuccess
        Get ComAppendUuid Of hoSbInstanceId True To iSuccess
        Get ComGetAsString Of hoSbInstanceId To sTemp1
        Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|xmpMM:InstanceID" sTemp1

    End

    // Add our custom metadata tags to either the existing XML metdata, or the newly created metadata.
    Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "xmlns:zf" "urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#" To iSuccess
    Get ComUpdateAttrAt Of hoXml "rdf:RDF|rdf:Description" True "rdf:about" " " To iSuccess
    Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:ConformanceLevel" "BASIC"
    Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:DocumentFileName" "ZZZZZZ-invoice.xml"
    Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:DocumentType" "INVOICE"
    Send ComUpdateChildContent To hoXml "rdf:RDF|rdf:Description|zf:Version" "1.0"

    // Create a new PDF with the updated metadata.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoSb to vSb
    Get ComGetXmlSb Of hoXml vSb To iSuccess
    Get pvComObject of hoSb to vSb
    Get ComUpdateMetadata Of hoPdf vSb "c:/temp/qa_output/out.pdf" To iSuccess
    If (iSuccess = True) Begin
        Showln "Success"
    End
    Else Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
    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.

    Showln "OK"


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.