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

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oJsonOut
LOCAL oFilter
LOCAL oPdf
LOCAL oPdf2
LOCAL oPdf3
LOCAL oPdf4

nSuccess := 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

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
oFilter := CreateObject("Chilkat.JsonObject")

//  The same watermark stored four different ways, each in its own copy of the document.
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    RETURN
ENDIF

oJson:UpdateString("type", "text")
oJson:UpdateString("text", "DRAFT")
oJson:UpdateString("id", "draft")
oJson:UpdateString("approach", "artifact")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    RETURN
ENDIF

//  detect answers whether anything matching the filter is in the document.  Finding nothing is
//  still success: "found" is simply false.
oFilter:UpdateString("id", "draft")
nSuccess := oPdf:Watermark("detect", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    RETURN
ENDIF

? "artifact: found = " + Str(oJsonOut:BoolOf("found"))

//  An annotation: always drawn on top, and the one approach a signed PDF accepts.
oPdf2 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf2:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

oJson:UpdateString("approach", "annotation")
nSuccess := oPdf2:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

nSuccess := oPdf2:Watermark("detect", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf2:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    RETURN
ENDIF

? "annotation: found = " + Str(oJsonOut:BoolOf("found"))

//  A layer, which the viewer's layers panel can switch off.
oPdf3 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf3:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

oJson:UpdateString("approach", "layer")
oJson:UpdateString("layer.name", "Draft marking")
nSuccess := oPdf3:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

nSuccess := oPdf3:Watermark("detect", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf3:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    RETURN
ENDIF

? "layer: found = " + Str(oJsonOut:BoolOf("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.
oPdf4 := CreateObject("Chilkat.Pdf")
nSuccess := oPdf4:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
    ? oPdf4:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    oPdf4:destroy()
    RETURN
ENDIF

oJson:UpdateString("approach", "content")
nSuccess := oPdf4:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf4:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    oPdf4:destroy()
    RETURN
ENDIF

nSuccess := oPdf4:Watermark("detect", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf4:LastErrorText
    oJson:destroy()
    oJsonOut:destroy()
    oFilter:destroy()
    oPdf:destroy()
    oPdf2:destroy()
    oPdf3:destroy()
    oPdf4:destroy()
    RETURN
ENDIF

? "content: found = " + Str(oJsonOut:BoolOf("found"))

//  Sample output:

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

oJson:destroy()
oJsonOut:destroy()
oFilter:destroy()
oPdf:destroy()
oPdf2:destroy()
oPdf3:destroy()
oPdf4:destroy()