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

Add an Image (Logo) Watermark to a PDF

See more PDF Watermark Examples

Demonstrates an image watermark placed in a corner of every page. The image is a PNG or JPEG given either by path ("image.path") or, as here, as base64 in the JSON itself ("image.base64"), which suits a server that keeps its logo in a database rather than on disk. PNG transparency is kept. Giving only a width keeps the aspect ratio, and the units member says those numbers are inches rather than points.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();
    pdf.loadFile("qa_data/pdf/four_pages.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("type", "image") catch {};

    // A small PNG logo, base64 encoded.  Use "image.path" instead to name a PNG or JPEG file:
    //    ignore = json.UpdateString("image.path","logo.png");
    json.updateString("image.base64", "iVBORw0KGgoAAAANSUhEUgAAAGAAAAAgCAYAAADtwH1UAAAApElEQVR42u3a7QmAIBgAYTdpo/ZphhZti6IBylLrlXoO/G3cIeFHSgAAFDKM08rCy8Jzg6Ug8UJ0JF+EDuSXRGgxX8+jufxlTk0nFuCCiF360aidXICMiDP5uQgCXAxQI782ggAHEu7IP4sgQOcBIMA35ItgBQhQEqBmtbX6xl//hAUI3gcIELwTFiD4LEiA4NNQAYLvA+BGzJ0wvIrwLggA8DgbwPbrr8c7+lkAAAAASUVORK5CYII=") catch {};

    // The size to draw it at.  Give only the width and the height follows the aspect ratio.
    json.updateString("units", "in") catch {};
    json.updateNumber("image.width", "1.5") catch {};

    // Bottom-right, half an inch in from the page edges.
    json.updateString("position.align", "bottomRight") catch {};
    json.updateNumber("position.margin", "0.5") catch {};
    json.updateNumber("opacity", "0.85") catch {};
    json.updateString("id", "logo") catch {};

    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.watermark("add", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("id: {s} pages: {s}\n", .{ try json_out.stringOf(alloc, "id"), try json_out.stringOf(alloc, "pages") });

    pdf.writeFile("qa_output/four_pages_logo.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Sample output:

    // id: logo pages: 1-4
}