How to Convert HTML Page to PDF in C#

This how-to guide assists on how to convert HTML page to PDF in C#. It contains information about configuring the environment to use Aspose.PDF for .NET to transform HTML to PDF, a list of steps to write the application, and a sample code demonstrating the HTML to PDF conversion using C#. You will also learn different options to load the HTML file and customize of the output PDF file.

Steps to Transform HTML to PDF in C#

  1. Establish the environment to use Aspose.PDF for .NET to transform HTML to PDF
  2. Create an instance of the HtmlLoadOptions object to customize the loading process
  3. Create an instance of the Document class object by using the above custom loading options
  4. Save the output PDF file having the HTML contents

These steps describe how using C# HTML to PDF conversion can be done. The process is quite simple where the source HTML file is to be loaded into the Document object along with the default settings of the HtmlLoadOptions class object used as the second argument in the constructor. You optionally set different properties of the HtmlLoadOptions class object before loading the HTML and then use the Save method in the Document class to save the output PDF file.

Code to Convert HTML Page to PDF in C#

For writing HTML to PDF converter C# code is demonstrated here. The HTMLLoadOptions can be customized by setting HtmlMediaType to be used during the rendering, setting the PageLayoutOption properties like fit to widest content width or scale to page width, and the option to render the HTML to a single page. Similarly, you may use the PdfSaveOptions class object while saving the PDF on the disk that supports setting the default font name, and desired save format like PDF, DOC, XPS, and SVG to name a few.

This topic has explained that to convert HTML to PDF C# is one of the simplest choices. If you want to learn other operations like adding a Digital signature in a PDF, refer to the article on how to add digital signature to PDF in C#.

 English