Export Data To PDF Using Asp.net C# (iTextSharp)

Posted: July 22, 2013 in ASP.net
Sometimes we require to export our data to pdf file. I am explaining you how we can do this using C# . We are here using ITextSharp

I`ll Give you A Simple Example How You Can Do This..

First we need to add iTextShap dll. To Download dll Click Here .

______________________________________________________________________________________

Now in code behind add these references

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;

Now i will explain it with 2 approaches :

1. Put string in pdf file.

2.Put HTML from Web Page .

______________________________________________________________________________________

1st Method  . You Have To Write this code in your .cs file

private void Export_to_Pdf()

{

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str = “<h1 title=’Header’ align=’Center’> Writing To PDF Using ASP.NET</h1> <br><table align=’Center’><tr><td style=’width:100px;color:green’> <b>iTextSharp</b></td><td style=’width:100px;color:red’>dotnetgeekblog</td></tr></table>”;
StringReader sr = new StringReader(str);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}

_______________________________________________________________________________________

______________________________________________________________________________________

2nd Method  . You Have To Write this code in your .cs file

private void Export_to_Pdf()

{

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}

_______________________________________________________________________________________

This will convert your string into pdf. You can download it and Save it.

This Is Basic , Now You Can Modify This Code as You Want 🙂

Hope This Post Will Help you Somehow . . :) . Feel Free To Ask Anything You Need.

Waiting For Your Valuable Feedback.

blog

Advertisements
Comments
  1. anand somaiya says:

    Thanks a Lot…..

  2. saya says:

    td width is not working while export to pdf in both the methods. I have given different width for table cells, but after export to pdf not reflecting width.
    Can you please check by giving different width?

    Regards,
    Saya

  3. kalai says:

    can u pls explain page size parameter????

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s