Create PDF in C# [HTML to PDF using Spire.PDF ]

 Create PDF: This article explains how to create PDF in C#. Creating pdf document is a very common task in every web application. And  in .NET framework there is no natural way to work with PDF files .i.e (create, read, edit PDF file).

While working on my previous project, I was looking for an Advance Tool, which creates complex PDF Report in C#.Net and then I found, so to work with PDF file in our Web application we are going to use Spire.PDF.


Spire.PDF is a PDF component which contains an incredible wealth of features to create, read, edit and manipulate PDF documents on .NET, Silverlight and WPF Platform.As an independent PDF library, it does not need users to install Adobe Acrobat or any other third party libraries.

Spire.PDF for .NET is completely written in C# but also supports VB.NET, Windows Forms, and ASP.NET Applications. Using Spire.PDF, we can create PDF file programmatically from C# applications very quickly.


Create PDF files in c#
Steps to create PDF in C#.

  1. Add HTML Markup under Panel Control.
  2. Download Spire.PDF DLL.
  3. Initialize PDF instance along page setting.
  4. Fetch HTML and generate PDF.

# Add HTML  markup under Panel Control:

Here we have a Web Page (.aspx) with some HTML content and by using Spire.PDF will generate PDF i.e (converting HTML to PDF)  we can also convert Gridview to PDF in C#.

But to create PDF for the particular part of our webpage, here we use Panel control. i.e place our HTML tag inside Panel control.

This is how our web page markup looks like containing an h1 and p tag under Panel Control.

<asp:Panel ID="Panel1" runat="server">
    <h2>Welcome to</h2>
    <p> is a programming blog .....
        ........                l
        PDF is generated by using Spire.PDF in C#
<asp:Button ID="btnHtml2PDF" runat="server" Text="Create Pdf" OnClick="btnHtml2PDF_Click" />

We are done with our markup for which we want to create PDF. Now will download Spire.PDF.

# Download Spire.PDF:

Downloading Spire.pdf DLL is very easy, you can download direct from the Spire.PDF official website for the Free Version and for the Pro Version, or you can also download from NuGet.

Make sure after downloading DLL import it into your Web Application Bin folder. Now we are done with downloading, Let’s head up to server-side coding (C#).

# Initialize PDF instance and page setting:

Yeah, now we are in the main part of this article.

First, we need to add some namespace in our Webpage (Default.aspx). Code looks like as written below.

using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System.Text;
using System.Threading;
using System.IO;

Now the following code shows how to create a  new instance of PDF document.

PdfDocument pdf = new PdfDocument();

Also, set the page layout and some page setting. Here we make our PDF file page size as  A4.

//  Set the layout and page setting
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
//webBrowser load html whether Waiting
htmlLayoutFormat.IsWaiting = false;
//page setting
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;

We are done importing required namespace and with page setting now it’s time to create pdf with some content.

# Fetch HTML and generate PDF:

Here first, we need to get innerHTML of Panel control.

There is no direct way to get innerHTML of Panel control, but with little tweak .i.e using RenderControl will get inner HTML of it.

Following code shows how to fetch innerHTML of Panel Control and then generate PDF document in C# .i.e (HTML to PDF ).

//Get innerHtml of Panel Control 
var sb_htmlCode = new StringBuilder();
Panel1.RenderControl(new HtmlTextWriter(new StringWriter(sb_htmlCode)));
string htmlCode = sb_htmlCode.ToString();

//use single thread to generate the pdf from above html code
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });

// Save the file to PDF and preview it.

Note: You can provide any name of the generated file. For example here I provided output.pdf as my generated pdf file.


Finally, we are done with pdf creation using Spire.PDF  DLL.

Create PDF files in c#


Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!

Sharing is Sexy:    
Satinder Singh

Hi all, am Founder and Author of, also atypical polyglot programmer from Bombay, India. I have a passion to create, solve, and deploy software applications.
       ForEach ( minute in MyLife ) { myExperience++ ; }

4 comments on “Create PDF in C# [HTML to PDF using Spire.PDF ]”

Post Comment

Your email address will not be published. Required fields are marked *