---
title: "Set PDF title and author from HTML in C#"
url: https://recursiveai.org/onerender/cookbook/set-title-author-metadata-in-html-to-pdf/
description: "Set title, author, subject, and keywords when converting HTML to PDF in C#, then reopen the file to read its document properties."
version: "1.4.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneRender.Pdf) · [GitHub](https://github.com/recursive-ai-oss/onerender-csharp) · [Docs](https://recursiveai.org/onerender/docs/) · [Download example (.zip)](https://recursiveai.org/onerender/cookbook/set-title-author-metadata-in-html-to-pdf/set-title-author-metadata-in-html-to-pdf.zip)

```shell
dotnet add package RecursiveAI.OneRender.Pdf
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onerender/cookbook/set-title-author-metadata-in-html-to-pdf/set-title-author-metadata-in-html-to-pdf.zip), 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.

```csharp
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:

```csharp
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.

## Related recipes

- [Convert HTML to PDF in C#](https://recursiveai.org/onerender/cookbook/html-to-pdf/)
- [Set document metadata on an existing PDF](https://recursiveai.org/onepdf/cookbook/set-pdf-document-metadata/)

## 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](https://recursiveai.org/onerender/docs/pdf/) 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.
