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

Remove a Watermark from a PDF

See more PDF Watermark Examples

Demonstrates remove, which deletes the watermarks matching the filter and reports how many went. The filter is the same one list takes, so the usual sequence is to list what is there and then remove by id, by source, or by text. An empty filter is refused unless "all" is true, so that a whole document cannot be stripped by an empty JSON object. Removing finds nothing is not an error: "removed" is simply 0. An annotation is deleted, the marked content of an artifact and any form it alone used are deleted, and the content of a layer goes with the layer once nothing else uses it. A watermark added with the content approach is part of the page and is never removable.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vFilter
    Handle hoFilter
    Variant vJsonOut
    Handle hoJsonOut
    Variant vJson
    Handle hoJson
    Variant vEmpty
    Handle hoEmpty
    String sTemp1
    String sTemp2
    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 acrobat_watermark.pdf

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoFilter
    If (Not(IsComObjectCreated(hoFilter))) Begin
        Send CreateComObject of hoFilter
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "list" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "count" To iTemp1
    Showln "watermarks: " iTemp1
    Set ComI Of hoJsonOut To 0
    Get ComStringOf Of hoJsonOut "watermarks[i].id" To sTemp1
    Get ComStringOf Of hoJsonOut "watermarks[i].text" To sTemp2
    Showln "first id: " sTemp1 " text: " sTemp2

    //  Add one of our own beside it, so that the filters below have two to choose between.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "type" "text" To iSuccess
    Get ComUpdateString Of hoJson "text" "SUPERSEDED" To iSuccess
    Get ComUpdateString Of hoJson "id" "superseded" 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

    //  Remove only what Acrobat put there, leaving ours in place.
    Get ComUpdateString Of hoFilter "source" "acrobat" To iSuccess
    Get pvComObject of hoFilter to vFilter
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "remove" vFilter vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "removed" To iTemp1
    Showln "removed (acrobat): " iTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoEmpty
    If (Not(IsComObjectCreated(hoEmpty))) Begin
        Send CreateComObject of hoEmpty
    End
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "detect" vEmpty vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "count" To iTemp1
    Showln "left in the document: " iTemp1

    //  An empty filter is refused for remove: say all explicitly to strip everything.
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "remove" vEmpty vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Showln "An empty filter was refused, as it should be."
    End

    Get ComUpdateBool Of hoEmpty "all" True To iSuccess
    Get pvComObject of hoEmpty to vEmpty
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComWatermark Of hoPdf "remove" vEmpty vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComIntOf Of hoJsonOut "removed" To iTemp1
    Showln "removed (all): " iTemp1

    //  The changes are in memory until the file is written.
    Get ComWriteFile Of hoPdf "qa_output/acrobat_unwatermarked.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Sample output:

    //  watermarks: 1
    //  first id: ext1 text: Sample
    //  removed (acrobat): 1
    //  left in the document: 1
    //  An empty filter was refused, as it should be.
    //  removed (all): 1


End_Procedure