我希望在生成pdf文档之前在stringbuilder中逐页添加动态数字页面,通过stringBuilder和executereader生成itextsharp。
数据是从sql查询生成的。
这是无效的,pdf中的所有文档都是在这里生成的:
protected void GeneraReporte_Click(object sender, EventArgs e)
{
using (SqlConnection cone = conexion.conectar())
{
using (SqlCommand comand = new SqlCommand("REPORTEALTASBAJAS", cone))
{
comand.CommandType = CommandType.StoredProcedure;
comand.Parameters.Add("@VALOR", SqlDbType.Int).Value = altas;[...]
cone.Open();
using (SqlDataReader dr = comand.ExecuteReader())
{
if (dr.HasRows)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[13] {
new DataColumn("USUARIO"),[...]
});
dt.Load(dr); //load grid with sql
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
StringBuilder sb = new StringBuilder();
sb.Append("<br/>");
//HEADER
sb.Append("<table style='font-size: 8px;' align='center' width='95%' cellspacing='0' cellpadding='2'>");
sb.Append("<tr>");
sb.Append("<td align='left'>");
sb.Append(@"<img height='50x' src=" + UAT + " />");
sb.Append("</td>");
sb.Append("<td align='center' colspan = '1'>");
sb.Append(@"<img height='50x' align='center' src=" + logoCentro + " />");
sb.Append("</td>");
sb.Append("<td colspan='1' align='right'>");
sb.Append(@"<img height='50x' align='right' src=" + FCAV + " />");
sb.Append("<br/></td></tr>");
sb.Append("<tr ><td><br/></td><td><br/></td><td><br/></td></tr>");
sb.Append("<tr><td align='left'><b></b></td><td align='center'>
sb.Append("REPORTE DE ALTAS Y BAJAS");
sb.Append("</b></td><td align='right'><b></b></td></tr>");
sb.Append("<tr ><td><br/></td><td><br/></td><td><br/></td></tr>");
if (dr.Read())
{
if (ddldepto.SelectedItem.Text == "--Todos--")
{
}
else
{
sb.Append("<tr><td align='left''><b>SRIA./COORD: " + dr["NOMBREC"].ToString() + "</b></td>");
if (dr["NOMBRED"] == DBNull.Value)
{
sb.Append("<td align='center'><b></b></td>");
}
else
{
sb.Append("<td align='center'><b>DEPARTAMENTO: " + dr["NOMBRED"].ToString()+ "</b></td>");
}
sb.Append("<td align='right'><b>RESPONSABLE: " + dr["NOMBREP"].ToString() + "</b></td></tr>");
}
}
sb.Append("</table>");
sb.Append("<br/>");
//ITERATE
sb.Append("<table align='center' width='95%' style='' border='1'>");
sb.Append("<tr>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<th style='font-size:9px;text-align: center;color:#000'>");
sb.Append("<b>");
sb.Append(column.ColumnName);
sb.Append("</b>");
sb.Append("</th>");
}
sb.Append("</tr>");
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr style='font-size: 8px;'>");
foreach (DataColumn column in dt.Columns)
{
sb.Append("<td width='100%'>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
sb.Append("<br/>");
//FOOTER
sb.Append("<br/>");
sb.Append("<table align='center' width='95%'>");
sb.Append("<tr><td align='left'>SECRETARIA ADMINISTRATIVA:_________________________________</td><td align='left'>CONTRALORIA INTERNA:______________________________</td></tr>");
sb.Append("</table>");
//EXPORTA EL STRING COMO PDF
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.LEGAL.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=Reporte.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}
}
else
{
notif.Visible = true;
}
}
}
}
}