Converting Between Formats | C#
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. So, Aspose.HTML for .NET offers several ways to convert between formats.
A Few Ways to Convert files
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:
using System.IO; using Aspose.Html.Converters; using Aspose.Html.Saving; ... // Invoke the ConvertHTML() method to convert the HTML code to PDF 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
outputPath. Note: You need to specify the path to the output file in your local file system (
To convert HTML to PDF using the RenderTo() method, you can use the following code snippet:
using Aspose.Html; using Aspose.Html.Rendering.Pdf; using System.IO; ... // Prepare path to a source HTML file string documentPath = Path.Combine(DataDir, "spring.html"); // Prepare path for converted file saving string savePath = Path.Combine(OutputDir, "spring-output.pdf"); // Initialize an HTML document from the file using var document = new HTMLDocument(documentPath); // Create an instance of the PdfRenderingOptions class var pdfOptions = new PdfRenderingOptions(); // Create the PDF Device and specify the output file to render using var device = new PdfDevice(pdfOptions, savePath); // Render HTML to PDF 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.
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, JPEG, PNG, BMP, TIFF, GIF, MD, and MHTML.
- SVG Converter - You find information on how to convert SVG to PDF, XPS, DOCX, JPEG, PNG, BMP, TIFF, and GIF formats.
- EPUB Converter - You find out how to convert EPUB to PDF, XPS, DOCX, JPEG, PNG, BMP, TIFF, and GIF formats.
- MHTML Converter provides a general description of how to convert MHTML to PDF, XPS, DOCX, JPEG, 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.