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:
1using System.IO;
2using Aspose.Html.Converters;
3using Aspose.Html.Saving;
4...
5 // Invoke the ConvertHTML() method to convert the HTML code to PDF
6 Converter.ConvertHTML(@"<h1>Hello, World!</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:
1using Aspose.Html;
2using Aspose.Html.Rendering.Pdf;
3using System.IO;
4...
5
6 // Prepare path to a source HTML file
7 string documentPath = Path.Combine(DataDir, "spring.html");
8
9 // Prepare path for converted file saving
10 string savePath = Path.Combine(OutputDir, "spring-output.pdf");
11
12 // Initialize an HTML document from the file
13 using var document = new HTMLDocument(documentPath);
14
15 // Create an instance of the PdfRenderingOptions class
16 var pdfOptions = new PdfRenderingOptions();
17
18 // Create the PDF Device and specify the output file to render
19 using var device = new PdfDevice(pdfOptions, savePath);
20
21 // Render HTML to PDF
22 document.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 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!