Renderers – C# – Aspose.HTML for .NET

Renderers

The Aspose.Html.Rendering namespace consists of numerous renderer objects and appropriate low-level options classes, which are responsible for rendering documents into IDevice implementation. Aspose.HTML for .NET API provides the following realization of renderers: HtmlRenderer, SvgRenderer, MhtmlRenderer, and EpubRenderer, which are used to render HTML, SVG, MHTML, and EPUB documents, respectively.

While the RenderTo() method of the Document class gives you the ability to send a single document to the output rendering device, using the Renderer instances directly, you can send multiple files at once. So, you can merge HTML, MHTML, EPUB, and SVG documents and apply rendering options to turn the output file.

The current article describes supported scenarios of HTML-based file conversion & merging to other popular formats using Render() methods of the Renderer class.

HtmlRenderer

Usage of the HtmlRenderer class to render HTML files to other popular formats – is one more way to convert HTML by customizing the rendering options and controlling the rendering process’s output. Let’s look at using the HtmlRenderer class to render HTML to PDF with custom rendering options:

  1. Initialize an HTML document. Use one of the HTMLDocument() constructors to create an HTMLDocument instance.
  2. Create a new HtmlRenderer object.
  3. Create the instance of the PdfRenderingOptions and set custom options for the output PDF document. In the example, we specify the AnyPage property of the PageSetup class that sets a new Page object with a page size of 600 pixels by 200 pixels and the Encryption property that identifies the user and owner passwords, allowed permissions, and encryption algorithm for the output PDF file.
  4. Use PdfDevice(options, savePath) constructor to create an object of the PdfDevice class.
  5. And finally, call the Render(device, document) method to render the HTML document to the output PDF file with the specified rendering options.
 1// Prepare a path to a source HTML file
 2string documentPath = Path.Combine(DataDir, "file.html");
 3
 4// Initialize an HTML document from the file
 5using var document = new HTMLDocument(documentPath);
 6
 7// Create an instance of HTML Renderer
 8using var renderer = new HtmlRenderer();
 9
10// Prepare a path to save the converted file 
11string savePath = Path.Combine(OutputDir, "convert-html-options.pdf");
12
13// Create the instance of Rendering Options and set a custom page-size
14var options = new PdfRenderingOptions();
15options.PageSetup.AnyPage = new Page(new Size(600, 200));
16options.Encryption = new PdfEncryptionInfo(
17       "user_pwd",
18       "owner_pwd",
19       PdfPermissions.PrintDocument,
20       PdfEncryptionAlgorithm.RC4_128);
21
22// Create an instance of PDF device
23using var device = new PdfDevice(options, savePath);
24
25// Render HTML to PDF
26renderer.Render(device, document);

Rendering options give you additional control over the rendering process. To learn more about them, please read the Rendering options article.

To learn more about rendering process, please read the Rendering Device 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?

SvgRenderer

The SvgRenderer allows you to render SVG files to other file formats, such as PDF, XPS, DOCX, and image formats. With this, you can customize the output file using various rendering options. The following example demonstrates how to use SvgRenderer class to render SVG to PDF with a custom page size specifying:

  1. Initialize an SVG document. Use one of the SVGDocument() constructors to create an SVGDocument instance.
  2. Create a new SvgRenderer object.
  3. Create an instance of the PdfRenderingOptions and set custom options for the output PDF document. In the example, we specify the PageSetup.AnyPage property that sets a new Page object with a page size of 600x500 pixels.
  4. Use the PdfDevice(options, savePath) constructor to create an object of the PdfDevice class.
  5. And finally, call the Render(device, document) method to render the SVG document to the output PDF file with the specified rendering options.
 1// Initialize an SVG document from the file
 2using var document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));           
 3
 4// Create an instance of SVG Renderer
 5using var renderer = new SvgRenderer();
 6
 7// Prepare a path to save the converted file 
 8string savePath = Path.Combine(OutputDir, "rendering-svg.pdf");
 9
10// Create the instance of Rendering Options and set a custom page-size
11var options = new PdfRenderingOptions();
12options.PageSetup.AnyPage = new Page(new Size(600, 500));
13
14// Create an instance of the Pdfdevice class
15using var device = new PdfDevice(options, savePath);
16
17// Merge all SVG documents into PDF
18renderer.Render(device, document);

MhtmlRenderer

The following example demonstrates how to convert MHTML to PDF with the rendering options specifying using the MhtmlRenderer class:

  1. Open an existing MHTML document.
  2. Create an instance of MhtmlRenderer using the MhtmlRenderer() constructor.
  3. Initialize the PdfRenderingOptions class and set rendering options.
  4. Create an instance of the PdfDevice class.
  5. Call the Render(device, stream) method to render MHTML to PDF.
 1// Open an existing MHTML file for reading
 2using var stream = File.OpenRead(DataDir + "sample.mht");
 3
 4// Create an instance of MHTML Renderer
 5using var renderer = new MhtmlRenderer();
 6
 7// Prepare a path to save the converted file 
 8string savePath = Path.Combine(OutputDir, "convert-mhtml-options.pdf");
 9
