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

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

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


Spire.PDF is a PDF component that 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 files programmatically from C# applications very quickly.


Steps to create PDF in C#.

  1. Add HTML Markup under Panel Control.
  2. Download Spire.PDF DLL.
  3. Initialize PDF instance along with 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 a 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 to 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 settings. 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 the required namespace and with page setting now it's time to create a 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.

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.

Post Comment

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