Click here to Skip to main content
15,077,641 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi how i can to PDF using iTextSharp, a formated gridview?
in the screen the GridView is formated, with 500px of width in the first column, and background colors.

But in the exported GridView only the backgroundcolors appears.


Here Some Images:

This is the formated GridView.
01

PDF_1
PDF_2

Here the .aspx code

ASP.NET
<asp:Button ID="btnExportPdf" runat="server" Text="ExportToPDF" onclick="btnExportPdf_Click" /><br />
        <asp:GridView ID="gvPartidasAnual" runat="server" Style="text-align:right" CellPadding="4" ForeColor="#000000" BorderWidth="1" BorderStyle="Solid" 
        GridLines="Both" AlternatingRowStyle-BackColor="White" onrowdatabound="gvPartidasAnual_RowDataBound" >
        <RowStyle BackColor="#F5ECFF" />
        <Columns >
        </Columns>
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center"   />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="White"/>
        <EditRowStyle BackColor="#2461BF" />
    </asp:GridView>



and here the .aspx.cs code

C#
protected void Page_Load(object sender, EventArgs e)
    {
        int anioInicio = 2014;
        int anioFin = 2015;
        DatosgvPartidasAnual(anioInicio, anioFin);
    }

    protected void DatosgvPartidasAnual(int anioInicio, int anioFin)
    {
        SqlConnection conexion = new SqlConnection(strConnString);
        conexion.Open();

        DataTable DTgvPartidasAnual = new DataTable();

        int anioFin_0 = anioInicio + 1;

        string fechaInicio = "01/01/" + anioInicio;
        string fechaFin = "01/01/" + anioFin_0;

        //AÑADIENDO LAS COLUMNAS
        DTgvPartidasAnual.Columns.Add("Código SAIDS");
        DTgvPartidasAnual.Columns.Add("Préstamo");
        DTgvPartidasAnual.Columns.Add("Fecha");
        DTgvPartidasAnual.Columns.Add("611");
        DTgvPartidasAnual.Columns.Add("612");
        DTgvPartidasAnual.Columns.Add("613");
        DTgvPartidasAnual.Columns.Add("616");
        DTgvPartidasAnual.Columns.Add("617");
        DTgvPartidasAnual.Columns.Add("618");
        DTgvPartidasAnual.Columns.Add("621");
        DTgvPartidasAnual.Columns.Add("622");
        DTgvPartidasAnual.Columns.Add("626");
        DTgvPartidasAnual.Columns.Add("627");
        DTgvPartidasAnual.Columns.Add("628");
        DTgvPartidasAnual.Columns.Add("662");
        DTgvPartidasAnual.Columns.Add("671");
        DTgvPartidasAnual.Columns.Add("961");
        DTgvPartidasAnual.Columns.Add("TOTAL");

        SqlCommand cmd_PartidasAnual = new SqlCommand(
        "SELECT TOP 20 cod_saids, credito, fecha, " +
        "SUM(part_611) AS part_611, SUM(part_612) AS part_612, SUM(part_613) AS part_613, SUM(part_616) AS part_616, " +
        "SUM(part_617) AS part_617, SUM(part_618) AS part_618, SUM(part_621) AS part_621, SUM(part_622) AS part_622, SUM(part_626) AS part_626, " +
        "SUM(part_627) AS part_627, SUM(part_628) AS part_628, SUM(part_662) AS part_662, SUM(part_671) AS part_671, SUM(part_961) AS part_961, SUM(total) AS total " +
        "FROM tblPartidas " +
        "WHERE fecha >= '" + fechaInicio + "' AND fecha < '" + fechaFin + "' " +
        "GROUP BY cod_saids, credito, fecha " +
        "ORDER BY cod_saids", conexion);

        SqlDataAdapter adp_PartidasAnual = new SqlDataAdapter(cmd_PartidasAnual);
        DataTable dt_PartidasAnual = new DataTable();
        adp_PartidasAnual.Fill(dt_PartidasAnual);

        int cant_PartidasAnual = dt_PartidasAnual.Rows.Count;

        decimal part_611_total_0 = 0M, part_612_total_0 = 0M, part_613_total_0 = 0M, part_616_total_0 = 0M, part_617_total_0 = 0M, part_618_total_0 = 0M, part_621_total_0 = 0M;
        decimal part_622_total_0 = 0M, part_626_total_0 = 0M, part_627_total_0 = 0M, part_628_total_0 = 0M, part_662_total_0 = 0M, part_671_total_0 = 0M, part_961_total_0 = 0M, total_total_0 = 0M;

        string part_611_total = "", part_612_total = "", part_613_total = "", part_616_total = "", part_617_total = "", part_618_total = "", part_621_total = "";
        string part_622_total = "", part_626_total = "", part_627_total = "", part_628_total = "", part_662_total = "", part_671_total = "", part_961_total = "", total_total = "";

        string part_611 = "", part_612 = "", part_613 = "", part_616 = "", part_617 = "", part_618 = "", part_621 = "", part_622 = "", part_626 = "", part_627 = "", part_628 = "", part_662 = "";
        string part_671 = "", part_961 = "", total = "";

        for (int a1 = 0; a1 < cant_PartidasAnual; a1++)
        {
            string cod_saids = Convert.ToString(dt_PartidasAnual.Rows[a1][0]);
            string credito = Convert.ToString(dt_PartidasAnual.Rows[a1][1]);
            DateTime fecha_0 = Convert.ToDateTime(dt_PartidasAnual.Rows[a1][2]);
            decimal part_611_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][3]); part_611_total_0 = part_611_total_0 + part_611_0;
            decimal part_612_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][4]); part_612_total_0 = part_612_total_0 + part_612_0;
            decimal part_613_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][5]); part_613_total_0 = part_613_total_0 + part_613_0;
            decimal part_616_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][6]); part_616_total_0 = part_616_total_0 + part_616_0;
            decimal part_617_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][7]); part_617_total_0 = part_617_total_0 + part_617_0;
            decimal part_618_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][8]); part_618_total_0 = part_618_total_0 + part_618_0;
            decimal part_621_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][9]); part_621_total_0 = part_621_total_0 + part_621_0;
            decimal part_622_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][10]); part_622_total_0 = part_622_total_0 + part_622_0;
            decimal part_626_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][11]); part_626_total_0 = part_626_total_0 + part_626_0;
            decimal part_627_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][12]); part_627_total_0 = part_627_total_0 + part_627_0;
            decimal part_628_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][13]); part_628_total_0 = part_628_total_0 + part_628_0;
            decimal part_662_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][14]); part_662_total_0 = part_662_total_0 + part_662_0;
            decimal part_671_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][15]); part_671_total_0 = part_671_total_0 + part_671_0;
            decimal part_961_0 = Convert.ToDecimal(dt_PartidasAnual.Rows[a1][16]); part_961_total_0 = part_961_total_0 + part_961_0;

            

            decimal total_0 = part_611_0 + part_612_0 + part_613_0 + part_616_0 + part_617_0 + part_618_0 + part_621_0 + part_622_0 + part_626_0 + part_627_0 + part_628_0 + part_662_0 + part_671_0 + part_961_0;
            total_total_0 = total_total_0 + total_0;

            part_611 = part_611_0.ToString("#,0.00");
            part_612 = part_612_0.ToString("#,0.00");
            part_613 = part_613_0.ToString("#,0.00");
            part_616 = part_616_0.ToString("#,0.00");
            part_617 = part_617_0.ToString("#,0.00");
            part_618 = part_618_0.ToString("#,0.00");
            part_621 = part_621_0.ToString("#,0.00");
            part_622 = part_622_0.ToString("#,0.00");
            part_626 = part_626_0.ToString("#,0.00");
            part_627 = part_627_0.ToString("#,0.00");
            part_628 = part_628_0.ToString("#,0.00");
            part_662 = part_662_0.ToString("#,0.00");
            part_671 = part_671_0.ToString("#,0.00");
            part_961 = part_961_0.ToString("#,0.00");
            total = total_0.ToString("#,0.00");

            string fecha = fecha_0.ToString("dd/MM/yyyy");

            DTgvPartidasAnual.Rows.Add(cod_saids, credito, fecha, part_611, part_612, part_613, part_616, part_617, part_618, part_621, part_622, part_626, part_627, part_628, part_662, part_671, part_961, total);
        }

        part_611_total = part_611_total_0.ToString("#,0.00");
        part_612_total = part_612_total_0.ToString("#,0.00");
        part_613_total = part_613_total_0.ToString("#,0.00");
        part_616_total = part_616_total_0.ToString("#,0.00");
        part_617_total = part_617_total_0.ToString("#,0.00");
        part_618_total = part_618_total_0.ToString("#,0.00");
        part_621_total = part_621_total_0.ToString("#,0.00");
        part_622_total = part_622_total_0.ToString("#,0.00");
        part_626_total = part_626_total_0.ToString("#,0.00");
        part_627_total = part_627_total_0.ToString("#,0.00");
        part_628_total = part_628_total_0.ToString("#,0.00");
        part_662_total = part_662_total_0.ToString("#,0.00");
        part_671_total = part_671_total_0.ToString("#,0.00");
        part_961_total = part_961_total_0.ToString("#,0.00");
        total_total = total_total_0.ToString("#,0.00");

        DTgvPartidasAnual.Rows.Add("", "", "TOTAL", part_611_total, part_612_total, part_613_total, part_616_total, part_617_total, part_618_total, part_621_total,
        part_622_total, part_626_total, part_627_total, part_628_total, part_662_total, part_671_total, part_961_total, total_total);

        gvPartidasAnual.DataSource = DTgvPartidasAnual;
        gvPartidasAnual.DataBind();

        conexion.Close();
    }

    protected void gvPartidasAnual_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCell cod_saids = e.Row.Cells[0]; /**/ cod_saids.Width = new Unit("500px");
            //cod_saids.Font.Size = FontUnit.XSmall;//error:"Font size too small: 0"
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string fecha = e.Row.Cells[2].Text.ToString();

            if (fecha == "TOTAL")
            {
                e.Row.BackColor = System.Drawing.Color.FromName("#000000");
                e.Row.ForeColor = System.Drawing.Color.FromName("#FFFFFF");
            }
        }
    }

    protected void btnExportPdf_Click(object sender, EventArgs e) 
    {
        Document document = new Document(PageSize.LEGAL.Rotate(), 10f, 10f, 10f, 0f);
        PdfWriter.GetInstance(document, Response.OutputStream);

        document.Open();

        //Tabla 1 -> Header
        PdfPTable table = new PdfPTable(3);
        table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
        table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
        table.AddCell("Logo 1");
        table.AddCell("TITULO");
        table.AddCell("Logo 2");
        document.Add(table);

        //Tabla 2 -> space
        PdfPTable table2 = new PdfPTable(1);
        table2.DefaultCell.Border = 0;
        table2.AddCell("");
        document.Add(table2);


        //Tabla 3 -> Data
        PdfPTable table3 = new PdfPTable(1);
        table3.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
        table3.DefaultCell.VerticalAlignment = Element.ALIGN_TOP;

        //GridView
        HTMLWorker htmlparser = new HTMLWorker(document);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        gvPartidasAnual.AllowPaging = false;
        gvPartidasAnual.DataBind();
        gvPartidasAnual.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
        htmlparser.Parse(sr);

        document.Close();

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=PDF_File.pdf");
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900