Sample code for 30+ languages & platforms
DataFlex 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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Handle hoCheck
    Variant vSigInfo
    Handle hoSigInfo
    Boolean iVerified
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/invoice_signed.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumSignatures Of hoPdf To iTemp1
    Showln "signatures: " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End
    Get ComUpdateString Of hoJson "type" "text" To iSuccess
    Get ComUpdateString Of hoJson "text" "PAID" To iSuccess
    Get ComUpdateString Of hoJson "id" "paid" To iSuccess
    Get ComUpdateString Of hoJson "color" "#2E7D32" To iSuccess
    Get ComUpdateString Of hoJson "position.align" "topRight" To iSuccess
    Get ComUpdateNumber Of hoJson "position.margin" "36" To iSuccess

    //  An artifact watermark changes the page content, so it is refused while the signatures matter.
    Get ComUpdateString Of hoJson "approach" "artifact" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Showln "The artifact approach was refused on a signed PDF, as it should be."
    End

    //  An annotation is added without touching the signed bytes.
    Get ComUpdateString Of hoJson "approach" "annotation" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "pages" To sTemp1
    Showln "annotation added, pages: " sTemp1

    Get ComWriteFile Of hoPdf "qa_output/invoice_paid.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  The signature still verifies: the original bytes are unchanged and the annotation was appended.
    Get Create (RefClass(cComChilkatPdf)) To hoCheck
    If (Not(IsComObjectCreated(hoCheck))) Begin
        Send CreateComObject of hoCheck
    End
    Get ComLoadFile Of hoCheck "qa_output/invoice_paid.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCheck To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoSigInfo
    If (Not(IsComObjectCreated(hoSigInfo))) Begin
        Send CreateComObject of hoSigInfo
    End
    Get pvComObject of hoSigInfo to vSigInfo
    Get ComVerifySignature Of hoCheck 0 vSigInfo To iVerified
    Showln "signature still verifies: " iVerified

    //  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


End_Procedure