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

Put a Letterhead Behind the First Page of a PDF

See more PDF Watermark Examples

Demonstrates the pdfPage type, which places a page of another PDF as the watermark. It is the way to use a letterhead, a cover overlay or a vector logo that was saved as a PDF, and unlike an image it stays sharp at any size because it is drawn, not resampled. "zOrder" set to back puts it behind the existing content, where a letterhead belongs, and "pages" set to first restricts it to page one.

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

    //  The PDF used in this example is available at letterhead.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: "pdfPage")
    json.updateString(jsonPath: "pdf.path", value: "qa_data/pdf/letterhead.pdf")

    //  The page of the source PDF to use (1 by default).  "match" instead of a number places source
    //  page n on target page n, which suits a set of per-page backgrounds.
    json.updateInt(jsonPath: "pdf.page", value: 1)

    //  Behind the memo's own text, on page one, scaled to the page.
    json.updateString(jsonPath: "zOrder", value: "back")
    json.updateString(jsonPath: "pages", value: "first")
    json.updateString(jsonPath: "scale.fit", value: "page")
    json.updateString(jsonPath: "id", value: "letterhead")

    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/memo_letterhead.pdf")
    if success == false {
        print("\(pdf.lastErrorText!)")
        return
    }

    //  Sample output:

    //  id: letterhead pages: 1

}