Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.7.0+

A PDF Watermark That Only Appears When Printed

See more PDF Watermark Examples

Demonstrates a watermark that is invisible on screen and appears on paper, which is what a "printed copies are uncontrolled" marking needs. Two approaches can do it: an optional-content layer with layer.view off and layer.print on, which also gives the reader an entry in the layers panel of the viewer (layer.locked stops them switching it), and a Watermark annotation with annotation.view false and annotation.print true. annotation.fixedPrint keeps the printed size and position the same whatever paper it goes on.

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

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

local json = chilkat.JsonObject.new()
json:update_string("type", "text")
json:update_string("text", "UNCONTROLLED WHEN PRINTED")
json:update_string("id", "printonly")

-- As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
json:update_string("approach", "layer")
json:update_string("layer.name", "Print marking")
json:update_string("layer.view", "off")
json:update_string("layer.print", "on")
json:update_bool("layer.locked", true)

json:update_string("rotation", "diagonal")
json:update_string("scale.fit", "page")
json:update_number("scale.fitPercent", "70")
json:update_number("opacity", "0.3")

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

print("layer id: " .. jsonOut:string_of("id") .. " pages: " .. jsonOut:string_of("pages"))

success = pdf:write_file("qa_output/memo_print_only.pdf")
if not success then
    print(pdf.last_error_text)
    return
end

-- The same thing as an annotation instead.
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

local json2 = chilkat.JsonObject.new()
json2:update_string("type", "text")
json2:update_string("text", "UNCONTROLLED WHEN PRINTED")
json2:update_string("id", "printonly2")
json2:update_string("approach", "annotation")
json2:update_bool("annotation.view", false)
json2:update_bool("annotation.print", true)
json2:update_bool("annotation.fixedPrint", true)
json2:update_string("rotation", "diagonal")
success = pdf2:watermark("add", json2, jsonOut)
if not success then
    print(pdf2.last_error_text)
    return
end

print("annotation id: " .. jsonOut:string_of("id"))

-- Sample output:

-- layer id: printonly pages: 1-2
-- annotation id: printonly2