HTML Converter – Convert HTML in C#

HTML (HyperText Markup Language) is the standard markup language for documents created for display in browsers. Known as the language of the web, HTML has evolved with requirements of new information demands to be displayed as part of web pages. The latest variant is known as HTML 5 that gives a lot of flexibility for working with the language. HTML pages are either received from a server, where these are hosted or can be loaded from a local system as well.

The main highlight of Aspose.HTML for .NET is a conversion feature. Converting between formats is required for various reasons: to work in a familiar, convenient format or to take advantage of different formats for specific tasks. The Aspose.Html.Converters namespace implements easy access to conversion methods. It provides a wide range of HTML conversions to popular formats, such as PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MHTML, and MD.

Online HTML Converter

You can check the Aspose.HTML API functionality and convert HTML in real-time. First, load an HTML file from your local drive and then run the example. In this example, the save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert HTML to PNG programmatically, please see the following C# code examples.

How to Convert HTML in C#

This article gives information on the list of supported HTML conversions and how to perform them using the Converter class that groups all low-level conversion operations in a single class to make them comfy and easy to use.

Any conversion you want to perform involves loading an HTML document and saving it in the supported format. It can be different scenarios, but it can be made with a few required steps:

  1. Load an HTML document into a Document object using one of HTMLDocument() constructors. You can load HTML from a file, HTML code, stream, or URL.
  2. Create a Save Options object.
  3. Invoke one of the ConvertHTML() methods and pass the required parameters to it.

Please take a look over the following C# code snippet that shows the HTML to PNG conversion using the ConvertHTML(document, options, savePath) method.

 1// Prepare a path to a source HTML file
 2string documentPath = Path.Combine(DataDir, "nature.html");
 3
 4// Prepare a path to save the converted file
 5string savePath = Path.Combine(OutputDir, "nature-output.png");
 6
 7// Initialize an HTML document from the file
 8using var document = new HTMLDocument(documentPath);
 9
10// Create an instance of the ImageSaveOptions class 
11var options = new ImageSaveOptions(ImageFormat.Png);
12
13// Convert HTML to PNG
14Converter.ConvertHTML(document, options, savePath);

The ConvertHTML() method takes as parameters document, options, and savePath and performs the operation. Note: You need to specify the path to the output file in your local file system (savePath). Also, for HTMLDocument(address) constructor, you need to provide the path to a source HTML file on your local file system.

Aspose.HTML offers a free online HTML Converter for converting HTML files to a variety of popular formats. You can easily convert HTML to PDF, HTML to XPS, HTML to DOCX, HTML to JPG, HTML to PNG, HTML to BMP, HTML to TIFF, HTML to GIF, HTML to MD, and HTML to MHTML. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!

You can download the complete C# examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.