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 for .NET 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:

 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);

Let’s explain the C# code snippet above:

  1. First, we create an HTML document from a string of code. So, in the code variable, we put the content that will be converted to PDF.
  2. Then, we prepare a path for converted file saving – savePath.
  3. Using the HTMLDocument(content, baseUri) constructor, we create an instance of HTMLDocument.
  4. We create a rendering device – an instance of the PdfDevice class. For this, we use PdfDevice(savePath) constructor.
  5. 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:

  1. Load a source file from a local file system using HTMLDocument(documentPath) constructor.
  2. Create an instance of the ImageRenderingOptions class with ImageFormat specifying. By default, the image format is PNG.
  3. 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.
  4. Render HTML to JPG image using RenderTo(device) method. This method takes an instance of the ImageDevice class as a parameter.
 1// Prepare path to a source HTML file
 2string documentPath = Path.Combine(DataDir, "spring.html");
 3
 4// Prepare a path to save the converted file
 5string savePath = Path.Combine(OutputDir, "spring-output.jpg");
 6
 7// Initialize an HTML document from the file
 8using var document = new HTMLDocument(documentPath);
 9
10// Create an instance of the ImageRenderingOptions class
11var imageOptions = new ImageRenderingOptions(ImageFormat.Jpeg);
12
13// Create the Image Device and specify the output file to render
14using var device = new ImageDevice(imageOptions, savePath);
15
16// Render HTML to JPG
17document.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:

 1// Prepare path to a source HTML file
 2string documentPath = Path.Combine(DataDir, "spring.html");
 3
 4// Prepare a path to save the converted file
 5string savePath = Path.Combine(OutputDir, "spring.xps");
 6
 7// Initialize an HTML document from the file
 8using var document = new HTMLDocument(documentPath);            
 9
10// Create an instance of the XpsDevice and specify the output file to render
11using var device = new XpsDevice(savePath);
12
13// Render HTML to XPS
14document.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:

 1// Prepare a path to save the converted file
 2string savePath = Path.Combine(OutputDir, "document.docx");
 3
 4// Load a document from 'https://docs.aspose.com/html/net/creating-a-document/document.html' web page
 5using var document = new HTMLDocument("https://docs.aspose.com/html/net/creating-a-document/document.html");
 6
 7// Create an instance of the DocRenderingOptions class
 8var docOptions = new DocRenderingOptions();
 9
10// Create the DocDevice object and specify the output file to render
11using var device = new DocDevice(docOptions, savePath);
12
13// Render HTML to DOCX
14document.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!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.