Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.7.0+

Add an Image (Logo) Watermark to a PDF

See more PDF Watermark Examples

Demonstrates an image watermark placed in a corner of every page. The image is a PNG or JPEG given either by path ("image.path") or, as here, as base64 in the JSON itself ("image.base64"), which suits a server that keeps its logo in a database rather than on disk. PNG transparency is kept. Giving only a width keeps the aspect ratio, and the units member says those numbers are inches rather than points.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    --  The PDF used in this example is available at four_pages.pdf

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

    EXEC sp_OAMethod @pdf, 'LoadFile', @success OUT, 'qa_data/pdf/four_pages.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'image'

    --  A small PNG logo, base64 encoded.  Use "image.path" instead to name a PNG or JPEG file:
    --     ignore = json.UpdateString("image.path","logo.png");
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'image.base64', 'iVBORw0KGgoAAAANSUhEUgAAAGAAAAAgCAYAAADtwH1UAAAApElEQVR42u3a7QmAIBgAYTdpo/ZphhZti6IBylLrlXoO/G3cIeFHSgAAFDKM08rCy8Jzg6Ug8UJ0JF+EDuSXRGgxX8+jufxlTk0nFuCCiF360aidXICMiDP5uQgCXAxQI782ggAHEu7IP4sgQOcBIMA35ItgBQhQEqBmtbX6xl//hAUI3gcIELwTFiD4LEiA4NNQAYLvA+BGzJ0wvIrwLggA8DgbwPbrr8c7+lkAAAAASUVORK5CYII='

    --  The size to draw it at.  Give only the width and the height follows the aspect ratio.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'units', 'in'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'image.width', '1.5'

    --  Bottom-right, half an inch in from the page edges.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'position.align', 'bottomRight'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'position.margin', '0.5'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'opacity', '0.85'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'logo'

    DECLARE @jsonOut int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonOut OUT

    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'add', @json, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'id'

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp1 OUT, 'pages'
    PRINT 'id: ' + @sTmp0 + ' pages: ' + @sTmp1

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/four_pages_logo.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        RETURN
      END

    --  Sample output:

    --  id: logo pages: 1-4

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @jsonOut


END
GO