Set PDF properties with Title and Author.
dotnet add package RecursiveAI.OnePdf.Html.RenderRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OnePdf.Html.Render; var options = new HtmlToPdfOptions { Title = "Quarterly results", Author = "Finance team", }; var pdf = HtmlToPdf.ConvertFile( "report.html", "results.pdf", options); Console.WriteLine($"results.pdf: {pdf.Title}");
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
The program writes results.pdf and prints the PDF title:
results.pdf: Quarterly resultsThe download includes report.html. To run the snippet in your own project, save this file beside Program.cs:
<!doctype html> <html lang="en"> <title>Internal draft</title> <meta name="author" content="Draft owner"> <h1>Sales report</h1> <p>April sales increased.</p>
In the PDF’s document properties, Title is Quarterly results and Author is Finance team. The text on the page still starts with “Sales report.” If the input file can’t be read, ConvertFile throws HtmlToPdfException instead of writing a PDF.
Variations
Keep the HTML title and add a subject and keywords. Replace everything after the using line with:
var options = new HtmlToPdfOptions { Author = "Finance team", Subject = "Sales results", Keywords = "sales, quarterly results", }; HtmlToPdf.ConvertFile("report.html", "sales.pdf", options);
sales.pdf keeps Internal draft as its title and gets Finance team as its author, Sales results as its subject and sales, quarterly results as its keywords.
Related recipes
The short answer
Pass Title and Author in HtmlToPdfOptions when you call HtmlToPdf.ConvertFile. The converter writes those values into the PDF document properties, in place of the ones it would have taken from <title> and <meta name="author">. This is useful when a shared HTML template carries a draft title but each PDF needs the title and author set by the application that produces it.
Does HTML-to-PDF conversion copy the title and author automatically?
Yes, if those fields aren’t set in the options. The converter reads the HTML title and the author meta tag. You can override the author but leave Title unset to reuse the title from the template. The same pattern works for Subject (the HTML description meta tag), Keywords (the keywords meta tag) and Language (the root element’s lang). See the metadata options.
Metadata and page content serve different purposes. <h1>Sales report</h1> is rendered on the page, but it isn’t the <title> field the converter reads for metadata. Setting Title in C# doesn’t change that heading. To change both what readers see and what the document properties show, update the heading in the HTML and set the title explicitly or supply it in <title>.
How can I check the title and author of the PDF?
Open results.pdf in a viewer that shows document properties and look for Title and Author. Check the properties rather than the filename: the sample writes results.pdf, but the title is “Quarterly results.” The HtmlToPdfResult.Title property also lets your application report the title after writing the file, as the example does.
Can I set the PDF creation date when converting HTML?
Yes. Supply a DateTimeOffset in CreationDate if the PDF needs an explicit date. If you leave that option unset, the converter doesn’t write a creation date; it doesn’t use the time of conversion. That lets you choose a date from the business record that the PDF represents, rather than the date a batch happened to run.