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

Place and Size a PDF Watermark: Alignment, Margins and Fitting

See more PDF Watermark Examples

Demonstrates where the stamp goes and how large it is drawn. position.align picks one of nine spots, position.margin insets it from the page edges, and position.offsetX and offsetY nudge it from there. For the size, scale.factor multiplies the natural size, scale.fit fits the rotated stamp inside the margin box (none, page, width or height) with scale.fitPercent saying how much of that box to fill, and scale.maxWidth and maxHeight cap the result. The units member applies to all of the measurements except font size, which is always in points.

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

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

    --  A banner across the top, inset three quarters of an inch, filling 60% of the width.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'text'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'text', 'REVIEW COPY'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'banner'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'units', 'in'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'position.align', 'top'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'position.margin', '0.75'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'scale.fit', 'width'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'scale.fitPercent', '60'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'color', '#1F4E79'
    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 'banner pages: ' + @sTmp0

    --  A second stamp in the bottom-left corner, nudged a quarter inch to the right and up, with its
    --  size capped rather than fitted.
    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', 'Not for distribution'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'id', 'footnote'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'units', 'in'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'position.align', 'bottomLeft'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'position.margin', '0.5'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'position.offsetX', '0.25'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'position.offsetY', '-0.25'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'font.size', '9'
    EXEC sp_OAMethod @json2, 'UpdateNumber', @success OUT, 'scale.maxWidth', '3.0'
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'add', @json2, @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
        EXEC @hr = sp_OADestroy @json2
        RETURN
      END

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

    --  Each call adds one watermark, so a banner plus a note is two calls.
    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/four_pages_placed.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 @json2
        RETURN
      END

    --  Sample output:

    --  banner pages: 1-4
    --  footnote pages: 1-4

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


END
GO