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

Lua
local chilkat = require("chilkat")

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

local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/acrobat_watermark.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

local filter = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()

-- What is in there?  A watermark from another producer is given an id of the form ext1, ext2, ...
success = pdf:watermark("list", filter, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("watermarks: " .. jsonOut:int_of("count"))
jsonOut.i = 0
print("first id: " .. jsonOut:string_of("watermarks[i].id") .. " text: " .. jsonOut:string_of("watermarks[i].text"))

-- Add one of our own beside it, so that the filters below have two to choose between.
local json = chilkat.JsonObject.new()
json:update_string("type", "text")
json:update_string("text", "SUPERSEDED")
json:update_string("id", "superseded")
success = pdf:watermark("add", json, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

-- Remove only what Acrobat put there, leaving ours in place.
filter:update_string("source", "acrobat")
success = pdf:watermark("remove", filter, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("removed (acrobat): " .. jsonOut:int_of("removed"))

local empty = chilkat.JsonObject.new()
success = pdf:watermark("detect", empty, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("left in the document: " .. jsonOut:int_of("count"))

-- An empty filter is refused for remove: say all explicitly to strip everything.
success = pdf:watermark("remove", empty, jsonOut)
if not success then
    print("An empty filter was refused, as it should be.")
end

empty:update_bool("all", true)
success = pdf:watermark("remove", empty, jsonOut)
if not success then
    print(pdf.last_error_text)
    return
end

print("removed (all): " .. jsonOut:int_of("removed"))

-- The changes are in memory until the file is written.
success = pdf:write_file("qa_output/acrobat_unwatermarked.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

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