Adding a watermark: Adding a watermark to a worksheet by adding a background image

An example of adding a background image to a worksheet. In this case it is used as a watermark.

See also the previous example where a watermark is created by putting an image in the worksheet header.

Image of the output file:

Image of output from app_images.rs

Code to generate the output file:

// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2025, John McNamara, jmcnamara@cpan.org

//! An example of inserting a background image into a worksheet using
//! `rust_xlsxwriter`.
//!
//! See also the `app_watermark.rs` example which shows how to set a watermark
//! via the header image of a worksheet. That is the way that the Microsoft
//! documentation recommends to set a watermark in Excel.

use rust_xlsxwriter::{Image, Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    // Create a new Excel file object.
    let mut workbook = Workbook::new();

    // Add a worksheet to the workbook.
    let worksheet = workbook.add_worksheet();

    // The image may not be visible unless the view is large.
    worksheet.write(0, 0, "Scroll down and right to see the background image")?;

    // Create a new image object.
    let image = Image::new("examples/watermark.png")?;

    // Insert the background image.
    worksheet.insert_background_image(&image);

    // Save the file to disk.
    workbook.save("background_image.xlsx")?;

    Ok(())
}