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

Watermark Only Some of a PDF's Pages

See more PDF Watermark Examples

Demonstrates choosing the pages to stamp. "pages" takes all (the default), first, last, odd, even, or a list of 1-based pages and ranges such as 1,3,5-9,12- where a trailing dash means "to the end". "excludePages" takes the same syntax and is applied afterwards, which is how you say "every page except the cover". The result reports the pages the watermark actually landed on.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 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 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

    EXEC sp_OAGetProperty @pdf, 'NumPages', @iTmp0 OUT
    PRINT 'pages in the document: ' + @iTmp0

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

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

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'INTERNAL'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'internal'

    --  Every page except the first.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'pages', 'all'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'excludePages', '1'
    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, 'pages'
    PRINT 'every page but the cover: ' + @sTmp0

    --  A list of pages and ranges, on a second copy of the document.
    DECLARE @pdf2 int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf2 OUT

    EXEC sp_OAMethod @pdf2, 'LoadFile', @success OUT, 'qa_data/pdf/four_pages.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @pdf2
        RETURN
      END
    DECLARE @json2 int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT

    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'type', 'text'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'text', 'APPENDIX'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'id', 'appendix'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'pages', '2,4-'
    EXEC sp_OAMethod @pdf2, 'Watermark', @success OUT, 'add', @json2, @jsonOut
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pdf2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @pdf2
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END

    EXEC sp_OAMethod @jsonOut, 'StringOf', @sTmp0 OUT, 'pages'
    PRINT 'page 2 and 4 onwards: ' + @sTmp0

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/four_pages_internal.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
        EXEC @hr = sp_OADestroy @pdf2
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END

    --  Sample output:

    --  pages in the document: 4
    --  every page but the cover: 2-4
    --  page 2 and 4 onwards: 2,4

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


END
GO