Rendering Device – C#
Using the ConvertHTML() methods of the Converter class is the most common way to convert HTML documents into various formats. However, Aspose.HTML for .NET API also provides alternative ways to render HTML documents with that can give you more control over the rendering process in your C# application.
In this article, we describe how to use rendering devices – PdfDevice, XpsDevice, DocDevice, and ImageDevice to render HTML to PDF, XPS, DOCX, and Images. In the C# examples, the rendering process is considered with default rendering options. To learn more, please read the Rendering Options article.
What is Rendering Device in Aspose.HTML?
The rendering device encapsulates a 2D drawing surface, whose API is implemented using the IDevice interface. Currently, API provides the following implementations: PdfDevice, XpsDevice, DocDevice, and ImageDevice, which are used to generate PDF, XPS, DOCX, and Image file formats, respectively.
The rendering devices in Aspose.HTML API are PdfDevice, XpsDevice, DocDevice, and ImageDevice. These are classes with constructors, properties, and methods that represent rendering HTML to PDF, XPS, DOCX, and image documents, respectively. Every rendering device has its own unique set of options implemented with classes PdfRenderingOptions, XpsRenderingOptions, DocRenderingOptions, and ImageRenderingOptions respectively.
Our C# library offers a set of namespaces with classes, methods, and properties to render HTML to output formats. Let’s consider them in more detail.
PdfDevice
The Aspose.Html.Rendering.Pdf namespace provides a specific PdfDevice class as well as a few rendering options classes responsible for rendering to a PDF document. The PdfDevice class implements the IDevice interface, which defines the basic functionality for rendering an HTML document.
The next C# example shows how to use PdfDevice to render HTML to PDF. The rendering process is considered with default rendering options:
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Rendering.Pdf;
4...
5 // Prepare HTML code
6 var code = @"<span>Hello World!!</span>";
7
8 // Prepare a path to save a converted file
9 string savePath = Path.Combine(OutputDir, "document.pdf");
10
11 // Initialize an HTML document from the HTML code
12 using (var document = new HTMLDocument(code, "."))
13 {
14 // Create a PDF Device and specify the output file to render
15 using (var device = new PdfDevice(savePath))
16 {
17 // Render HTML to PDF
18 document.RenderTo(device);
19 }
20 }
Let’s explain the C# code snippet above:
- First, we create an HTML document from a string of code. So, in the
code
variable, we put thecontent
that will be converted to PDF. - Then, we prepare a path for converted file saving –
savePath
. - Using the
HTMLDocument(
content
,baseUri
) constructor, we create an instance of HTMLDocument. - We create a rendering device – an instance of the
PdfDevice class. For this, we use PdfDevice(
savePath
) constructor. - And finally, we call
RenderTo(
device
) method with the specified device.
Rendering options give you additional control over the rendering process. To learn more about them, please read the Rendering options article.
If you are interested in how to use rendering options to resize document pages to the size of the content and vice versa, please visit the article How to Resize Document During Conversion from HTML?
ImageDevice
The Aspose.Html.Rendering.Image namespace provides a specific ImageDevice class as well as a few rendering options classes responsible for rendering HTML files to raster formats: JPG, PNG, BMP, GIF, and TIFF.
In the С# example, to render HTML to JPG image, we follow several steps:
- Load a source file from a local file system using
HTMLDocument(
documentPath
) constructor. - Create an instance of the
ImageRenderingOptions class with
ImageFormat
specifying. By default, the image format is PNG. - Create a rendering device – an instance of the
ImageDevice class. Use
ImageDevice(
imageOptions
,savePath
) constructor that takes rendering options and output file path as parameters. - Render HTML to JPG image using
RenderTo(
device
) method. This method takes an instance of the ImageDevice class as a parameter.
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Rendering.Image;
4...
5 // Prepare path to a source HTML file
6 string documentPath = Path.Combine(DataDir, "spring.html");
7
8 // Prepare path for converted file saving
9 string savePath = Path.Combine(OutputDir, "spring-output.jpg");
10
11 // Initialize an HTML document from the file
12 using var document = new HTMLDocument(documentPath);
13
14 // Create an instance of the ImageRenderingOptions class
15 var imageOptions = new ImageRenderingOptions(ImageFormat.Jpeg);
16
17 // Create an instance of the ImageDevice
18 using var device = new ImageDevice(imageOptions, savePath);
19
20 // Render HTML to JPG
21 document.RenderTo(device);
XpsDevice
The Aspose.Html.Rendering.Xps namespace provides a specific XpsDevice class for rendering HTML files to an XPS document. In the following C# example, the rendering process is considered with default rendering options:
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Rendering.Xps;
4...
5 // Prepare path to a source HTML file
6 string documentPath = Path.Combine(DataDir, "spring.html");
7
8 // Prepare path for converted file saving
9 string savePath = Path.Combine(OutputDir, "spring.xps");
10
11 // Initialize an HTML document from the file
12 using var document = new HTMLDocument(documentPath);
13
14 // Create the XPS Device and specify the output file to render
15 using var device = new XpsDevice(savePath);
16
17 // Render HTML to XPS
18 document.RenderTo(device);
DocDevice
The Aspose.Html.Rendering.Doc namespace provides a specific DocDevice class for rendering HTML files to DOCX format. In the following C# example, the rendering process is considered with default rendering options:
1using System.IO;
2using Aspose.Html;
3using Aspose.Html.Rendering.Doc;
4...
5 // Prepare path for converted file saving
6 string savePath = Path.Combine(OutputDir, "document.docx");
7
8 // Load a document from 'https://docs.aspose.com/html/net/creating-a-document/document.html' web page
9 using var document = new HTMLDocument("https://docs.aspose.com/html/net/creating-a-document/document.html");
10
11 // Create the DOC Device and specify the output file to render
12 using var device = new DocDevice(savePath);
13
14 // Render HTML to DOCX
15 document.RenderTo(device);
You can download the complete C# examples and data files from GitHub.
Aspose.HTML offers free online Converters that can convert HTML, XHTML, MHTML, EPUB, XML, and Markdown files to a range of popular formats. You can easily convert your HTML-based documents to PDF, XPS, DOCX, JPG, PNG, GIF, TIFF, and others. Just select a file, choose the format to convert, and you’re done. Best of all, it’s completely free!