Converting Between Formats – C# Examples
Welcome to the “Converting between Formats” guides! The main highlight of Aspose.HTML is the 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. This chapter provides a general description of the conversion features of Aspose.HTML .NET. In the article you find out how to convert HTML, MHTML, EPUB, Markdown and SVG files using .NET library.
A Few Ways to Convert files
Aspose.HTML for .NET offers several ways to convert between formats: converting can perform by using methods of the Converter class or the RenderTo() method.
The static Converter class is a shared facade that provides HTML, MHTML, EPUB, Markdown and SVG file conversions to the popular formats and allows to make these operations convenient and easy. The static methods as an understandable and straightforward way to convert HTML, SVG, MHTML, EPUB, MD documents to PDF, XPS, DOCX, MD, MHTML and Image formats.
The RenderTo() method is used to render HTML or SVG to another format and send a document to the output device. Aspose.HTML API provides the following output devices implementation: the PdfDevice, XpsDevice, DocDevise, and ImageDevice classes, which perform rendering to PDF, XPS, DOCX and Image file formats respectively.
The following example demonstrates converting HTML to PDF using the Converter class, with one line of code:
1// Invoke the ConvertHTML() method to convert HTML to PDF
2Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));
In the example, we use the
ConvertHTML(content, baseUri, options, outputPath
) method of the Converter class. Despite the HTML to PDF conversion is required only one line of code, the following operations were performed:
- Prepare code for an HTML document.
- Create a new PdfSaveOptions object with default options.
- Use the ConvertHTML() method that takes as parameters
content
,baseUri
,options
, andoutputPath
. Note: You need to specify the path to the output file in your local file system (outputPath
).
To convert HTML to PDF using the RenderTo() method, you can use the following code snippet:
1// Prepare HTML code
2var code = @"<span>Hello, World!!</span>";
3
4// Prepare a path to save a converted file
5string savePath = Path.Combine(OutputDir, "document.pdf");
6
7// Initialize an HTML document from the HTML code
8using var document = new HTMLDocument(code, ".");
9
10// Create a PDF Device and specify the output file to render
11using var device = new PdfDevice(savePath);
12
13// Render HTML to PDF
14document.RenderTo(device);
In the Fine-Tuning Converters article, you will find more information on using different rendering options and parameters to customize the rendering result.
Popular Converters
The current chapter describes popular converters, as well as C# examples for most common conversions scenarios. Using the examples, you can understand that the conversion process is quite universal and includes only a few necessary steps. The articles in this chapter provide information on the list of supported popular conversions and how to perform them using the Converter class that groups all low-level conversion operations in a single class to make it comfy and easy to use. Don’t delay! Test the conversion between formats!
- HTML Converter – allows convert an HTML document to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MD, and MHTML.
- SVG Converter – You find information on how to convert SVG to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
- EPUB Converter – You find out how to convert EPUB to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
- MHTML Converter provides a general description of how to convert MHTML to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
- Markdown Converter – You learn how to convert MD to HTML, PDF, XPS, DOCX, and Image file formats using Aspose.HTML for .NET API.
Aspose.HTML offers free online Converters for converting HTML, XHTML, MHTML, EPUB, XML, and Markdown files to a variety of popular formats. You can easily convert HTML to PDF, HTML to JPG, SVG to PDF, MHTML to PDF, or MD to HTML. Just select the file, choose the format to convert, and you’re done. It’s fast and completely free!