raje
raje

Reputation: 165

converting gridview to itextsharp table and generating pdf using asp.net

here i am trying to convert GridView to PDF, when converting PDF not getting the GridView column width in PDF.So i used another method to generate PDF (i.e) created itextsharp table from GridView .Here i got column width size which i gave in GridView column ItemStyle-Width.But i have done grouped column in that GridView so i am not getting the full GridView with same column width in PDF.can anyone please help me .

here is the source:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     onsorting="GridView1_Sorting">
     <HeaderStyle BackColor="#4F81BD" Font-Bold="true" ForeColor="#ffffff" HorizontalAlign="Center" Font-Size="12px"/>
    <AlternatingRowStyle BackColor="#D3DFEE" />
    <RowStyle />
    <Columns>
         <asp:BoundField DataField="Category" HeaderText="Category"
            SortExpression="Category" ItemStyle-Width="100px"/>
            <asp:BoundField DataField="Partno" HeaderText="Partno" 
             ItemStyle-Width="100px"/>
            <asp:BoundField DataField="Productname" HeaderText="Productname" 
             ItemStyle-Width="300px"/>
            <asp:BoundField DataField="Brand" HeaderText="Brand" 
            ItemStyle-Width="100px"/>
            <asp:BoundField DataField="Qty" HeaderText="Qty" 
             ItemStyle-Width="50px"/>
            <asp:BoundField DataField="Unitprice" HeaderText="Unitprice" 
             ItemStyle-Width="50px"/>
            <asp:BoundField DataField="Totalprice" HeaderText="Totalprice" 
             ItemStyle-Width="50px"/>
    </Columns>

</asp:GridView>  

here is the grouped gridview:

enter image description here

here is the code to convert gridview to itextsharp table and generating PDF:

//Create a table
iTextSharp.text.Table table = new iTextSharp.text.Table(GridView1.Columns.Count);
table.Cellpadding = 5;
//Set the column widths
int[] widths = new int[GridView1.Columns.Count];
for (int x = 0; x < GridView1.Columns.Count; x++)
{
    widths[x] = (int)GridView1.Columns[x].ItemStyle.Width.Value;
    string cellText = Server.HtmlDecode(GridView1.HeaderRow.Cells[x].Text);
    iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
    cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#008000"));
    table.AddCell(cell);
}

table.SetWidths(widths);
//Transfer rows from GridView to table

for (int i = 0; i < GridView1.Rows.Count; i++)
{
    if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
    {
        for (int j = 0; j < GridView1.Columns.Count; j++)
        {
            string cellText = Page.Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
            iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);

            //Set Color of Alternating row
            if (i % 2 != 0)
            {
                cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.ColorTranslator.FromHtml("#C2D69B"));
            }

            table.AddCell(cell);
        }
    }
}

//Create the PDF Document

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
                               "filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();

here is the PDF:

enter image description here

i want exact gridview with same column width in PDF.

Upvotes: 2

Views: 11449

Answers (2)

Teja
Teja

Reputation: 1

This should solve the issue:

GridView1.AllowPaging = False

Upvotes: 0

Chris Haas
Chris Haas

Reputation: 55427

The PdfPTable uses relative widths by default. When you say table.SetWidths(widths) iText sums up widths and divides each individual width by that sum to get a percentage. So 1, 2, 3 is the same as 10, 20, 30 and even 2, 4, 6. You want to use SetTotalWidth(widths) along with setting LockedWidth = true the force absolute widths.

table.SetTotalWidth(widths);
table.LockedWidth = true;

However, doing this is declaring that you are now aware of and in control of the table's horizontal layout. If you overflow the page horizontally you will not getting any wrapping and things will just cut off. You are also declaring that margin space is acceptable to run into if needed.

Upvotes: 2

Related Questions