This quick tutorial provides details on how to convert HTML page to PDF in C# where the source HTML file is loaded from the disk. The output PDF file can be configured by setting different properties like title, modification date, etc. This conversion of HTML to PDF using C# is finally completed by saving the PDF file along with above stated properties.
Steps to Convert HTML Page to PDF in C#
- Configure the project to add Aspose.HTML from the NuGet package manager
- Load the source HTML file using HTMLDocument class for conversion to PDF
- Initialize the PdfSaveOptions object to configure the output PDF file
- Set the output PDF document title, modification date, and background color
- By calling Save method in C# export HTML to PDF
These steps to convert HTML to PDF using C# show that first the necessary libraries are added and then HTMLDocument class is used to load the source HTML file. In the next step PdfSaveOptions class object is created that contains a variety of properties like DocumentInfo class object that further contains different properties including Title and ModificationDate mentioned above. Similarly, the PdfSaveOptions class object contains background color property that is set in this tutorial.
Code to convert HTML to PDF using C#
This code uses HTMLDocument class object to load file from the disk, however there are a lot of other overloads available like you can load an HTML page using a URL, create an HTML document from RequestMessage, load from stream, and many more. Similarly, PdfSaveOptions contains other properties like encryption, page setup, horizontal and vertical resolution, etc.
By using C# generate PDF from HTML is easily possible using detailed steps and code sample given above. If you want to convert HTML to some other format like text, refer to the article on how to convert HTML to Text in C#.