Lua Requires Chilkat v11.7.0+
Lua
Find Out Whether a PDF Has a Watermark
See more PDF Watermark Examples
Demonstrates detect, which answers yes or no, and list, which enumerates what is there. Both recognize the watermarks Chilkat writes and the standard forms other tools write: Watermark annotations from any producer, marked content tagged as a Watermark or Background artifact, Acrobat watermarks and backgrounds, and optional-content layers whose name says watermark. Acrobat headers and footers are not watermarks and are ignored. A watermark from another producer has no id of its own, so Chilkat assigns one (ext1, ext2, ...) that stays valid until the PDF is changed, which is what makes a list followed by a remove by id work. Properties that cannot be read back are left out rather than guessed.Chilkat Lua Downloads
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_two_watermarks.pdf
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/acrobat_two_watermarks.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- An empty filter means every watermark in the document.
local filter = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
success = pdf:watermark("detect", filter, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("found: " .. tostring(jsonOut:bool_of("found")) .. " count: " .. jsonOut:int_of("count"))
success = pdf:watermark("list", filter, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
local i = 0
local count_i = jsonOut:size_of_array("watermarks")
while i < count_i do
jsonOut.i = i
print("id: " .. jsonOut:string_of("watermarks[i].id"))
print(" source: " .. jsonOut:string_of("watermarks[i].source"))
print(" approach: " .. jsonOut:string_of("watermarks[i].approach"))
print(" type: " .. jsonOut:string_of("watermarks[i].type"))
print(" text: " .. jsonOut:string_of("watermarks[i].text"))
print(" pages: " .. jsonOut:string_of("watermarks[i].pages"))
print(" zOrder: " .. jsonOut:string_of("watermarks[i].zOrder"))
print(" removable: " .. tostring(jsonOut:bool_of("watermarks[i].removable")))
i = i + 1
end
-- The filter narrows what is reported: by producer (chilkat, acrobat or other), by approach, by
-- pages, by an exact id or label, or by text with textContains.
local bySource = chilkat.JsonObject.new()
bySource:update_string("source", "acrobat")
success = pdf:watermark("detect", bySource, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("made by Acrobat: " .. jsonOut:int_of("count"))
-- Sample output:
-- found: True count: 2
-- id: ext1
-- source: acrobat
-- approach: artifact
-- type: image
-- text:
-- pages: 1
-- zOrder: front
-- removable: True
-- id: ext2
-- source: acrobat
-- approach: artifact
-- type: text
-- text: sample
-- pages: 1
-- zOrder: front
-- removable: True
-- made by Acrobat: 2