PDF单元格宽度不变

本文关键字:单元格 PDF | 更新日期: 2024-08-01 22:51:34

我正在将阿拉伯语网格视图导出到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();

PDF单元格宽度不变

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

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