Swift Requires Chilkat v11.7.0+
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// 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
let pdf = CkoPdf()!
success = pdf.loadFile(filePath: "qa_data/pdf/two_page_memo.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
let json = CkoJsonObject()!
json.updateString(jsonPath: "type", value: "text")
json.updateString(jsonPath: "text", value: "UNCONTROLLED WHEN PRINTED")
json.updateString(jsonPath: "id", value: "printonly")
// As a layer: off on screen, on when printing, and locked so the reader cannot switch it on.
json.updateString(jsonPath: "approach", value: "layer")
json.updateString(jsonPath: "layer.name", value: "Print marking")
json.updateString(jsonPath: "layer.view", value: "off")
json.updateString(jsonPath: "layer.print", value: "on")
json.updateBool(jsonPath: "layer.locked", value: true)
json.updateString(jsonPath: "rotation", value: "diagonal")
json.updateString(jsonPath: "scale.fit", value: "page")
json.updateNumber(jsonPath: "scale.fitPercent", numericStr: "70")
json.updateNumber(jsonPath: "opacity", numericStr: "0.3")
let jsonOut = CkoJsonObject()!
success = pdf.watermark(op: "add", jsonIn: json, jsonOut: jsonOut)
if success == false {
print("\(pdf.lastErrorText!)")
return
}
print("layer id: \(jsonOut.string(of: "id")!) pages: \(jsonOut.string(of: "pages")!)")
success = pdf.writeFile(path: "qa_output/memo_print_only.pdf")
if success == false {
print("\(pdf.lastErrorText!)")
return
}
// The same thing as an annotation instead.
let pdf2 = CkoPdf()!
success = pdf2.loadFile(filePath: "qa_data/pdf/two_page_memo.pdf")
if success == false {
print("\(pdf2.lastErrorText!)")
return
}
let json2 = CkoJsonObject()!
json2.updateString(jsonPath: "type", value: "text")
json2.updateString(jsonPath: "text", value: "UNCONTROLLED WHEN PRINTED")
json2.updateString(jsonPath: "id", value: "printonly2")
json2.updateString(jsonPath: "approach", value: "annotation")
json2.updateBool(jsonPath: "annotation.view", value: false)
json2.updateBool(jsonPath: "annotation.print", value: true)
json2.updateBool(jsonPath: "annotation.fixedPrint", value: true)
json2.updateString(jsonPath: "rotation", value: "diagonal")
success = pdf2.watermark(op: "add", jsonIn: json2, jsonOut: jsonOut)
if success == false {
print("\(pdf2.lastErrorText!)")
return
}
print("annotation id: \(jsonOut.string(of: "id")!)")
// Sample output:
// layer id: printonly pages: 1-2
// annotation id: printonly2
}