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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Filter
oleobject loo_JsonOut
oleobject loo_Json
oleobject loo_Empty

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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/acrobat_watermark.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    return
end if

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

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

//  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
li_Success = loo_Pdf.Watermark("list",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Filter
    destroy loo_JsonOut
    return
end if

Write-Debug "watermarks: " + string(loo_JsonOut.IntOf("count"))
loo_JsonOut.I = 0
Write-Debug "first id: " + loo_JsonOut.StringOf("watermarks[i].id") + " text: " + loo_JsonOut.StringOf("watermarks[i].text")

//  Add one of our own beside it, so that the filters below have two to choose between.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

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

//  Remove only what Acrobat put there, leaving ours in place.
loo_Filter.UpdateString("source","acrobat")
li_Success = loo_Pdf.Watermark("remove",loo_Filter,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Filter
    destroy loo_JsonOut
    destroy loo_Json
    return
end if

Write-Debug "removed (acrobat): " + string(loo_JsonOut.IntOf("removed"))

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

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

Write-Debug "left in the document: " + string(loo_JsonOut.IntOf("count"))

//  An empty filter is refused for remove: say all explicitly to strip everything.
li_Success = loo_Pdf.Watermark("remove",loo_Empty,loo_JsonOut)
if li_Success = 0 then
    Write-Debug "An empty filter was refused, as it should be."
end if

loo_Empty.UpdateBool("all",1)
li_Success = loo_Pdf.Watermark("remove",loo_Empty,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Filter
    destroy loo_JsonOut
    destroy loo_Json
    destroy loo_Empty
    return
end if

Write-Debug "removed (all): " + string(loo_JsonOut.IntOf("removed"))

//  The changes are in memory until the file is written.
li_Success = loo_Pdf.WriteFile("qa_output/acrobat_unwatermarked.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Filter
    destroy loo_JsonOut
    destroy loo_Json
    destroy loo_Empty
    return
end if

//  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


destroy loo_Pdf
destroy loo_Filter
destroy loo_JsonOut
destroy loo_Json
destroy loo_Empty