Sample code for 30+ languages & platforms
Zig

Update a String Property in XMP

See more XMP Examples

Demonstrates how to open a JPG or TIF image file, access the XMP metadata, and update the value of a string property. (If the string property does not already exist, it is created.)

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const xmp = try chilkat.Xmp.init();
    defer xmp.deinit();

    // Load a JPG or TIF image file.
    xmp.loadAppFile("qa_data/xmp/AJ_123642_1511.tif") catch {
        std.debug.print("{s}\n", .{try xmp.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Num embedded XMP docs: {d}\n", .{xmp.getNumEmbedded()});

    // This example assumes that XMP metadata is already present in the image file.
    if (xmp.getNumEmbedded() == 0) {
        std.debug.print("No XMP metadata already exists..\n", .{});
        return;
    }

    // Get the XMP metadata.
    const xml = try xmp.getEmbedded(0);
    defer xml.deinit();

    // Show the XML:
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Update (overwrite) a string property.
    xmp.addSimpleStr(xml, "NumberofTimes", "123") catch {};

    xmp.saveAppFile("qa_output/updated.tif") catch {
        std.debug.print("{s}\n", .{try xmp.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Success.\n", .{});
}