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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Filter
oleobject loo_Pdf
oleobject loo_Pdf2
oleobject loo_Pdf3
oleobject loo_Pdf4

li_Success = 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

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Json
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

loo_Filter = create oleobject
li_rc = loo_Filter.ConnectToNewObject("Chilkat.JsonObject")

//  The same watermark stored four different ways, each in its own copy of the document.
loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    return
end if

loo_Json.UpdateString("type","text")
loo_Json.UpdateString("text","DRAFT")
loo_Json.UpdateString("id","draft")
loo_Json.UpdateString("approach","artifact")
li_Success = loo_Pdf.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    return
end if

//  detect answers whether anything matching the filter is in the document.  Finding nothing is
//  still success: "found" is simply false.
loo_Filter.UpdateString("id","draft")
li_Success = loo_Pdf.Watermark("detect",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    return
end if

Write-Debug "artifact: found = " + string(loo_JsonOut.BoolOf("found"))

//  An annotation: always drawn on top, and the one approach a signed PDF accepts.
loo_Pdf2 = create oleobject
li_rc = loo_Pdf2.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf2.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    return
end if

loo_Json.UpdateString("approach","annotation")
li_Success = loo_Pdf2.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    return
end if

li_Success = loo_Pdf2.Watermark("detect",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf2.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    return
end if

Write-Debug "annotation: found = " + string(loo_JsonOut.BoolOf("found"))

//  A layer, which the viewer's layers panel can switch off.
loo_Pdf3 = create oleobject
li_rc = loo_Pdf3.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf3.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

loo_Json.UpdateString("approach","layer")
loo_Json.UpdateString("layer.name","Draft marking")
li_Success = loo_Pdf3.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

li_Success = loo_Pdf3.Watermark("detect",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf3.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    return
end if

Write-Debug "layer: found = " + string(loo_JsonOut.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.
loo_Pdf4 = create oleobject
li_rc = loo_Pdf4.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Pdf4.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf4.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    destroy loo_Pdf4
    return
end if

loo_Json.UpdateString("approach","content")
li_Success = loo_Pdf4.Watermark("add",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf4.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    destroy loo_Pdf4
    return
end if

li_Success = loo_Pdf4.Watermark("detect",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf4.LastErrorText
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Filter
    destroy loo_Pdf
    destroy loo_Pdf2
    destroy loo_Pdf3
    destroy loo_Pdf4
    return
end if

Write-Debug "content: found = " + string(loo_JsonOut.BoolOf("found"))

//  Sample output:

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


destroy loo_Json
destroy loo_JsonOut
destroy loo_Filter
destroy loo_Pdf
destroy loo_Pdf2
destroy loo_Pdf3
destroy loo_Pdf4