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

How a PDF Watermark Is Stored: artifact, annotation, layer or content

See more PDF Watermark Examples

Demonstrates the approach member, which decides how the watermark is stored and therefore what can be done with it afterwards. artifact (the default) is marked content that viewers draw, text extraction leaves out and Chilkat can remove later. annotation is a Watermark annotation, always on top, and the only approach allowed on a signed PDF while PreserveSignatures is true. layer is an optional-content layer the viewer can hide and that can be made print-only. content draws straight into the page with no marker: visible everywhere and the tamper-resistant choice, but by design it can never be listed or removed again.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    Variant vFilter
    Handle hoFilter
    Handle hoPdf
    Handle hoPdf2
3    Handle hoPdf3
    Handle hoPdf4
    String sTemp1
    Boolean bTemp1

    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 two_page_memo.pdf

    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 Create (RefClass(cComChilkatJsonObject)) To hoFilter
    If (Not(IsComObjectCreated(hoFilter))) Begin
        Send CreateComObject of hoFilter
    End

    //  The same watermark stored four different ways, each in its own copy of the document.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get ComLoadFile Of hoPdf "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComUpdateString Of hoJson "type" "text" To iSuccess
    Get ComUpdateString Of hoJson "text" "DRAFT" To iSuccess
    Get ComUpdateString Of hoJson "id" "draft" To iSuccess
    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
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  detect answers whether anything matching the filter is in the document.  Finding nothing is
    //  still success: "found" is simply false.
    Get ComUpdateString Of hoFilter "id" "draft" To iSuccess
    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "detect" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "found" To bTemp1
    Showln "artifact: found = " bTemp1

    //  An annotation: always drawn on top, and the one approach a signed PDF accepts.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf2
    If (Not(IsComObjectCreated(hoPdf2))) Begin
        Send CreateComObject of hoPdf2
    End
    Get ComLoadFile Of hoPdf2 "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComUpdateString Of hoJson "approach" "annotation" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf2 "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf2 "detect" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf2 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "found" To bTemp1
    Showln "annotation: found = " bTemp1

    //  A layer, which the viewer's layers panel can switch off.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf3
    If (Not(IsComObjectCreated(hoPdf3))) Begin
        Send CreateComObject of hoPdf3
    End
    Get ComLoadFile Of hoPdf3 "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf3 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComUpdateString Of hoJson "approach" "layer" To iSuccess
    Get ComUpdateString Of hoJson "layer.name" "Draft marking" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf3 "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf3 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf3 "detect" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf3 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "found" To bTemp1
    Showln "layer: found = " bTemp1

    //  Drawn into the page content with no marker.  There is nothing to find afterwards, which is the
    //  point of it: it cannot be stripped out by a tool that removes watermarks.
    Get Create (RefClass(cComChilkatPdf)) To hoPdf4
    If (Not(IsComObjectCreated(hoPdf4))) Begin
        Send CreateComObject of hoPdf4
    End
    Get ComLoadFile Of hoPdf4 "qa_data/pdf/two_page_memo.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf4 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComUpdateString Of hoJson "approach" "content" To iSuccess
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf4 "add" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf4 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf4 "detect" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf4 To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComBoolOf Of hoJsonOut "found" To bTemp1
    Showln "content: found = " bTemp1

    //  Sample output:

    //  artifact: found = True
    //  annotation: found = True
    //  layer: found = True
    //  content: found = False


End_Procedure