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

Watermark a Signed PDF Without Breaking the Signature

See more PDF Watermark Examples

Demonstrates the one approach a signed PDF accepts while its signatures are being preserved. Adding a watermark to the page content rewrites the page, which invalidates every existing signature, so with PreserveSignatures true (the default) only the annotation approach is allowed: an annotation is added as an incremental update that leaves the signed bytes untouched. Removing anything is refused for the same reason. Set PreserveSignatures to false to allow the other approaches, and the signatures in the file stop verifying.

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 invoice_signed.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/invoice_signed.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, 'NumSignatures', @iTmp0 OUT
    PRINT 'signatures: ' + @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', 'PAID'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'id', 'paid'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'color', '#2E7D32'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'position.align', 'topRight'
    EXEC sp_OAMethod @json, 'UpdateNumber', @success OUT, 'position.margin', '36'

    --  An artifact watermark changes the page content, so it is refused while the signatures matter.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'approach', 'artifact'
    EXEC sp_OAMethod @pdf, 'Watermark', @success OUT, 'add', @json, @jsonOut
    IF @success = 0
      BEGIN

        PRINT 'The artifact approach was refused on a signed PDF, as it should be.'
      END

    --  An annotation is added without touching the signed bytes.
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'approach', 'annotation'
    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 'annotation added, pages: ' + @sTmp0

    EXEC sp_OAMethod @pdf, 'WriteFile', @success OUT, 'qa_output/invoice_paid.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 signature still verifies: the original bytes are unchanged and the annotation was appended.
    DECLARE @check int
    EXEC @hr = sp_OACreate 'Chilkat.Pdf', @check OUT

    EXEC sp_OAMethod @check, 'LoadFile', @success OUT, 'qa_output/invoice_paid.pdf'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @check, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pdf
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @jsonOut
        EXEC @hr = sp_OADestroy @check
        RETURN
      END
    DECLARE @sigInfo int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @sigInfo OUT

    DECLARE @verified int
    EXEC sp_OAMethod @check, 'VerifySignature', @verified OUT, 0, @sigInfo

    PRINT 'signature still verifies: ' + @verified

    --  Sample output:

    --  signatures: 1
    --  The artifact approach was refused on a signed PDF, as it should be.
    --  annotation added, pages: 1
    --  signature still verifies: True

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


END
GO