HTML Converter – Convert HTML in C#

Use the HTML Converter guides when your source is an HTML file, HTML string, stream, or URL. Aspose.HTML for .NET lets C# applications convert HTML to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MHTML, and Markdown.

HTML conversion is the central workflow in Aspose.HTML for .NET because HTML is often the source format for reports, invoices, web pages, archives, and generated content. Most examples in this section use HTMLDocument, a target-specific save options class, and Converter.ConvertHTML().

How HTML Conversion Works

  1. Load HTML from a file, string, stream, or URL with HTMLDocument or a ConvertHTML() overload.
  2. Create save options for the target format, such as PdfSaveOptions, ImageSaveOptions, DocSaveOptions, XpsSaveOptions, MHTMLSaveOptions, or MarkdownSaveOptions.
  3. Call Converter.ConvertHTML() and pass the source, options, and output path or stream provider.

The following example converts an HTML file to PNG with ImageSaveOptions.

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

For conversion parameters, output streams, and page setup, open the format-specific guides below.

HTML Conversion Guides

HTML to fixed-layout documents

HTML to Word and web archive formats

HTML to image formats

Related Conversion Controls

Other Platforms

Try Online HTML Conversion

                
            

Use the online HTML Converter for quick file checks. Use Aspose.HTML for .NET when HTML conversion must be automated in C# code.