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

Remove a Watermark from a PDF

See more PDF Watermark Examples

Demonstrates remove, which deletes the watermarks matching the filter and reports how many went. The filter is the same one list takes, so the usual sequence is to list what is there and then remove by id, by source, or by text. An empty filter is refused unless "all" is true, so that a whole document cannot be stripped by an empty JSON object. Removing finds nothing is not an error: "removed" is simply 0. An annotation is deleted, the marked content of an artifact and any form it alone used are deleted, and the content of a layer goes with the layer once nothing else uses it. A watermark added with the content approach is part of the page and is never removable.

Chilkat SQL Server Downloads

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

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

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

    --  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
    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

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'count'
    PRINT 'watermarks: ' + @iTmp0
    EXEC sp_OASetProperty @jsonOut, 'I', 0

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

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

    --  Add one of our own beside it, so that the filters below have two to choose between.
    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'SUPERSEDED'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'superseded'
    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 @filter
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    --  Remove only what Acrobat put there, leaving ours in place.
    EXEC sp_OAMethod @filter, 'UpdateString', @success OUT, 'source', 'acrobat'
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'remove', @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
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'removed'
    PRINT 'removed (acrobat): ' + @iTmp0

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

    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'detect', @empty, @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 @json
        EXEC @hr = sp_OADestroy @empty
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'count'
    PRINT 'left in the document: ' + @iTmp0

    --  An empty filter is refused for remove: say all explicitly to strip everything.
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'remove', @empty, @jsonOut
    IF @success = 0
      BEGIN

        PRINT 'An empty filter was refused, as it should be.'
      END
    EXEC sp_OAMethod @empty, 'UpdateBool', @success OUT, 'all', 1
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'remove', @empty, @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 @json
        EXEC @hr = sp_OADestroy @empty
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'IntOf', @iTmp0 OUT, 'removed'
    PRINT 'removed (all): ' + @iTmp0

    --  The changes are in memory until the file is written.
    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/acrobat_unwatermarked.pdf'
    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 @json
        EXEC @hr = sp_OADestroy @empty
        RETURN
      END

    --  Sample output:

    --  watermarks: 1
    --  first id: ext1 text: Sample
    --  removed (acrobat): 1
    --  left in the document: 1
    --  An empty filter was refused, as it should be.
    --  removed (all): 1

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


END
GO