• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2029
  • Last Modified:

Export Grid view to PDF with specific table width

Hi Experts
i am facing a problem in exporting to PDF since the grid view contain an Arabic text, so i changed my code and it working, the problem is the table was very small to handle the content, how can i change to width? and how can i export the same look and feel of the grid view to the PDF, also is it possible to export it as landscape document

        DataTable dt = GetData(SecCode, DirCode);
        GridView1.DataSource = dt;

            iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable(GridView1.Columns.Count);
            table.RunDirection = PdfWriter.RUN_DIRECTION_LTR;
            BaseFont bf = BaseFont.CreateFont("c:\\\\windows\\\\fonts\\\\tahoma.ttf", BaseFont.IDENTITY_H, true);
            iTextSharp.text.Font f2 = new iTextSharp.text.Font(bf, 8, iTextSharp.text.Font.NORMAL);
            //Transfer rows from GridView to table
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
                if (GridView1.Rows[i].RowType == DataControlRowType.DataRow)
                    for (int j = 0; j <= GridView1.Columns.Count - 1; j++)
                        string cellText = Page.Server.HtmlDecode(GridView1.Rows[i].Cells[j].Text);
                        iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(new Phrase(100, cellText, f2));
            //Create the PDF Document
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);
            Chunk c = new Chunk("Export GridView to PDF \n", FontFactory.GetFont("Verdana", 15));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            pdfDoc.Add(p); // add title            
            pdfDoc.Add(table); // add the table
            Page.Response.ContentType = "application/pdf";
            Page.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        catch (Exception ex)

Open in new window

please help me solving this problem
  • 3
1 Solution
Hiran DesaiSolution ArchitectCommented:
are you searching for ?

PdfPTable table = new PdfPTable(2);
//actual width of table in points
table.TotalWidth = 216f;
AZZA-KHAMEESAuthor Commented:
thank you for the reply, i manage to change the width of the of the columns using this

int[] intTblWidth = { 10, 10, 25, 50,25,25,50,10,50,10 };

Open in new window

1- how can i export the same look and feel of the grid view to the PDF?

2- is it possible to export it as landscape document?

3- also how can i change the margin of the exported PDF?
AZZA-KHAMEESAuthor Commented:
I manage to solve the problem using this link
Exporting GridView to PDF

thank you
AZZA-KHAMEESAuthor Commented:
found the best solution
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now