Lua Requires Chilkat v11.7.0+
Lua
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 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 two_page_memo.pdf
local json = chilkat.JsonObject.new()
local jsonOut = chilkat.JsonObject.new()
local filter = chilkat.JsonObject.new()
-- The same watermark stored four different ways, each in its own copy of the document.
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf.last_error_text)
return
end
json:update_string("type", "text")
json:update_string("text", "DRAFT")
json:update_string("id", "draft")
json:update_string("approach", "artifact")
success = pdf:watermark("add", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
-- detect answers whether anything matching the filter is in the document. Finding nothing is
-- still success: "found" is simply false.
filter:update_string("id", "draft")
success = pdf:watermark("detect", filter, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("artifact: found = " .. tostring(jsonOut:bool_of("found")))
-- An annotation: always drawn on top, and the one approach a signed PDF accepts.
local pdf2 = chilkat.Pdf.new()
success = pdf2:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf2.last_error_text)
return
end
json:update_string("approach", "annotation")
success = pdf2:watermark("add", json, jsonOut)
if not success then
print(pdf2.last_error_text)
return
end
success = pdf2:watermark("detect", filter, jsonOut)
if not success then
print(pdf2.last_error_text)
return
end
print("annotation: found = " .. tostring(jsonOut:bool_of("found")))
-- A layer, which the viewer's layers panel can switch off.
local pdf3 = chilkat.Pdf.new()
success = pdf3:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf3.last_error_text)
return
end
json:update_string("approach", "layer")
json:update_string("layer.name", "Draft marking")
success = pdf3:watermark("add", json, jsonOut)
if not success then
print(pdf3.last_error_text)
return
end
success = pdf3:watermark("detect", filter, jsonOut)
if not success then
print(pdf3.last_error_text)
return
end
print("layer: found = " .. tostring(jsonOut:bool_of("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.
local pdf4 = chilkat.Pdf.new()
success = pdf4:load_file("qa_data/pdf/two_page_memo.pdf")
if not success then
print(pdf4.last_error_text)
return
end
json:update_string("approach", "content")
success = pdf4:watermark("add", json, jsonOut)
if not success then
print(pdf4.last_error_text)
return
end
success = pdf4:watermark("detect", filter, jsonOut)
if not success then
print(pdf4.last_error_text)
return
end
print("content: found = " .. tostring(jsonOut:bool_of("found")))
-- Sample output:
-- artifact: found = True
-- annotation: found = True
-- layer: found = True
-- content: found = False