This article demonstrates the easiest methods to convert HTML content to PDF in C# .NET with high-fidelity.
There are various scenarios when you need to perform HTML to PDF conversion from within C# .NET applications. For example, you may want to convert a web page to PDF from within your application or you may need to generate PDF from the content of a WYSIWYG HTML editor. Another scenario could be converting the HTML page from a specific URL to PDF. In order to deal with such cases, you will learn how to perform HTML to PDF conversion in C#. The HTML content could be a .html file or a web page from a URL.
- C# .NET HTML to PDF Converter API
- HTML to PDF C# Conversion
- Convert HTML to password-protected PDF using C#
- Generate PDF from a Web Page in C#
- Get a Free API License
C# .NET HTML to PDF Converter - Free Download
Aspose.PDF for .NET is a PDF manipulation API that lets you convert HTML documents to PDF seamlessly. We will use this API to convert HTML content to PDF. You can either download API’s DLL or install its package from NuGet.
NuGet Package Manager
Package Manager Console
PM> Install-Package Aspose.PDF
Steps to Convert HTML to PDF in C# .NET
The following are the steps to convert HTML to PDF using C# with Aspose.PDF for .NET.
- Download and install the Aspose.PDF for .NET
- Add required namespaces of the API
- Load the HTML content from file or URL
- Generate PDF from provided HTML content
Now, let’s have a look at how to perform these steps in C# and convert an HTML file to PDF.
Convert HTML to PDF in C#
The following are the simple steps to generate PDF from HTML using Aspose.PDF for .NET.
- Create an instance of the HtmlLoadOptions class.
- Create an instance of Document class and initialize it with the HTML file’s path and HtmlLoadOptions object.
- Call Document.Save(String) method with the output PDF file’s name.
The following code sample shows how to convert HTML to PDF using C#.
Input HTML File
Converted PDF Document
C# HTML to Password-Protected PDF
You can also convert HTML files to encrypted PDF documents using Aspose.PDF for .NET. The resultant PDF document can be protected with the user’s password, the owner’s password, access permissions, and cryptographic algorithm. You can encrypt the converted PDF using the Document.Encrypt() method.
The following code sample shows how to convert an HTML file to an encrypted PDF in C#.
C# Export HTML to PDF from URL
Aspose.PDF for .NET also supports conversion from the live URLs. The following are the steps to generate PDF from a webpage using C#.
- Make a web request to the URL using WebRequest.
- Get the response of the web request into the Stream object.
- Create the Document object and initialize it with InputStream and HtmlLoadOptions objects.
- Save HTML stream as PDF using Document.Save() method.
The following code sample shows how to convert the web page to PDF in C#.
HTML to PDF Converter - Get a Free API License
You can convert HTML to PDF without evaluation limitations by getting a free temporary license.
In this article, you have learned how to convert HTML content to PDF programmatically in C#. In addition to the simple conversion, this article also covered how to convert HTML to password-protected PDF. You can easily integrate the provided code into your applications and perform HTML to PDF conversion.
You may learn more about Aspose.PDF for .NET using the documentation.