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

Page Numbers, Dates and the File Name in a PDF Watermark

See more PDF Watermark Examples

Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.

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 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', 'text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'pagefoot'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'position.align', 'bottom'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'position.margin', '24'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'font.size', '9'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'color', '#505050'

    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, 'pages'
    PRINT 'footer pages: ' + @sTmp0

    --  Roman numerals that start at i on page 2, so that a cover page is not numbered.
    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', '- {page} -'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'id', 'roman'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'pages', '2-'
    EXEC sp_OAMethod @json2, 'UpdateInt', @success OUT, 'pageNumber.start', 1
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'pageNumber.style', 'romanLower'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'position.align', 'bottom'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'font.size', '10'
    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 'roman pages: ' + @sTmp0

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/four_pages_numbered.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:

    --  footer pages: 1-4
    --  roman pages: 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