PDFPCell宽度不变

时间:2013-12-08 08:24:33

标签: c# asp.net pdf gridview datagridview

我正在处理出口阿拉伯网格视图到pdf文件,经过一百个经验我终于设法解决了问题,现在只有一个我面临的问题,我无法改变列的宽度,我试过使用

float[] columnWidths = {2f, 1f, 1f};
table.setWidths(columnWidths);

但它也不起作用,所有他的专栏都是相同的尺寸

                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);

        for (int i = 0; i < noOfColumns; i++)
        {
            Phrase ph = null;

            if (GridView1.AutoGenerateColumns)
            {
                ph = new Phrase(tbl.Columns[i].ColumnName, FontFactory.GetFont("Tahoma", 8, iTextSharp.text.Font.BOLD));
            }
            else
            {
                ph = new Phrase(GridView1.Columns[i].HeaderText, FontFactory.GetFont("Tahoma", 8, iTextSharp.text.Font.BOLD));
            }
            PdfPCell clHeader = new PdfPCell(ph);
            clHeader.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#e9e9e9"));

            table.AddCell(clHeader);                
        }

        for (int i = 0; i <= GridView1.Rows.Count-1; i++)
        {
                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);

                }
        }


        PdfWriter.GetInstance(document, Page.Response.OutputStream);
        document.Open();
        document.SetMargins(0, 0, 0, 0);

        document.Add(table); // add the table

        document.Close();
        Page.Response.ContentType = "application/pdf";
        Page.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Page.Response.Write(document);
        Page.Response.End();

1 个答案:

答案 0 :(得分:0)

通过更改单元格宽度的位置来解决问题。 我在第二个循环下添加了读取数据列

for (int i = 0; i <= GridView1.Rows.Count-1; i++)
        {
                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);

                }
        }
        //--set the columns width----
        float[] columnWidths = new float[] { 5f, 11f, 10f, 20f,15f,20f,7f,40f };
        table.SetWidths(columnWidths);