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

Tile a Watermark Across a PDF Page

See more PDF Watermark Examples

Demonstrates a tiled watermark, which repeats the stamp over the whole page instead of placing it once. Tiling overrides position.align; tile.spacingX and spacingY set the gaps, and tile.stagger offsets alternate rows by half a tile so the pattern does not line up in columns. A faint tiled stamp carrying the name or address is the usual way to mark a document so that a leaked copy can be traced back, and zOrder back keeps it behind the text where it does not hinder reading.

Chilkat Swift Downloads

Swift

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

    let pdf = CkoPdf()!
    success = pdf.loadFile(filePath: "qa_data/pdf/long_report.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "type", value: "text")

    //  {date:FORMAT} is replaced per page when the watermark is added.  The other placeholders are
    //  {page}, {pages} and {filename}; {{ gives a literal brace.
    json.updateString(jsonPath: "text", value: "jdoe@example.com  {date:yyyy-MM-dd}")
    json.updateString(jsonPath: "id", value: "trace")
    json.updateString(jsonPath: "zOrder", value: "back")
    json.updateNumber(jsonPath: "font.size", numericStr: "14")
    json.updateNumber(jsonPath: "opacity", numericStr: "0.15")
    json.updateNumber(jsonPath: "rotation", numericStr: "30")

    json.updateBool(jsonPath: "tile.enabled", value: true)
    json.updateNumber(jsonPath: "tile.spacingX", numericStr: "60")
    json.updateNumber(jsonPath: "tile.spacingY", numericStr: "80")
    json.updateBool(jsonPath: "tile.stagger", value: true)

    let jsonOut = CkoJsonObject()!
    success = pdf.watermark(op: "add", jsonIn: json, jsonOut: jsonOut)
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    print("id: \(jsonOut.string(of: "id")!) pages: \(jsonOut.string(of: "pages")!)")

    success = pdf.writeFile(path: "qa_output/report_traced.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  Sample output:

    //  id: trace pages: 1-3

}