How-to guides/Metadata

Write and update PDF metadata in Rust

Set or update title, author, subject, keywords, and custom fields in the PDF Info dictionary and the XMP metadata stream.

rust
use pdfluent::{PdfDocument, PdfMetadata};

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

    doc.set_metadata(PdfMetadata {
        title: Some("Q3 Financial Report".into()),
        author: Some("Finance Team".into()),
        subject: Some("Quarterly earnings summary".into()),
        keywords: Some("finance, earnings, Q3 2025".into()),
        ..Default::default()
    })?;

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

Step by step

1

Open the document for editing

Open the PDF with a mutable binding. PDFluent loads the existing metadata so you can selectively update fields.

rust
use pdfluent::PdfDocument;

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

Build a PdfMetadata value

PdfMetadata implements Default, so only the fields you set are updated. Fields left as None keep their existing values.

rust
use pdfluent::PdfMetadata;

let meta = PdfMetadata {
    title: Some("Annual Report 2025".into()),
    author: Some("Acme Corp".into()),
    keywords: Some("annual, report, 2025".into()),
    ..Default::default()
};
3

Apply the metadata

Call set_metadata() to write the fields to the Info dictionary. The XMP stream is updated automatically to stay in sync.

rust
doc.set_metadata(meta)?;
4

Set a specific field without replacing others

Use the individual setter methods if you only want to change one field.

rust
doc.set_title("Revised Annual Report 2025")?;
doc.set_author("Acme Corp Communications")?;
5

Save the updated document

Call save() or save_as() to write the changes. The modification date is updated automatically.

rust
doc.save("report_updated.pdf")?;
// or save to a new path:
doc.save_as("report_v2.pdf")?;

Notes and tips

  • The modification date (ModDate) is set automatically to the current UTC time when you call save().
  • PDFluent keeps the Info dictionary and XMP stream in sync. You do not need to update both manually.
  • To clear a field, set it to Some(String::new()) or use doc.clear_metadata_field("Title").

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