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

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

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

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

    ;  The same watermark stored four different ways, each in its own copy of the document.
    pdf.i = CkPdf::ckCreate()
    If pdf.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/two_page_memo.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

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

    ;  detect answers whether anything matching the filter is in the document.  Finding nothing is
    ;  still success: "found" is simply false.
    CkJsonObject::ckUpdateString(filter,"id","draft")
    success = CkPdf::ckWatermark(pdf,"detect",filter,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        ProcedureReturn
    EndIf

    Debug "artifact: found = " + Str(CkJsonObject::ckBoolOf(jsonOut,"found"))

    ;  An annotation: always drawn on top, and the one approach a signed PDF accepts.
    pdf2.i = CkPdf::ckCreate()
    If pdf2.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf2,"qa_data/pdf/two_page_memo.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"approach","annotation")
    success = CkPdf::ckWatermark(pdf2,"add",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf2)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        ProcedureReturn
    EndIf

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

    Debug "annotation: found = " + Str(CkJsonObject::ckBoolOf(jsonOut,"found"))

    ;  A layer, which the viewer's layers panel can switch off.
    pdf3.i = CkPdf::ckCreate()
    If pdf3.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf3,"qa_data/pdf/two_page_memo.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf3)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        CkPdf::ckDispose(pdf3)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"approach","layer")
    CkJsonObject::ckUpdateString(json,"layer.name","Draft marking")
    success = CkPdf::ckWatermark(pdf3,"add",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf3)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        CkPdf::ckDispose(pdf3)
        ProcedureReturn
    EndIf

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

    Debug "layer: found = " + Str(CkJsonObject::ckBoolOf(jsonOut,"found"))

    ;  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.
    pdf4.i = CkPdf::ckCreate()
    If pdf4.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPdf::ckLoadFile(pdf4,"qa_data/pdf/two_page_memo.pdf")
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf4)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        CkPdf::ckDispose(pdf3)
        CkPdf::ckDispose(pdf4)
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"approach","content")
    success = CkPdf::ckWatermark(pdf4,"add",json,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf4)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(filter)
        CkPdf::ckDispose(pdf)
        CkPdf::ckDispose(pdf2)
        CkPdf::ckDispose(pdf3)
        CkPdf::ckDispose(pdf4)
        ProcedureReturn
    EndIf

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

    Debug "content: found = " + Str(CkJsonObject::ckBoolOf(jsonOut,"found"))

    ;  Sample output:

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


    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)
    CkJsonObject::ckDispose(filter)
    CkPdf::ckDispose(pdf)
    CkPdf::ckDispose(pdf2)
    CkPdf::ckDispose(pdf3)
    CkPdf::ckDispose(pdf4)


    ProcedureReturn
EndProcedure