This article includes a step-by-step approach to document transformation as well as sample code to convert HTML to PDF using C#. Document conversion for PDF to HTML C# capability can be performed with a few API calls and two-three lines of code by following this guide. Furthermore, this article defines a few properties for designing a PDF document so that it can be generated according to the expectations of the user.
Steps to Convert HTML to PDF using C#
- Install GroupDocs.Conversion for .NET from the NuGet package manager into the project
- Add a reference to the GroupDocs.Conversion namespace in the code
- Create an object of the Converter class and load the input HTML file
- Create an instance of the PdfConvertOptions class and set attributes for the output PDF document
- Save the output PDF document to the disk by using the Convert method of the Converter class and pass the PdfConvertOptions instance along with the output PDF file name
These steps show how easily you can implement export HTML to PDF C# functionality for performing document transformation by installing the required package and then loading the source HTML file in the constructor of the Converter class. You need to create an instance of PdfConvertOptions class in the next step for setting different properties for the output PDF document and finally use the Convert method to save the output file to a disk.
Code to Convert HTML to PDF using C#
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertHtmlToPdfUsingCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert HTML to PDF using C# | |
{ | |
// Remove the watermark in output PDF document by adding license | |
string licensePath = "GroupDocs.Conversion.lic"; | |
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License(); | |
lic.SetLicense(licensePath); | |
// Load the source HTML file for conversion to PDF | |
var converter = new GroupDocs.Conversion.Converter("sample.html"); | |
// Set the convert options for PDF document | |
var convertOptions = new PdfConvertOptions() | |
{ | |
Height = 500, | |
Width = 500, | |
Dpi = 100, | |
PageNumber = 1, | |
PagesCount = 1 | |
}; | |
// Convert and save the HTML in PDF format | |
converter.Convert("converted.pdf", convertOptions); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
This example shows how to convert documents using the C# generate PDF from HTML functionality. It loads the source document with the Converter class, defines the attributes for the output document with the PdfConvertOptions class, and then saves the converted file with the Convert method. In this example, only a few parameters have been defined for the output PDF document; you can add more properties to the converted file, such as bottom margin, left margin, right margin, top margin, password, Watermark, and so on.
We published an article on converting HTML document to image using C#. Refer to the tutorial on how to convert HTML to Image using C# for more information.