Lua Requires Chilkat v11.7.0+
Lua
Page Numbers, Dates and the File Name in a PDF Watermark
See more PDF Watermark Examples
Demonstrates the placeholders a text watermark understands, each replaced per page as the watermark is added: {page} is the page number, {pages} the page count, {date:FORMAT} the current local date and time, and {filename} the name of the file that was loaded. Two braces give a literal brace. pageNumber.start sets what {page} is on the first page, which is how a numbering that ignores a cover is produced, and pageNumber.style formats both {page} and {pages} as decimal, romanUpper, romanLower, letterUpper or letterLower.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 four_pages.pdf
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/four_pages.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", "Page {page} of {pages} - printed {date:yyyy-MM-dd HH:mm} - {filename}")
json:update_string("id", "pagefoot")
json:update_string("position.align", "bottom")
json:update_number("position.margin", "24")
json:update_number("font.size", "9")
json:update_string("color", "#505050")
local jsonOut = chilkat.JsonObject.new()
success = pdf:watermark("add", json, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("footer pages: " .. jsonOut:string_of("pages"))
-- Roman numerals that start at i on page 2, so that a cover page is not numbered.
local pdf2 = chilkat.Pdf.new()
success = pdf2:load_file("qa_data/pdf/four_pages.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", "- {page} -")
json2:update_string("id", "roman")
json2:update_string("pages", "2-")
json2:update_int("pageNumber.start", 1)
json2:update_string("pageNumber.style", "romanLower")
json2:update_string("position.align", "bottom")
json2:update_number("font.size", "10")
success = pdf2:watermark("add", json2, jsonOut)
if not success then
print(pdf2.last_error_text)
return
end
print("roman pages: " .. jsonOut:string_of("pages"))
success = pdf:write_file("qa_output/four_pages_numbered.pdf")
if not success then
print(pdf.last_error_text)
return
end
-- Sample output:
-- footer pages: 1-4
-- roman pages: 2-4