Reputation: 433
This is my working code. I need a little help with making this iterative as I am not familiar with itextsharp
.
When I click Print
button, it runs MySelectProcedure1
and table result is downloaded in a PDF named DataTable
.
protected void btnPrint_Click(object sender, EventArgs e)
{
Procedure("MySelectProcedure1");.
Procedure("MySelectProcedure2");
}
protected void Procedure(string Proc)
{
Connection con = new Connection();
SqlDataAdapter da;
DataTable dt;
con.con = new SqlConnection(con.str);
con.cmd.CommandText = Proc;
con.cmd.CommandType = CommandType.StoredProcedure;
da = new SqlDataAdapter();
da.SelectCommand = con.cmd;
dt = new DataTable();
try
{
con.con.Open();
da.Fill(dt);
print(dt);
}
catch (Exception ex)
{
}
finally
{
con.con.Close();
con.con.Dispose();
}
}
protected void print (DataTable dt)
{
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();
}
}
However, it does not print MySelectProcedure1
in the same PDF.
This is what I want:
MySelectProcedure1
MySelectProcedure2
How do I do this?
Upvotes: 0
Views: 1635
Reputation: 26
Yok can create a public function for export PDF. I will write how i use it.
Create a public class for example general.cs. And add this code in it.
public static void ExportPDF(DataTable dt1, DataTable dt2)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.pdf", "PDFExport"));
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
//For First DataTable
System.IO.StringWriter stringWrite1 = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite1 = new HtmlTextWriter(stringWrite1);
DataGrid myDataGrid1 = new DataGrid();
myDataGrid1.DataSource = dt1;
myDataGrid1.DataBind();
myDataGrid1.RenderControl(htmlWrite1);
//For Second DataTable
System.IO.StringWriter stringWrite2 = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite2 = new HtmlTextWriter(stringWrite2);
DataGrid myDataGrid2 = new DataGrid();
myDataGrid2.DataSource = dt2;
myDataGrid2.DataBind();
myDataGrid2.RenderControl(htmlWrite2);
//You can add more DataTable
StringReader sr = new StringReader(stringWrite1.ToString() + stringWrite2.ToString());
Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 10f, 0f);
pdfDoc.SetPageSize(PageSize.A4.Rotate());
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}
When you need PDF only call this function
protected void lnkExportPDF_Click(object sender, EventArgs e)
{
General.ExportPDF(rptList);
}
public override void VerifyRenderingInServerForm(Control control)
{ }
Upvotes: 1