Sample code for 30+ languages & platforms
Rust 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 Rust Downloads

Rust

// 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 = chilkat::Pdf::new();
if pdf.load_file("qa_data/pdf/two_page_memo.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

let json = chilkat::JsonObject::new();
let _ = json.update_string("type", "pdfPage");
let _ = json.update_string("pdf.path", "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.
let _ = json.update_int("pdf.page", 1);

// Behind the memo's own text, on page one, scaled to the page.
let _ = json.update_string("zOrder", "back");
let _ = json.update_string("pages", "first");
let _ = json.update_string("scale.fit", "page");
let _ = json.update_string("id", "letterhead");

let json_out = chilkat::JsonObject::new();
if pdf.watermark("add", &json, &json_out).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

println!("id: {} pages: {}", json_out.string_of("id").unwrap_or_default(), json_out.string_of("pages").unwrap_or_default());

if pdf.write_file("qa_output/memo_letterhead.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

// Sample output:

// id: letterhead pages: 1