How-to guides/Metadata

Write XMP metadata to a PDF in Rust

Write Dublin Core, XMP Basic, and custom XMP metadata packets to a PDF. XMP metadata is readable by search engines, DAM systems, and archival tools.

rust
use pdfluent::{PdfDocument, XmpMetadata};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut doc = PdfDocument::open("whitepaper.pdf")?;

    let xmp = XmpMetadata::new()
        .title("PDFluent Technical Whitepaper")
        .creator("Engineering Team")
        .description("Architecture overview of the PDFluent Rust SDK")
        .subject(vec!["PDF", "Rust", "SDK"])
        .rights("Copyright 2025 PDFluent")
        .language("en-US");

    doc.set_xmp_metadata(xmp)?;
    doc.save("whitepaper_with_xmp.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml.

rust
[dependencies]
pdfluent = "0.9"
2

Open the PDF

Load the document to which you want to add XMP metadata.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("report.pdf")?;
3

Build the XMP metadata object

XmpMetadata provides setters for Dublin Core and XMP Basic properties. All fields are optional.

rust
use pdfluent::XmpMetadata;

let xmp = XmpMetadata::new()
    .title("Annual Report 2025")
    .creator("Finance Department")
    .description("Consolidated financial statements for fiscal year 2025")
    .subject(vec!["Finance", "Annual Report", "2025"])
    .publisher("Acme Corp")
    .rights("All rights reserved")
    .language("en-GB")
    .creation_date("2025-03-01T09:00:00Z")
    .modify_date("2025-04-14T15:30:00Z");
4

Add a custom XMP namespace and property

Register a custom namespace to store application-specific metadata alongside the standard Dublin Core fields.

rust
let xmp = xmp
    .custom_namespace("http://ns.acme.com/pdf/1.0/", "acme")
    .custom_property("acme:documentId", "DOC-2025-0042")
    .custom_property("acme:department", "Legal")
    .custom_property("acme:confidentiality", "Internal");
5

Write the metadata and save

set_xmp_metadata() serialises the XMP packet and embeds it in the PDF. Existing XMP metadata is replaced.

rust
doc.set_xmp_metadata(xmp)?;
doc.save("report_with_xmp.pdf")?;
println!("XMP metadata written.");

Notes and tips

  • XMP metadata in PDFs is stored as an XML packet in the /Metadata stream of the document catalog.
  • Setting XMP metadata does not change the DocInfo dictionary (/Author, /Title, etc.). Use doc.set_info() to set both.
  • XMP supports multi-language values via xml:lang attributes. Use .title_lang("fr-FR", "Rapport Annuel") for localised titles.
  • After calling set_xmp_metadata(), any existing digital signature becomes invalid. Set metadata before signing.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions