Solved

Export Grid view to PDF with specific table width

Posted on 2013-12-04
4
1,392 Views
Last Modified: 2013-12-12
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;

        try
        {
            
            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));
                        
                        
                        table.AddCell(cell);
                    }
                }
            }
            //Create the PDF Document
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            PdfWriter.GetInstance(pdfDoc, Page.Response.OutputStream);
            pdfDoc.Open();
            Chunk c = new Chunk("Export GridView to PDF \n", FontFactory.GetFont("Verdana", 15));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            pdfDoc.Add(p); // add title            
            pdfDoc.Add(table); // add the table
            pdfDoc.Close();
            Page.Response.ContentType = "application/pdf";
            Page.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Page.Response.Write(pdfDoc);
            Page.Response.End();
        }
        catch (Exception ex)
        {
            Page.Response.Write(ex.Message);
        }

Open in new window


please help me solving this problem
0
Comment
Question by:AZZA-KHAMEES
  • 3
4 Comments
 
LVL 4

Expert Comment

by:ItWorked
ID: 39697609
are you searching for ?

PdfPTable table = new PdfPTable(2);
//actual width of table in points
table.TotalWidth = 216f;
0
 

Author Comment

by:AZZA-KHAMEES
ID: 39697639
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 };
table.SetWidths(intTblWidth);

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?
0
 

Accepted Solution

by:
AZZA-KHAMEES earned 0 total points
ID: 39703962
I manage to solve the problem using this link
Exporting GridView to PDF

thank you
0
 

Author Closing Comment

by:AZZA-KHAMEES
ID: 39713683
found the best solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now