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

Find Out Whether a PDF Has a Watermark

See more PDF Watermark Examples

Demonstrates detect, which answers yes or no, and list, which enumerates what is there. Both recognize the watermarks Chilkat writes and the standard forms other tools write: Watermark annotations from any producer, marked content tagged as a Watermark or Background artifact, Acrobat watermarks and backgrounds, and optional-content layers whose name says watermark. Acrobat headers and footers are not watermarks and are ignored. A watermark from another producer has no id of its own, so Chilkat assigns one (ext1, ext2, ...) that stays valid until the PDF is changed, which is what makes a list followed by a remove by id work. Properties that cannot be read back are left out rather than guessed.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 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 acrobat_two_watermarks.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/acrobat_two_watermarks.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        RETURN
      END

    --  An empty filter means every watermark in the document.
    DECLARE @filter int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @filter OUT

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

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

    EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'found'

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp1 OUT, 'count'
    PRINT 'found: ' + @iTmp0 + ' count: ' + @iTmp1

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

    DECLARE @i int
    SELECT @i = 0
    DECLARE @count_i int
    EXEC sp_OAMethod @jsonOut, 'SizeOfArray', @count_i OUT, 'watermarks'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonOut, 'I', @i

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].id'
        PRINT 'id: ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].source'
        PRINT '   source:    ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].approach'
        PRINT '   approach:  ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].type'
        PRINT '   type:      ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].text'
        PRINT '   text:      ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].pages'
        PRINT '   pages:     ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'watermarks[i].zOrder'
        PRINT '   zOrder:    ' + @sTmp0

        EXEC sp_OAMethod @jsonOut, 'BoolOf', @iTmp0 OUT, 'watermarks[i].removable'
        PRINT '   removable: ' + @iTmp0
        SELECT @i = @i + 1
      END

    --  The filter narrows what is reported: by producer (chilkat, acrobat or other), by approach, by
    --  pages, by an exact id or label, or by text with textContains.
    DECLARE @bySource int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @bySource OUT

    EXEC sp_OAMethod @bySource, 'UpdateString', @success OUT, 'source', 'acrobat'
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'detect', @bySource, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @filter
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @bySource
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'count'
    PRINT 'made by Acrobat: ' + @iTmp0

    --  Sample output:

    --  found: True count: 2
    --  id: ext1
    --     source:    acrobat
    --     approach:  artifact
    --     type:      image
    --     text:
    --     pages:     1
    --     zOrder:    front
    --     removable: True
    --  id: ext2
    --     source:    acrobat
    --     approach:  artifact
    --     type:      text
    --     text:      sample
    --     pages:     1
    --     zOrder:    front
    --     removable: True
    --  made by Acrobat: 2

    EXEC @hr = sp_OADestroy @pdf
    EXEC @hr = sp_OADestroy @filter
    EXEC @hr = sp_OADestroy @jsonOut
    EXEC @hr = sp_OADestroy @bySource


END
GO