HTML Converter for .NET

Introduction to HTML Converter for .NET

The HTML Converter for .NET is a plugin that allows developers to convert HTML documents to various formats, including PDF, XPS, and image files. This guide provides an overview of the available features and explains how to perform common tasks using code examples in .NET.

Converting HTML to PDF

To convert an HTML document to a PDF file, you can use the HTMLDocument class. Here is an example:

using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;

// Create an instance of HTMLDocument
var doc = new HTMLDocument("developer-guide.html");
var options = new PdfSaveOptions();

// Convert HTML file to PDF
Converter.ConvertHTML(doc, options, "developer-guide.pdf")

This code converts an HTML string to a PDF file and saves it to a file named “developer-guide.pdf”.

Converting HTML to Image

To convert an HTML document to an image file, you can use the HTMLDocument class with the ConvertHTML method. Here is an example:

using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;

// Create an instance of HTMLDocument
var doc = new HTMLDocument("developer-guide.html");
var options = new ImageSaveOptions(ImageFormat.Png);

// Convert HTML file to PNG
Converter.ConvertHTML(doc, options, "developer-guide.png")

This code converts an HTML string to a PNG image and saves it to a file named “output.png”.

Handling Errors and Exceptions

You can handle errors and exceptions using try-catch blocks. Here is an example:

using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;

// Create an instance of HTMLDocument
var doc = new HTMLDocument("developer-guide.html");
var options = new PdfSaveOptions();

try {
    // Convert HTML file to PDF
    Converter.ConvertHTML(doc, options, "developer-guide.pdf")
catch (Exception x) {
    Console.WriteLine($"Error converting HTML: {x.Message}");
}
 English