10// Create the instance of Rendering Options and set a custom page-size and background color
11var options = new PdfRenderingOptions();
12options.PageSetup.AnyPage = new Page(new Size(600, 200));
13options.BackgroundColor = System.Drawing.Color.Azure;
14
15// Create an instance of PDF device
16using var device = new PdfDevice(options, savePath);
17
18// Convert MHTML to PDF
19renderer.Render(device, stream);

EpubRenderer

The EpubRenderer class allows you to convert EPUB files to other formats such as PDF, XPS, DOCX, and images. The following C# examples shows how to convert EPUB to DOCX with a custom page size setting:

  1. Open an existing EPUB file.
  2. Create an instance of EpubRenderer class.
  3. Initialize the DocRenderingOptions class and set rendering options.
  4. Create an instance of the DocDevice class.
  5. Call the Render(device, stream) method to render EPUB to DOCX.
 1// Open an existing EPUB file for reading
 2using var stream = File.OpenRead(DataDir + "input.epub");
 3
 4// Create an instance of EPUB Renderer
 5using var renderer = new EpubRenderer();
 6
 7// Prepare a path to save the converted file 
 8string savePath = Path.Combine(OutputDir, "convert-epub-options.docx");
 9
10// Create the instance of Rendering Options and set a custom page-size
11var options = new DocRenderingOptions();
12options.PageSetup.AnyPage = new Page(new Size(800, 400));            
13
14// Create an instance of the DocDevice class
15using var device = new DocDevice(options, savePath);
16
17// Render EPUB to DOCX
18renderer.Render(device, stream);

Merge HTML

Aspose.HTML for .NET API provides the Renderer class for rendering and merging HTML, MHTML, EPUB, and SVG documents to popular formats. While the RenderTo() method gives you the ability to send a single document to the output rendering device, using the Renderer instances directly, you can send multiple files at once. Using the implementation of renderers: HtmlRenderer, SvgRenderer, MhtmlRenderer, and EpubRenderer, you can merge HTML, SVG, MHTML, and EPUB documents, respectively. The next example demonstrates how to use HtmlRenderer to render multiple HTML documents:

 1// Prepare HTML code
 2var code1 = @"<br><span style='color: green'>Hello, World!!</span>";
 3var code2 = @"<br><span style='color: blue'>Hello, World!!</span>";
 4var code3 = @"<br><span style='color: red'>Hello, World!!</span>";
 5
 6// Create three HTML documents to merge later
 7using var document1 = new HTMLDocument(code1, ".");
 8using var document2 = new HTMLDocument(code2, ".");
 9using var document3 = new HTMLDocument(code3, ".");
10
11// Create an instance of HTML Renderer
12using var renderer = new HtmlRenderer();
13
14// Prepare a path to save the converted file 
15string savePath = Path.Combine(OutputDir, "merge-html.pdf");
16
17// Create an instance of PDF device
18using var device = new PdfDevice(savePath);
19        
20// Merge all HTML documents into PDF
21renderer.Render(device, document1, document2, document3);

Set Timeout

One more important feature that is available for renderers is timeout. You can use it to specify how long you are ready to wait for all internal processes related to a document lifecycle to be completed, such as resource loading, active timers, etc. Sure, you can specify an infinite waiting period. However, if the document contains a script with an endless loop, you will wait indefinitely. The example below demonstrates how to use the timeout parameter:

 1// Prepare HTML code
 2var code = @"
 3<script>
 4    var count = 0;
 5    setInterval(function()
 6        {
 7            var element = document.createElement('div');
 8            var message = (++count) + '. ' + 'Hello, World!! I know how to use Renderers!';
 9            var text = document.createTextNode(message);
10            element.appendChild(text);
11            document.body.appendChild(element);
12        }, 1000);
13</script>";
14
15// Initialize an HTML document based on prepared HTML code
16using var document = new HTMLDocument(code, ".");
17
18// Create an instance of HTML Renderer
19using HtmlRenderer renderer = new HtmlRenderer();
20
21// Prepare a path to save the converted file 
22string savePath = Path.Combine(OutputDir, "output-timeout.pdf");
23
24// Create an instance of the PdfDevice class
25using var device = new PdfDevice(savePath);
26        
27// Render HTML to PDF
28renderer.Render(device, TimeSpan.FromSeconds(5), document);

In this example, we create an HTML document that adds a new <div> element every second with a sequential number and the message “Hello, World!!” We use the TimeSpan parameter in the Render(device, timeout, document) method to set timeout. This setting specifies the maximum time the renderer will spend rendering the document. In this case, the total time is set to 5 seconds. If the renderer has not generated a document within the specified time, it will stop and render a partially rendered document. Running this example, you will get a document with five lines of “Hello World!! I know how to use Renderers!” message, as follows:

Five lines of ‘Hello, World!! I know how to use Renderers!’

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!

Text “Free Online Converters”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.