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

A PDF Watermark That Only Appears When Printed

See more PDF Watermark Examples

Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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 two_page_memo.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/two_page_memo.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', 'UNCONTROLLED WHEN PRINTED'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'printonly'

    --  As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'approach', 'layer'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layer.name', 'Print marking'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layer.view', 'off'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'layer.print', 'on'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'layer.locked', 1

    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', '70'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'opacity', '0.3'

    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, 'id'

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

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

    --  The same thing as an annotation instead.
    DECLARE @pdf2 int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @pdf2 OUT

    EXEC sp_OAMethod @pdf2, 'LoadFile', @success OUT, 'qa_data/pdf/two_page_memo.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', 'UNCONTROLLED WHEN PRINTED'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'id', 'printonly2'
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'approach', 'annotation'
    EXEC sp_OAMethod @json2, 'UpdateBool', @success OUT, 'annotation.view', 0
    EXEC sp_OAMethod @json2, 'UpdateBool', @success OUT, 'annotation.print', 1
    EXEC sp_OAMethod @json2, 'UpdateBool', @success OUT, 'annotation.fixedPrint', 1
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'rotation', 'diagonal'
    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, 'id'
    PRINT 'annotation id: ' + @sTmp0

    --  Sample output:

    --  layer id: printonly pages: 1-2
    --  annotation id: printonly2

    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