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

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oFilter
LOCAL oJsonOut
LOCAL oJson
LOCAL oEmpty

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

oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/acrobat_watermark.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

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

//  What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
nSuccess := oPdf:Watermark("list", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "watermarks: " + Str(oJsonOut:IntOf("count"))
oJsonOut:I := 0
? "first id: " + oJsonOut:StringOf("watermarks[i].id") + " text: " + oJsonOut:StringOf("watermarks[i].text")

//  Add one of our own beside it, so that the filters below have two to choose between.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("type", "text")
oJson:UpdateString("text", "SUPERSEDED")
oJson:UpdateString("id", "superseded")
nSuccess := oPdf:Watermark("add", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    oJson:destroy()
    RETURN
ENDIF

//  Remove only what Acrobat put there, leaving ours in place.
oFilter:UpdateString("source", "acrobat")
nSuccess := oPdf:Watermark("remove", oFilter, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    oJson:destroy()
    RETURN
ENDIF

? "removed (acrobat): " + Str(oJsonOut:IntOf("removed"))

oEmpty := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Watermark("detect", oEmpty, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    oJson:destroy()
    oEmpty:destroy()
    RETURN
ENDIF

? "left in the document: " + Str(oJsonOut:IntOf("count"))

//  An empty filter is refused for remove: say all explicitly to strip everything.
nSuccess := oPdf:Watermark("remove", oEmpty, oJsonOut)
IF (nSuccess == 0)
    ? "An empty filter was refused, as it should be."
ENDIF

oEmpty:UpdateBool("all", 1)
nSuccess := oPdf:Watermark("remove", oEmpty, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    oJson:destroy()
    oEmpty:destroy()
    RETURN
ENDIF

? "removed (all): " + Str(oJsonOut:IntOf("removed"))

//  The changes are in memory until the file is written.
nSuccess := oPdf:WriteFile("qa_output/acrobat_unwatermarked.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oFilter:destroy()
    oJsonOut:destroy()
    oJson:destroy()
    oEmpty:destroy()
    RETURN
ENDIF

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

oPdf:destroy()
oFilter:destroy()
oJsonOut:destroy()
oJson:destroy()
oEmpty:destroy()