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

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

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

    let json = CkoJsonObject()!
    json.updateString(jsonPath: "type", value: "text")
    json.updateString(jsonPath: "text", value: "Page {page} of {pages}  -  printed {date:yyyy-MM-dd HH:mm}  -  {filename}")
    json.updateString(jsonPath: "id", value: "pagefoot")
    json.updateString(jsonPath: "position.align", value: "bottom")
    json.updateNumber(jsonPath: "position.margin", numericStr: "24")
    json.updateNumber(jsonPath: "font.size", numericStr: "9")
    json.updateString(jsonPath: "color", value: "#505050")

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

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

    //  Roman numerals that start at i on page 2, so that a cover page is not numbered.
    let pdf2 = CkoPdf()!
    success = pdf2.loadFile(filePath: "qa_data/pdf/four_pages.pdf")
    if success == false {
        print("\(pdf2.lastErrorText!)")
        return
    }

    let json2 = CkoJsonObject()!
    json2.updateString(jsonPath: "type", value: "text")
    json2.updateString(jsonPath: "text", value: "- {page} -")
    json2.updateString(jsonPath: "id", value: "roman")
    json2.updateString(jsonPath: "pages", value: "2-")
    json2.updateInt(jsonPath: "pageNumber.start", value: 1)
    json2.updateString(jsonPath: "pageNumber.style", value: "romanLower")
    json2.updateString(jsonPath: "position.align", value: "bottom")
    json2.updateNumber(jsonPath: "font.size", numericStr: "10")
    success = pdf2.watermark(op: "add", jsonIn: json2, jsonOut: jsonOut)
    if success == false {
        print("\(pdf2.lastErrorText!)")
        return
    }

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

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

    //  Sample output:

    //  footer pages: 1-4
    //  roman pages: 2-4

}