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

Find Out Whether a PDF Has a Watermark

See more PDF Watermark Examples

Demonstrates detect, which answers yes or no, and list, which enumerates what is there. Both recognize the watermarks Chilkat writes and the standard forms other tools write: Watermark annotations from any producer, marked content tagged as a Watermark or Background artifact, Acrobat watermarks and backgrounds, and optional-content layers whose name says watermark. Acrobat headers and footers are not watermarks and are ignored. A watermark from another producer has no id of its own, so Chilkat assigns one (ext1, ext2, ...) that stays valid until the PDF is changed, which is what makes a list followed by a remove by id work. Properties that cannot be read back are left out rather than guessed.

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

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

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

    ;  An empty filter means every watermark in the document.
    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

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

    Debug "found: " + Str(CkJsonObject::ckBoolOf(jsonOut,"found")) + " count: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))

    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

    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonOut,"watermarks")
    While i < count_i
        CkJsonObject::setCkI(jsonOut, i)
        Debug "id: " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].id")
        Debug "   source:    " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].source")
        Debug "   approach:  " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].approach")
        Debug "   type:      " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].type")
        Debug "   text:      " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].text")
        Debug "   pages:     " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].pages")
        Debug "   zOrder:    " + CkJsonObject::ckStringOf(jsonOut,"watermarks[i].zOrder")
        Debug "   removable: " + Str(CkJsonObject::ckBoolOf(jsonOut,"watermarks[i].removable"))
        i = i + 1
    Wend

    ;  The filter narrows what is reported: by producer (chilkat, acrobat or other), by approach, by
    ;  pages, by an exact id or label, or by text with textContains.
    bySource.i = CkJsonObject::ckCreate()
    If bySource.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(bySource,"source","acrobat")
    success = CkPdf::ckWatermark(pdf,"detect",bySource,jsonOut)
    If success = 0
        Debug CkPdf::ckLastErrorText(pdf)
        CkPdf::ckDispose(pdf)
        CkJsonObject::ckDispose(filter)
        CkJsonObject::ckDispose(jsonOut)
        CkJsonObject::ckDispose(bySource)
        ProcedureReturn
    EndIf

    Debug "made by Acrobat: " + Str(CkJsonObject::ckIntOf(jsonOut,"count"))

    ;  Sample output:

    ;  found: True count: 2
    ;  id: ext1
    ;     source:    acrobat
    ;     approach:  artifact
    ;     type:      image
    ;     text:
    ;     pages:     1
    ;     zOrder:    front
    ;     removable: True
    ;  id: ext2
    ;     source:    acrobat
    ;     approach:  artifact
    ;     type:      text
    ;     text:      sample
    ;     pages:     1
    ;     zOrder:    front
    ;     removable: True
    ;  made by Acrobat: 2


    CkPdf::ckDispose(pdf)
    CkJsonObject::ckDispose(filter)
    CkJsonObject::ckDispose(jsonOut)
    CkJsonObject::ckDispose(bySource)


    ProcedureReturn
EndProcedure