I have a project where I want to generate a PDF from gridview but after generating Pdf I want to refresh my gridview and add new data.
private void Generate_PDF()
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
//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>Bill Report</b></td></tr>");
sb.Append("<tr><td colspan = '2'></td></tr>");
sb.Append("<tr><td><b>Bill No: </b>");
sb.Append(HiddenField1.Value);
sb.Append("</td><td align = 'right'><b>Date: </b>");
sb.Append(DateTime.Now);
sb.Append(" </td></tr>");
sb.Append("</table>");
sb.Append("<br />");
GridOfTestRequest.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
StringReader sr1 = new StringReader(sb.ToString());
sb1.Append("<table border = '1'>");
sb1.Append("<tr><td align = 'right' colspan = '");
sb1.Append(GridOfTestRequest.HeaderRow.Cells.Count - 1);
sb1.Append("'>Total</td>");
sb1.Append("<td>");
sb1.Append(testList.Sum(x=>x.testFee));
sb1.Append("</td>");
sb1.Append("</tr></table>");
StringReader sr2 = new StringReader(sb1.ToString());
testList.Clear();
GridOfTestRequest.DataSource = null;
GridOfTestRequest.DataBind();
Document pdfDoc = new Document(PageSize.A5, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr1);
htmlparser.Parse(sr);
htmlparser.Parse(sr2);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Invoic.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.Flush();
Response.ClearContent();
Response.Clear();
Response.Cookies.Clear();
Response.End();
}
After execution of this method in a button click, I want to clear the Gridview. How can I do it? I tried to clear it with gridview.datasource=null but it doesn't work.
Related
I am downloading data in Pdf form, but my document is not grid look
Below is the code:
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ClientRecord.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.Style.Add("font-size", "10px");
GridView1.Style.Add("border", "solid 2px blue");
GridView1.DataBind();
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();
Can anybody help me with that, plz?
I generate a Table object where some cells have a background-color. This background-color is dynamically loaded from a database.
I set the BackColor in my code with the following lines:
TableCell tCell = new TableCell();
tCell.BackColor = (Color)converter.ConvertFromString(color_startBorderCrtColor);
tCell.Text = Convert.ToString(row[column.ColumnName]);
tRow.Cells.Add(tCell);
When I append the rendered table to a StringBuilder and write it into a PDF using iTextSharp the Background-Color of the cells is not shown. Instead when I write the StringBuilder into a literal the cells are painted correctly.
Here is the code how I convert the Table and append it to my StringBuilder:
TextWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
Table myGenTable = (Table)genObjects[0];
myGenTable.RenderControl(hw);
sb.Append(tw.ToString()); //sb is the StringBuilder I'm working with
Is there a way where I can paint the ccells even in the pdf codument?
The same problem exists with the border of the table which is set as attributes to the table itself with the following code:
tblcblCellsCQ.BorderColor = Color.Black;
tblcblCellsCQ.BorderWidth = 2;
tblcblCellsCQ.BorderStyle = BorderStyle.Dashed;
Here is the code where I write the StringBuilder into the PDF file:
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4.Rotate(), 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=" + friendlyName + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
Try this :
for (int i = 0; i < col.Length; ++i)
{
Cell cell = new Cell(new Phrase(col[i], new iTextSharp.text.Font(iTextSharp.text.Font.COURIER, 5)));
cell.Header = true;
cell.BackgroundColor = new iTextSharp.text.Color(204, 204, 204);
table.AddCell(cell);
}
check the answer at : https://forums.asp.net/t/1577892.aspx?iTextSharp+table+headers
I am creating an application in which I successfully exported my ListView to PDF format. But I have some problem regarding my PDF: the formatting of the PDF is very bad.
Take a look:
I am using the following code to export to PDF:
protected void ExportToPdf_Click(object sender, EventArgs e)
{
string File = "PDFCollegeDetails";
var result = GetData().ToList();
ExportListToPDF(result, File);
}
private void ExportListToPDF(List<CollegeDetail> result, string File)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=CollegeDetailsInPdf.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView gridView = new GridView();
gridView.DataSource = result;
gridView.DataBind();
gridView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 50f, 50f, 50f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
gridView.AutoGenerateColumns = true;
}
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=ZoneWiseReport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridViewZoneReport.AllowPaging = false;
this.BindGridView();
GridViewZoneReport.DataBind();
GridViewZoneReport.RenderControl(hw);
GridViewZoneReport.HeaderRow.Style.Add("width", "15%");
GridViewZoneReport.HeaderRow.Style.Add("font-size", "10px");
GridViewZoneReport.Style.Add("text-decoration", "none");
GridViewZoneReport.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridViewZoneReport.Style.Add("font-size", "8px");
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
protected void ExportToPdf(DataTable dt, string str)
{
ArrayList ADA = new ArrayList();
ADA.Add(FirstAssignment.SelectedItem);
ADA.Add(SecondAssignment.SelectedItem);
GridView GridView1 = new GridView();
GridView GridView2 = new GridView();
GridView1.ShowHeaderWhenEmpty = true;
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
int i1 = GridView1.Columns.Count;
GridView1.HeaderRow.Cells[1].Text = ADA[0].ToString();
GridView1.HeaderRow.Cells[2].Text = ADA[1].ToString();
GridView1.HeaderRow.Cells[3].Text = "% of Improvement from " + (ADA[0].ToString()) + " to " + (ADA[1].ToString());
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + str + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.HeaderRow.ForeColor = System.Drawing.Color.Black;
GridView1.FooterRow.ForeColor = System.Drawing.Color.Black;
GridView1.HeaderRow.Style.Add("font-Color", "Black");
GridView1.HeaderRow.Style.Add("font-size", "13px");
GridView1.HeaderRow.Style.Add("text-decoration", "none");
GridView1.HeaderRow.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView1.Style.Add("font-Color", "Black");
GridView1.Style.Add("text-decoration", "none");
GridView1.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
GridView1.Style.Add("font-size", "11px");
GridView1.ForeColor = System.Drawing.Color.Black;
Document pdfDoc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
// HTMLWorker htmlparser1 = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("hey! rashmi"));
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
This is for creating one grid view. can any one help me to create a dynamic grid view using this code.I have tried to giving a for loop GridView1.RenderControl(hw); but I am getting single grid view with same data.Thanks in advance.
If you want show gridview in pdf,you can show as table also here is
code :
public void createPdf()
{
// step 1
using (MemoryStream ms = new MemoryStream())
{
Document document = new Document(iTextSharp.text.PageSize.A4, 10, 22, 34, 34);
// step 2
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// step 3
document.Open();
MemoryStream stream = new MemoryStream();
// step 4 how many tables you want to create
for (int i = 0; i < 5; i++)
{
document.Add(new Paragraph("Table:"));
document.Add(createFirstTable());
}
// step 5
document.Close();
writer.Close();
Response.ContentType = "pdf/application";
Response.AddHeader("content-disposition",
"attachment;filename=First PDF document.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
}
}
public static PdfPTable createFirstTable(DataTable dt)
{
// a table with three columns
PdfPTable table = new PdfPTable(dt.Columns.Count);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
//cell = new PdfPCell(new Phrase("Cell with colspan 3"));
table.AddCell(cell);
//// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
table.AddCell("ADXHGS");
table.AddCell("WFEWSA");
table.AddCell("EWSFCEDSW");
table.AddCell("EWSEWSFDFCEDSW");
//PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table
return table;
}
This is my code :
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.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, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
**htmlparser.Parse(sr);** //the exception here
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
the error is :
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type
'iTextSharp.text.Paragraph'.
What is this exception ?
I don't know if this is the answer, but I found that iTextSharp is picky about having valid html. My tests had a table that was opened twice and never closed and that took about an hour for me to notice. The exception was very similar to the one you have there.
use can you A4 size width & height for html deigning .
use this set od code to create pdf from html.
String htmlText = "<div>Your HTML text here</div>";
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\outfile.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(Request.PhysicalApplicationPath + "\\outfile.pdf");
Response.Flush();
Response.Clear();