Recursive AI

Set PDF title and author in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use report.pdf from the zip or your own PDF.

Terminal
dotnet add package RecursiveAI.OnePdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf;
var edit = new PdfOpenOptions
    { AllowEditing = true };
using var pdf = PdfDocument.Open(
    "report.pdf", edit);
pdf.SetInfo("Title", "Quarterly report");
pdf.SetInfo("Author", "Jordan Lee");
pdf.Save("report-updated.pdf");
Console.WriteLine("Wrote report-updated.pdf");

Unzip the example, then dotnet run.

This writes report-updated.pdf with new Title and Author entries in its Info dictionary, the PDF’s document-properties record.

Text
Wrote report-updated.pdf

The full program

The one-page sample is headed “Quarterly report draft.” Its original document title is “Draft” and author is “Editorial team.” The program reopens the saved copy to print the updated values from disk.

C#
using OnePdf;
 
var options = new PdfOpenOptions { AllowEditing = true };
using var pdf = PdfDocument.Open("report.pdf", options);
pdf.SetInfo("Title", "Quarterly report");
pdf.SetInfo("Author", "Jordan Lee");
pdf.Save("report-updated.pdf");
 
using var saved = PdfDocument.Open("report-updated.pdf");
Console.WriteLine($"Title: {saved.Info.Title}");
Console.WriteLine($"Author: {saved.Info.Author}");
Text
Title: Quarterly report
Author: Jordan Lee

OnePDF opens PDFs read-only by default; without AllowEditing = true, SetInfo throws InvalidOperationException.

Variations

Add these calls just before pdf.Save(...) to save a subject and keywords in the same Info dictionary:

C#
pdf.SetInfo("Subject", "Quarterly operations");
pdf.SetInfo("Keywords", "operations, quarterly");

To remove the Info author entry rather than replacing it, change the Author call to:

C#
pdf.SetInfo("Author", null);

The short answer

Open the PDF with editing enabled, call SetInfo("Title", ...) and SetInfo("Author", ...), then call Save with a different path. SetInfo creates the Info dictionary if the PDF doesn’t already have one. It updates only the entry you name, so you can fix the author without supplying a new title or subject. The sample leaves the input file untouched.

Why does the title of my PDF not match the filename?

report-updated.pdf is the name of the saved file. Quarterly report is its document title. A reader can display the stored title instead of the filename, so renaming the file isn’t a way to correct its title. Likewise, setting Title won’t rename the file or replace a heading printed on page one. Open the new file’s document properties or read Info.Title and Info.Author as the full program does to check the values you saved. Setting Author also doesn’t change who is listed as the author of an existing comment or annotation.

What is XMP metadata in a PDF?

XMP is an XML metadata packet that a PDF can store separately from its Info dictionary. A file can have both, and the same properties can appear in both places. SetInfo changes the Info dictionary only. If an app reads title or author from XMP, it can still see old values even though the new file’s Info entries are correct.

If both stores matter for your workflow, update each one. OnePDF exposes the XMP packet separately and takes replacement XML bytes through SetXmp. That replaces the whole packet rather than patching a field. Preserve the other XML properties your workflow needs when preparing the replacement. The sample has no XMP packet to update. See the OnePDF writing reference for both APIs.

All onepdf recipes