Recursive AI

Set PDF title and author from HTML in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Terminal
dotnet add package RecursiveAI.OneRender.Pdf

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

C#
using OneRender.Pdf;
string html = "<h1>April shipments</h1>";
var options = new HtmlPdfOptions {
    Output = new PdfOutputOptions {
        Title = "April shipping report",
        Author = "Shipping Desk"
    }
};
byte[] pdf = HtmlPdf.ToPdf(html, options);
File.WriteAllBytes("report.pdf", pdf);
Console.WriteLine("Saved report.pdf");
Text
Saved report.pdf

Unzip the example, then dotnet run.

The full program

The example reads back the PDF’s document information using OnePdf, a dependency of RecursiveAI.OneRender.Pdf. It also extracts the visible heading to show that it’s separate from the metadata title.

C#
using OnePdf;
using OneRender.Pdf;
 
string html = "<h1>April shipments</h1>";
var options = new HtmlPdfOptions {
    Output = new PdfOutputOptions {
        Title = "April shipping report",
        Author = "Shipping Desk",
        Subject = "Monthly dispatches",
        Keywords = "April, shipping"
    }
};
using (var file = File.Create("report.pdf"))
    HtmlPdf.ToPdf(html, file, options);
 
using var doc = PdfDocument.Open("report.pdf");
Console.WriteLine($"Title: {doc.Info.Title}");
Console.WriteLine($"Author: {doc.Info.Author}");
Console.WriteLine($"Subject: {doc.Info.Subject}");
Console.WriteLine($"Keywords: {doc.Info.Keywords}");
Console.WriteLine($"Page: {doc.ExtractText()[0].Text.Trim()}");

dotnet run prints:

Text
Title: April shipping report
Author: Shipping Desk
Subject: Monthly dispatches
Keywords: April, shipping
Page: April shipments

If you see the heading but not the title in your viewer, check Document Properties: the two texts are stored separately.

Variations

Use the HTML document title instead of an explicit Title. Replace the HTML and options in the full program with:

C#
string html = """
<title>April shipping report</title>
<h1>April shipments</h1>
""";
var options = new HtmlPdfOptions {
    Output = new PdfOutputOptions {
        Author = "Shipping Desk"
    }
};

The title still reads April shipping report and the heading still reads April shipments. Subject and keywords are unset in this variation.

The short answer

Set HtmlPdfOptions.Output to a PdfOutputOptions object with Title and Author. Add Subject and Keywords to the same object. Pass those options to HtmlPdf.ToPdf when you create the PDF. To check the file, close the output stream, reopen the PDF and read doc.Info.

How do you set the title and author of a PDF made from HTML?

Assign the values you want in the output options, not in a visible element. The Title is the document name, Author identifies who prepared it, and Subject can give a one-line description. Keywords is a single string: the recipe writes April, shipping and gets the same string back. The PDF backend guide describes the output options; the document properties are distinct from the text and fonts that get drawn on the page.

Is the PDF title the same as the HTML heading?

No. The <h1> supplies visible page content. The metadata title comes from PdfOutputOptions.Title if you supply it. Otherwise, the HTML converter looks for a nonempty <title> and uses its trimmed text. It doesn’t take the first <h1> as a fallback. In the sample, “April shipping report” identifies the file, while the report itself starts with “April shipments.” To rename both, change both values. To change only the title of an already-rendered PDF, edit its document information instead of regenerating the HTML.

All onerender recipes