public void ProcessRequest(HttpContext context) { string tabData = context.Request["excelData"]; DataTable dt = ConvertCsvData(tabData); DataTable cr = ConvertCsvData(context.Request["criteriaArray"]); if (dt == null) { // Add some error-catching here... return; } //PDF Code int xy; //Create a dummy GridView GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); string title = context.Request["filename"]; System.Web.HttpContext.Current.Response.ContentType = "application/pdf"; System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + title + ".pdf"); System.Web.HttpContext.Current.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.Rotate(), 7f, 7f, 70f, 35f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream); //iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("http://10.65.216.71:5666/resources/report_images/header_logo.PNG"); //Header Call pdfPage page = new pdfPage(); //set the PageEvent of the pdfWriter instance to the instance of our PDFPage class //PdfWriter.PageEvent = page; writer.PageEvent = page; Paragraph para = new Paragraph(new Phrase(title + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 16f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.DARK_GRAY))); para.Alignment = Element.ALIGN_CENTER; Paragraph criteria = new Paragraph(context.Request["criteria"] + "\n\n", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 10f, iTextSharp.text.Font.TIMES_ROMAN, iTextSharp.text.Color.GRAY)); criteria.Alignment = Element.ALIGN_CENTER; int x = GridView1.Columns.Count; PdfPTable table = new PdfPTable(dt.Columns.Count); table.WidthPercentage = 95; /*PdfPCell cell = new PdfPCell(new Phrase("<Title of the Report>", new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 16f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.WHITE))); cell.BackgroundColor = new iTextSharp.text.Color(55, 178, 255); cell.BorderColor = new Color(192, 192, 192); cell.Border = Rectangle.BOTTOM_BORDER; cell.Colspan = dt.Columns.Count; cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right*/ // table.AddCell(cell); PdfPCell cell_data; for (int j = 0; j < dt.Columns.Count; j++) { cell_data = new PdfPCell(new Phrase(GridView1.HeaderRow.Cells[j].Text, new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 12f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(102, 178, 255); cell_data.PaddingLeft = cell_data.PaddingRight = 1f; cell_data.PaddingTop = cell_data.PaddingBottom = 5f; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right cell_data.BorderColor = new Color(192, 192, 192); cell_data.Border = Rectangle.BOTTOM_BORDER; table.AddCell(cell_data); } // table.HeaderRows = 0; //Flag the first row as a header for (int i = 0; i < GridView1.Rows.Count; i++ ) { for(int k = 0; k < dt.Columns.Count; k++) { if(i%2 == 0) { if (GridView1.Rows[i].Cells[k].Text != null) { cell_data = new PdfPCell(new Phrase(GridView1.Rows[i].Cells[k].Text, new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.HELVETICA, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(230, 230, 230); cell_data.PaddingLeft = cell_data.PaddingRight = 2f; cell_data.PaddingTop = cell_data.PaddingBottom = 3f; cell_data.BorderColor = new Color(192,192,192); cell_data.Border = Rectangle.BOTTOM_BORDER; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right table.AddCell(cell_data); } } else { if (GridView1.Rows[i].Cells[k].Text != null) { cell_data = new PdfPCell(new Phrase(GridView1.Rows[i].Cells[k].Text, new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.HELVETICA, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(255,255,255); cell_data.PaddingLeft = cell_data.PaddingRight = 2f; cell_data.PaddingTop = cell_data.PaddingBottom = 3f; cell_data.BorderColor = new Color(192, 192, 192); cell_data.Border = Rectangle.BOTTOM_BORDER; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right table.AddCell(cell_data); } } } } pdfDoc.Open(); // pdfDoc.Add(logo); pdfDoc.Add(para); pdfDoc.Add(criteria); //table.WriteSelectedRows(0, -1, 0, table.TotalHeight() + 18, writer.DirectContent); pdfDoc.Add(table); //htmlparser.Parse(sr); pdfDoc.Close(); System.Web.HttpContext.Current.Response.Write(pdfDoc); System.Web.HttpContext.Current.Response.End(); //End of PDF Code // ************************************************ // /* string excelFilename = context.Request["filename"]; if (File.Exists(excelFilename)) File.Delete(excelFilename); CreateExcelFile.CreateExcelDocument(dt, excelFilename, context.Response);*/ }
public void ProcessRequest(HttpContext context) { string tabData = context.Request["excelData"]; DataTable dt = ConvertCsvData(tabData); DataTable cr = ConvertCsvData(context.Request["criteriaArray"]); if (dt == null) { // Add some error-catching here... return; } //PDF Code int xy; //Create a dummy GridView GridView GridView1 = new GridView(); GridView1.AllowPaging = false; GridView1.DataSource = dt; GridView1.DataBind(); string title = context.Request["filename"]; System.Web.HttpContext.Current.Response.ContentType = "application/pdf"; System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + title + ".pdf"); System.Web.HttpContext.Current.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.Rotate(), 7f, 7f, 70f, 35f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream); //iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance("http://10.65.216.71:5666/resources/report_images/header_logo.PNG"); //Header Call pdfPage page = new pdfPage(); //set the PageEvent of the pdfWriter instance to the instance of our PDFPage class //PdfWriter.PageEvent = page; writer.PageEvent = page; Paragraph para = new Paragraph(new Phrase(title + "\n", new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 16f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.DARK_GRAY))); para.Alignment = Element.ALIGN_CENTER; Paragraph criteria = new Paragraph(context.Request["criteria"] + "\n\n", new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 10f, iTextSharp.text.Font.TIMES_ROMAN, iTextSharp.text.Color.GRAY)); criteria.Alignment = Element.ALIGN_CENTER; int x = GridView1.Columns.Count; PdfPTable table = new PdfPTable(dt.Columns.Count); table.WidthPercentage = 95; /*PdfPCell cell = new PdfPCell(new Phrase("<Title of the Report>", new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 16f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.WHITE))); * cell.BackgroundColor = new iTextSharp.text.Color(55, 178, 255); * cell.BorderColor = new Color(192, 192, 192); * cell.Border = Rectangle.BOTTOM_BORDER; * * cell.Colspan = dt.Columns.Count; * cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right*/ // table.AddCell(cell); PdfPCell cell_data; for (int j = 0; j < dt.Columns.Count; j++) { cell_data = new PdfPCell(new Phrase(GridView1.HeaderRow.Cells[j].Text, new iTextSharp.text.Font(iTextSharp.text.Font.BOLD, 12f, iTextSharp.text.Font.COURIER, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(102, 178, 255); cell_data.PaddingLeft = cell_data.PaddingRight = 1f; cell_data.PaddingTop = cell_data.PaddingBottom = 5f; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right cell_data.BorderColor = new Color(192, 192, 192); cell_data.Border = Rectangle.BOTTOM_BORDER; table.AddCell(cell_data); } // table.HeaderRows = 0; //Flag the first row as a header for (int i = 0; i < GridView1.Rows.Count; i++) { for (int k = 0; k < dt.Columns.Count; k++) { if (i % 2 == 0) { if (GridView1.Rows[i].Cells[k].Text != null) { cell_data = new PdfPCell(new Phrase(GridView1.Rows[i].Cells[k].Text, new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.HELVETICA, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(230, 230, 230); cell_data.PaddingLeft = cell_data.PaddingRight = 2f; cell_data.PaddingTop = cell_data.PaddingBottom = 3f; cell_data.BorderColor = new Color(192, 192, 192); cell_data.Border = Rectangle.BOTTOM_BORDER; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right table.AddCell(cell_data); } } else { if (GridView1.Rows[i].Cells[k].Text != null) { cell_data = new PdfPCell(new Phrase(GridView1.Rows[i].Cells[k].Text, new iTextSharp.text.Font(iTextSharp.text.Font.NORMAL, 8f, iTextSharp.text.Font.HELVETICA, iTextSharp.text.Color.BLACK))); cell_data.BackgroundColor = new iTextSharp.text.Color(255, 255, 255); cell_data.PaddingLeft = cell_data.PaddingRight = 2f; cell_data.PaddingTop = cell_data.PaddingBottom = 3f; cell_data.BorderColor = new Color(192, 192, 192); cell_data.Border = Rectangle.BOTTOM_BORDER; cell_data.HorizontalAlignment = 0; //0=Left, 1=Centre, 2=Right table.AddCell(cell_data); } } } } pdfDoc.Open(); // pdfDoc.Add(logo); pdfDoc.Add(para); pdfDoc.Add(criteria); //table.WriteSelectedRows(0, -1, 0, table.TotalHeight() + 18, writer.DirectContent); pdfDoc.Add(table); //htmlparser.Parse(sr); pdfDoc.Close(); System.Web.HttpContext.Current.Response.Write(pdfDoc); System.Web.HttpContext.Current.Response.End(); //End of PDF Code // ************************************************ // /* * string excelFilename = context.Request["filename"]; * * if (File.Exists(excelFilename)) * File.Delete(excelFilename); * * CreateExcelFile.CreateExcelDocument(dt, excelFilename, context.Response);*/ }