Click here to Skip to main content
15,068,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

i was trying to export repeater data to pdf in asp.net using c#,

where in a repeater i have web user controls also.

to export this, i'm using itextshare.dll

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text;


protected void btnExport_Click(object sender, ImageClickEventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            DailyReportRepeater.DataBind();
            DailyReportRepeater.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();//the doc has no pages... Error
            Response.Write(pdfDoc);
            Response.End(); 
        }



trying:
i have already bind the repeater in the pageload, if(!ispostback).

....
in btnexport code:

i have commented //  DailyReportRepeater.DataBind();
n then checked the repeater count n it was greater than zero

            int c = DailyReportRepeater.Items.Count;
,
but if i uncommented this line ' DailyReportRepeater.DataBind();'
then the count is getting zero.

the problem is in binding of a repeater.


can anyone please help me to solve this issue.


Thanks
Posted
Updated 4-Sep-14 2:28am
v4

1 solution

Hi
Try this,

Response.ContentType = "application/pdf";

Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");

Response.Cache.SetCacheability(HttpCacheability.NoCache);

StringWriter sw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(sw);

DailyReportRepeater.DataBind();

DailyReportRepeater.RenderControl(hw);

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

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

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

pdfDoc.Open();

htmlparser.Parse(sr);

pdfDoc.Close();//the doc has no pages... Error

Response.Write(pdfDoc);

Here is code I have used to write out a table if it helps

iTextSharp.text.Document doc = new iTextSharp.text.Document();

iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(System.Web.HttpContext.Current.Server.MapPath("\\") + "file.pdf", FileMode.Create));

doc.Open();

iTextSharp.text.pdf.PdfPTable Table = new iTextSharp.text.pdf.PdfPTable(2);
Table.DefaultCell.Border = 0;

doc.Add(Table);

doc.Close();
   
Comments
Shrikesh_kale 16-Feb-15 7:42am
   
not working Give same error at doc.close();
Member 11770023 21-Jun-15 9:14am
   
you must be load data at page load event of btnexport page,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900