Sample code for 30+ languages & platforms
SQL Server

Add XMP MetaData to JPG or TIFF

See more XMP Examples

Demonstrates how to add XMP metadata to a JPG or TIFF image that doesn't already have XMP metadata.

Chilkat SQL Server Downloads

SQL Server
-- 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 example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @xmp int
    EXEC @hr = sp_OACreate 'Chilkat.Xmp', @xmp OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @xml int

    -- The first step is to create a new XMP document, which is nothing
    -- more than XML.  The NewXmp method returns an XML document with
    -- the standard XMP boilerplate.  
    EXEC sp_OAMethod @xmp, 'NewXmp', @xml OUT

    -- Add some properties...
    EXEC sp_OAMethod @xmp, 'AddSimpleStr', @success OUT, @xml, 'Iptc4xmpCore:Chilkat', 'Blah blah'
    -- If you wish to view the XML, save it to a file and review it
    -- with a text editor:
    EXEC sp_OAMethod @xml, 'SaveXml', @success OUT, 'newXmp.xml'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xml, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmp
        RETURN
      END

    -- To add the XMP to the JPG (or TIFF), simply load the JPG,
    -- append the XMP, and save:
    EXEC sp_OAMethod @xmp, 'LoadAppFile', @success OUT, 'scream.jpg'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xmp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmp
        RETURN
      END
    EXEC sp_OAMethod @xmp, 'Append', @success OUT, @xml
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xmp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmp
        RETURN
      END
    EXEC sp_OAMethod @xmp, 'SaveAppFile', @success OUT, 'screamOut.jpg'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @xmp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @xmp
        RETURN
      END

    EXEC @hr = sp_OADestroy @xmp


END
GO