C# .NET - How to create PDF file from dataset in ASP.net

Asked By Abhi Rana on 27-Oct-09 01:36 AM

I want to design ASP.net that could generate PDF file from the Dataset.

How to do it.

Use crystal report

Or

PDF dlls

Please provide me code snippets



Re

Huggy Bear replied to Abhi Rana on 27-Oct-09 01:51 AM
You can use a open source dll called iTextSharp.dll to work with pdf files in .net and java.

You can download it from 
http://sourceforge.net/projects/itextsharp/

You can get the tutorials at
http://itextsharp.sourceforge.net/tutorial/index.html

reply

Deepak Sonawane replied to Abhi Rana on 27-Oct-09 01:57 AM
Hi,
You can explore either of these 2 free & open source libraries  to generate PDFs on the fly:
PDFSharp - http://www.pdfsharp.com/PDFsharp/index.php?option=com_frontpage&Itemid=1
iText#  - http://sourceforge.net/projects/itextsharp/

For a code sample on exporting data to PDF using iTextSharp, check this -
http://www.ronosaurus.com/opensource/datagridexporter/

reply

Deepak Sonawane replied to Abhi Rana on 27-Oct-09 01:58 AM

Hi,

Also try this:

u can create pdf and open is in different way which u wnat to do

go for PDFSharp for creating pdf files

http://pdfsharp.com/PDFsharp/

or

this thread give u full info for this

http://www.eggheadcafe.com/community/aspnet/2/10090000/welcome.aspx

or

use this open source c# libraries

http://csharp-source.net/open-source/pdf-libraries

`create PDF with iTextSharp
mv ark replied to Abhi Rana on 27-Oct-09 02:01 AM
If you just have to generate PDF files,you can explore either of these 2 free & open source libraries  to generate PDFs on the fly:
PDFSharp - http://www.pdfsharp.com/PDFsharp/index.php?option=com_frontpage&Itemid=1
iText#  - http://sourceforge.net/projects/itextsharp/

Comparatively, Crystal Reports consumes more resources & requires a license

You can find a snippet to create PDF with iTextSharp here - http://www.martinwilley.com/net/code/createpdf.html

create PDF file from dataset in ASP.net
Sagar P replied to Abhi Rana on 27-Oct-09 02:03 AM

protected void ExportToPDF(object sender, EventArgs e)

{

    //Get the data from database into datatable

    string strQuery = "select CustomerID, ContactName, City, PostalCode" +

        " from customers";

    SqlCommand cmd = new SqlCommand(strQuery);

    DataTable dt = GetData(cmd);

    //Create a dummy GridView

    GridView GridView1 = new GridView();

    GridView1.AllowPaging = false;

    GridView1.DataSource = dt;

    GridView1.DataBind();

    Response.ContentType = "application/pdf";

    Response.AddHeader("content-disposition",

        "attachment;filename=DataTable.pdf");

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw = new StringWriter();

    HtmlTextWriter hw = new HtmlTextWriter(sw);

    GridView1.RenderControl(hw);

    StringReader sr = new StringReader(sw.ToString());

    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End(); 

}

http://www.aspsnippets.com/post/2009/05/20/Export-DataSet-or-DataTable-to-Word-Excel-PDF-and-CSV-Formats.aspx

Also to export dataset to PDF. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly. It can be downloaded from here - http://sourceforge.net/projects/itextsharp/

There is a detailed code sample related to your requirement here -
http://www.tanguay.info/web/codeExample.php?id=656

You can find a tutorial on iText# -
http://itextsharp.sourceforge.net/tutorial/index.html

See this Thread as well;

http://www.eggheadcafe.com/community/aspnet/2/10041208/export-dataset-to-pdf-in.aspx

Unable to use HTMLWorker and PDFWriter
Abhi Rana replied to Sagar P on 27-Oct-09 02:54 AM
I have use

using iTextSharp;

using iTextSharp.text;

What Im missing here......

Thanks alot
Abhi Rana replied to Sagar P on 27-Oct-09 04:34 AM

Your post really help me alot.

Now I could convert dataset to pdf

But Now I want to convert web page into pdf

that includes Labels, Gridview

suchita replied to Sagar P on 01-Sep-10 03:47 PM
can you give the page header in each page using same version of iTextSharp?