Renderers | C#

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.
 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Rendering;
 4using Aspose.Html.Rendering.Pdf;
 5using Aspose.Html.Rendering.Pdf.Encryption;
 6...
 7    // Prepare a path to a source HTML file
 8    string documentPath = Path.Combine(DataDir, "file.html");
 9
10    // Initialize an HTML document from the file
11    using var document = new HTMLDocument(documentPath);
12
13    // Create an instance of HTML Renderer
14    using var renderer = new HtmlRenderer();
15
16    // Prepare a path to save the converted file 
17    string savePath = Path.Combine(OutputDir, "convert-html-options.pdf");
18
19    // Create the instance of the PdfRenderingOptions and set a custom page size and encryption
20    var options = new PdfRenderingOptions();
21    options.PageSetup.AnyPage = new Page(new Size(600, 200));
22    options.Encryption = new PdfEncryptionInfo(
23            "user_pwd",
24            "owner_pwd",
25            PdfPermissions.PrintDocument,
26            PdfEncryptionAlgorithm.RC4_128);
27
28    // Create an instance of PDF device
29    using var device = new PdfDevice(options, savePath);
30
31    // Render HTML to PDF
32    renderer.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.
 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Rendering;
 4using Aspose.Html.Rendering.Pdf;
 5using Aspose.Html.Rendering.Pdf.Encryption;
 6...
 7    // Initialize an SVG document from the file
 8    using var document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));    
 9
10    // Create an instance of SVG Renderer
11    using var renderer = new SvgRenderer();
12
13    // Prepare a path to save the converted file 
14    string savePath = Path.Combine(OutputDir, "merge-svg.pdf");
15
16    // Create the instance of the PdfRenderingOptions class and set a custom page size
17    var options = new PdfRenderingOptions();
18    options.PageSetup.AnyPage = new Page(new Size(600, 500));            
19
20    // Create an instance of PdfDevice class
21    using var device = new PdfDevice(options, savePath);
22
23    // Render SVG to PDF
24    renderer.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.
 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Rendering;
 4using Aspose.Html.Rendering.Pdf;
 5...
 6    // Open an existing MHTML file for reading
 7    using var stream = File.OpenRead(DataDir + "sample.mht");
 8
 9    // Create an instance of MHTML Renderer
10    using var renderer = new MhtmlRenderer();
11
12    // Prepare a path to save the converted file 
13    string savePath = Path.Combine(OutputDir, "convert-mhtml-options.pdf");
14
15    // Create the instance of Rendering Options and set a custom page size and background color
16    var options = new PdfRenderingOptions();
17    options.PageSetup.AnyPage = new Page(new Size(600, 200));
18    options.BackgroundColor = System.Drawing.Color.Azure;
19
20    // Create an instance of PDF device
21    using var device = new PdfDevice(options, savePath);
22
23    // Render MHTML to PDF
24    renderer.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.
 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Rendering;
 4using Aspose.Html.Rendering.Doc;
 5...
 6    // Open an existing EPUB file for reading
 7    using var stream = File.OpenRead(DataDir + "input.epub");
 8
 9    // Create an instance of EPUB Renderer
10    using var renderer = new EpubRenderer();
11
12    // Prepare a path to save the converted file 
13    string savePath = Path.Combine(OutputDir, "convert-epub-options.docx");
14
15    // Create the instance of the DocRenderingOptions class and set a custom page size
16    var options = new DocRenderingOptions();
17    options.PageSetup.AnyPage = new Page(new Size(800, 400));            
18
19    // Create an instance of the DocDevice class
20    using var device = new DocDevice(options, savePath);
21
22    // Render EPUB to DOCX
23    renderer.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:

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

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Rendering;
 4using Aspose.Html.Rendering.Pdf;
 5...
 6    // Prepare HTML code
 7    var code = @"
 8    <script>
 9        var count = 0;
10        setInterval(function()
11            {
12                var element = document.createElement('div');
13                var message = (++count) + '. ' + 'Hello, World!! I know how to use Renderers!';
14                var text = document.createTextNode(message);
15                element.appendChild(text);
16                document.body.appendChild(element);
17            }, 1000);
18    </script>";
19
20    // Initialize an HTML document based on prepared HTML code
21    using var document = new HTMLDocument(code, ".");
22
23    // Create an instance of HTML Renderer
24    using HtmlRenderer renderer = new HtmlRenderer();
25
26    // Prepare a path to save the converted file 
27    string savePath = Path.Combine(OutputDir, "set-timeout.pdf");
28
29    // Create an instance of PDF device
30    using var device = new PdfDevice(savePath);
31                    
32    // Render HTML to PDF and set timeout
33    renderer.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 “Banner Free Online Converters”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.