0

I need to have a functionality that on click of a button i need to generate pdf file for each record in the data table using C#.net

I require to generate PDF for each and then Email attachment that PDF to each person. the data for PDF is fetched into a data table for all students.

I just need a functionality that i can generate individual pdf for each row from data table and send email attachment.

Can anyone please help me and tell me any other packages are available in C#.net.

I have tried this way but does not work and this working for one row in the data table after that its not returning next loop to generate pdf, please look at the following code.

StringBuilder sb = new StringBuilder();
string companyName = "ASPSnippets";
int orderNo = 2303;
protected void BTNsubmint_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("server=ServerName; database=Dbname; Integrated Security=True");
    SqlDataAdapter da = new SqlDataAdapter("select * from productss", con);
    DataTable dt = new DataTable();          
    da.Fill(dt);
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {

            //Generate Invoice (Bill) Header.
            sb.Append("<table width='100%' cellspacing='0' cellpadding='2'>");
            sb.Append("<tr><td align='center' style='background-color: #18B5F0' colspan = '2'><b>Order Sheet</b></td></tr>");
            sb.Append("<tr><td colspan = '2'></td></tr>");
            sb.Append("<tr><td><b>Order No: </b>");
            sb.Append(orderNo);
            sb.Append("</td><td align = 'right'><b>Date: </b>");
            sb.Append(DateTime.Now);
            sb.Append(" </td></tr>");
            sb.Append("<tr><td colspan = '2'><b>Company Name: </b>");
            sb.Append(companyName);
            sb.Append("</td></tr>");
            sb.Append("</table>");
            sb.Append("<br />");

            //Generate Invoice (Bill) Items Grid.
            sb.Append("<table border = '1'>");
            sb.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                sb.Append("<th>");
                sb.Append(column.ColumnName);
                sb.Append("</th>");
            }
            sb.Append("</tr>");
            foreach (DataRow row in dt.Rows)
            {
                sb.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    sb.Append("<td>");
                    sb.Append(row[column]);
                    sb.Append("</td>");
                }

                sb.Append("</tr>");
                sb.Append("</table>");

                StringReader sr = new StringReader(sb.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();
                Response.ContentType = "application/pdf";
                Response.AddHeader("content-disposition", "attachment;filename=Invoice_" + orderNo + ".pdf");
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Write(pdfDoc);
                Response.End();
        }
    }
}
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
venkatesh
  • 5
  • 4
  • `this working for one row in the data table` - does the DataTable have more than one rows (while debugging) ? Form your code it is not very logical that you want to create multiple PDF, you are appending individual rows over and over to the same `StringBuilder`. – Vojtěch Dohnal Mar 08 '16 at 07:25
  • Its working first row in the data table and generating pdf , but data table has may rows and have generate different pdf files for each row of data table . but my code is working for only one row , could not looping for next row to generate pdf. can you please suggest any other way – venkatesh Mar 08 '16 at 07:33
  • You should generate all pdf files and put them in one zip file for download. You could also write the pdf files to your hard disk and offer a static link for each. – fuchs777 Mar 08 '16 at 07:45
  • `could not looping for next row` - are you actually debugging the code? – Vojtěch Dohnal Mar 08 '16 at 07:48

1 Answers1

0

this working for one row in the data table

The problem is Response.End() command - it quits your script, so second and next rows do not get processed.

See this question: Convert HTML to PDF in .NET to rewrite your code without Response.End. Debug your code to see, where the problem actually is.

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
  • I agree with you Vojtěch Dohnal , I have debug my code due to Response.End() command , i did not processed for second row, can you help me any other functionality to achieve this. – venkatesh Mar 08 '16 at 08:13