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

PureBasic
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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 acrobat_watermark.pdf

    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/acrobat_watermark.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    filter.i = CkJsonObject::ckCreate()
    If filter.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
    success = CkPdf::ckWatermark(pdf,"list",filter,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    Debug "watermarks: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))
    CkJsonObject::setCkI(jsonOut, 0)
    Debug "first id: " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].id") + " text: " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].text")

    ;  Add one of our own beside it, so that the filters below have two to choose between.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"type","text")
    CkJsonObject::ckUpdateString(json,"text","SUPERSEDED")
    CkJsonObject::ckUpdateString(json,"id","superseded")
    success = CkPdf::ckWatermark(pdf,"add",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ;  Remove only what Acrobat put there, leaving ours in place.
    CkJsonObject::ckUpdateString(filter,"source","acrobat")
    success = CkPdf::ckWatermark(pdf,"remove",filter,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug "removed (acrobat): " + Str(CkJsonObject::ckIntOf(jsonOut,"removed"))

    empty.i = CkJsonObject::ckCreate()
    If empty.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckWatermark(pdf,"detect",empty,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(empty)
        ProcedureReturn
    EndIf

    Debug "left in the document: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))

    ;  An empty filter is refused for remove: say all explicitly to strip everything.
    success = CkPdf::ckWatermark(pdf,"remove",empty,jsonOut)
    If success = 0
        Debug "An empty filter was refused, as it should be."
    EndIf

    CkJsonObject::ckUpdateBool(empty,"all",1)
    success = CkPdf::ckWatermark(pdf,"remove",empty,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(empty)
        ProcedureReturn
    EndIf

    Debug "removed (all): " + Str(CkJsonObject::ckIntOf(jsonOut,"removed"))

    ;  The changes are in memory until the file is written.
    success = CkPdf::ckWriteFile(pdf,"qa_output/acrobat_unwatermarked.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(empty)
        ProcedureReturn
    EndIf

    ;  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


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(filter)
    CkJsonObject::ckDispose(jsonOut)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(empty)


    ProcedureReturn
EndProcedure