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

/ Updated : / 0 Comments
C# generate pdf: This article explains how to create PDF in Asp.net 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 Spire.net, so to work with PDF file in our Asp.net Web application we are going to use Spire.PDF.

Intro:

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.

Screenshot:

Create PDF files in asp.net c#


Steps to create PDF in Asp.net 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 Asp.net C#.

But to create PDF for the particular part of our webpage, here we use Asp.net Panel control. i.e place our HTML tag inside Asp.net 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 Codepedia.info</h2>
    <p>
        Codepedia.info is a programming blog .....
        ........                l
        </p>
    <p>
        PDF is generated by using Spire.PDF in Asp.net C#
    </p>
</asp:Panel>
<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 Asp.net Panel control.

There is no direct way to get innerHTML of Asp.net 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 Asp.net 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); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

// Save the file to PDF and preview it.
pdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");
//*
Note: You can provide any name of the generated file. For example here I provided output.pdf as my generated pdf file.

Output:

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

Create PDF files in asp.net c#

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 do a favor, then 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 *

Comments

  1. Adarsh 10/14/2015 10:10:52

    Good Job my friend. I like the way growing ur experience... :-) Thanks for the post.

  2. Satinder singh 10/18/2015 09:17:40

    Thank for reading Adarsh.

  3. Anjan Kant 05/05/2016 04:37:37

    Create PDF in Asp.net C# with spire explained very well.

  4. درب ضد سرقت 05/28/2016 12:12:34

    tanks alot very good way for converting html pages with unicode characteres like Persian to pdf

  5. Fred Green 04/12/2018 08:13:02

    Wow, I saw a lot of posts about iTextSharp, but it is my first time to see a post about Spire.PDF.

  6. درب ضد سرقت 04/25/2019 08:32:00

    good job. This article was very helpful to me. thank you