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

Add a CONFIDENTIAL Watermark to Every Page of a PDF

See more PDF Watermark Examples

Demonstrates the simplest watermark: a line of text across every page. "rotation" set to diagonal runs the text from the bottom-left corner to the top-right, computed for each page size, and "scale" with fit page sizes it to fill 80 percent of the page. The watermark is stored as a marked artifact by default, which means viewers show it, text extraction leaves it out, and it can be found and removed again later. Nothing is written until WriteFile is called.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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 long_report.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/long_report.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', 'text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'CONFIDENTIAL'

    --  An id of your own choosing, so that the watermark can be found and removed later.  One is
    --  generated when you do not give one.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'confidential'

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'color', '#C00000'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'opacity', '0.25'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'rotation', 'diagonal'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'scale.fit', 'page'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'scale.fitPercent', '80'

    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

    --  The id, and the pages it was stamped on.  "pages" defaults to all.

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

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

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/report_confidential.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: confidential
    --  pages: 1-3

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


END
GO