How to Merge HTML Files to PDF in C#

This basic tutorial focuses on how to merge HTML files to PDF in C#. It covers how to configure the library in your environment and the step-by-step process followed by a working sample code to combine HTML to PDF in C#. Moreover, you do not need to install any other tool or application to work with this feature.

Steps to Merge HTML Files to PDF in C#

  1. Install Aspose.HTML for .NET API to merge the HTML files
  2. Load multiple HTML files using a separate HTMLDocument class object for each file
  3. Create an HTMLRenderer class object
  4. Initialize a PdfDevice class instance
  5. Merge the source HTML files into one PDF document

These steps present the complete process to merge multiple HTML files into one PDF in C#. First of all, configure the system environment to process the HTML files. Then proceed to load the source HTML files and append them together as a PDF document.

Code to Merge HTML Files to PDF in C#

using Aspose.Html;
using Aspose.Html.Rendering;
using Aspose.Html.Rendering.Pdf;
namespace AsposeProjects
{
class Program
{
static void Main(string[] args) // Main function to merge HTML files in C#
{
// Initialize license
License lic = new License();
lic.SetLicense("Aspose.Total.lic");
// Load HTML documents to merge
using (var document1 = new HTMLDocument("page1.html"))
using (var document2 = new HTMLDocument("page2.html"))
using (var document3 = new HTMLDocument("page3.html"))
{
// Create HTML Renderer class object
using (HtmlRenderer renderer = new HtmlRenderer())
{
// Initialize PdfDevice
using (var device = new PdfDevice("Combine.pdf"))
{
// Merge HTML documents to PDF
renderer.Render(device, document1, document2, document3);
}
}
}
System.Console.WriteLine("Done");
}
}
}

The code snippet above is a bare-minimum sample to demonstrate the feature to combine multiple HTML files into one PDF in C#. Whereas, you can change the number of input files to any other number of files as required into a single PDF file. The Render() method has several overload constructors where different parameters like a maximum time limit or the list of input files can be specified as per your requirements.

This tutorial explains the details to merge HTML to PDF in C#. Besides, if you are interested in HTML to PDF conversion, you may take a look at how to convert HTML Page to PDF in C#.

 English