DEV Community

Andriy Andruhovski
Andriy Andruhovski

Posted on • Updated on

How to render HTML to PDF document using Aspose.HTML for .NET

In this article, we'll look at examples of how to render HTML to PDF documents using the Aspose.HTML library

First, we need to make some initial steps to prepare example project: create a Console Application project, add Aspose.HTML library, create an HTMLDocument object, and generate (or load) HTML content.

Let's try to render PDF document with default rendering options and file to store output. In this example we use following classes:

So, to render PDF document we need to create instances of those and run the rendering process.

We've generated a simple PDF document here, but that's not very practical. In reality we'll often have requirements about paper size, page orientation, margins, etc. There are number of configuration options that we can set to make PdfDevice more suitable for our real-world needs. All of them are listed and explained below:

  • Encryption - gets or sets a encryption details. If not set, then no encryption will be performed;
  • JpegQuality - specifies the quality of JPEG compression for images (if JPEG compression is used);
  • PageSetup - gets a page setup object is used for configuration output page-set.

Now let's talk about advanced settings PdfRenderingOptions. Assume, we need to get the document with paper size A3 and margins: top – 20mm, bottom – 15mm, left/right – 15mm and these settings will be applied for any page. In order to setup page settings and margins for any pages, we'll use PageSetup.AnyPage property. This property is the instance of Page class. To define the page layout we need to set up Size and Margin. In both cases we'll use Unit class to get appropriate measurement units. Following example shows how to set up page settings to any page:

The PdfRenderingOptions also offers the feature to set mirror pages and their rendering. When using this approach, we need to specify the Page settings for both Left and Right pages. Note, that values are set simultaneously for the left and right pages by calling SetLeftRightPage method.

The last feature of PdfRenderingOptions, described in this article, is the FirstPage property. This property is used to set the cover page configuration and the values of this property are set similarly to AnyPage.

Top comments (0)