iTextSharp不显示所有列

本文关键字:显示 iTextSharp | 更新日期: 2023-09-27 18:08:09

我最近开始使用itextsharp,并一直使用它在asp.net中使用web服务创建PDF报告。我在web服务中的代码如下所示。我的问题是,它不显示前3列的数据。

我认为问题出在dt.Rows

        string[] strFile = Directory.GetFiles(strUploadPath);
        Array.ForEach(Directory.GetFiles(strUploadPath), File.Delete);
        Document document = new Document();
        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strUploadPath + "/" + strFilename, FileMode.Create));
        document.Open();
        Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);
        PdfPTable table = new PdfPTable(dt.Columns.Count);
        float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f };
        table.SetWidths(widths);
        table.WidthPercentage = 200;
        PdfPCell cell = new PdfPCell(new Phrase());
        int j = 1;
        foreach (DataColumn c in dt.Columns)
        {
            if (j <= (dt.Columns.Count))
            {
                //table.AddCell(new Phrase(c.ToString(),font5));
                //table.AddCell(new Phrase(j.ToString(),font5));
                table.AddCell(new Phrase(c.ColumnName, font5));
            }
            j++;
        }
        int k = 1;
        foreach (DataRow r in dt.Rows)
        {
            if (dt.Rows.Count > 0)
            {
                table.AddCell(new Phrase(k.ToString(), font5));
                table.AddCell(new Phrase(r[1].ToString(), font5));
                table.AddCell(new Phrase(r[2].ToString(), font5));
                table.AddCell(new Phrase(r[3].ToString(), font5));
                table.AddCell(new Phrase(r[4].ToString(), font5));
                table.AddCell(new Phrase(r[5].ToString(), font5));
               // table.AddCell(new Phrase(r[6].ToString(), font5));
                //                table.AddCell(new Phrase(r[7].ToString(), font5));
            }
            k++;
        }
        document.Add(table);
        document.CloseDocument();
        document.Close();
        return strFilename;
    }
    else
    {
        return null;
    }

iTextSharp不显示所有列

OP的代码包含这一行:

table.WidthPercentage = 200;

这将导致表的宽度是页面减去页边距的两倍(200%)。因此,它是部分隐藏的。

当使用WidthPercentage属性设置表宽度时,通常不应该设置超过100。

正如OP同时确认的那样,适当的更改会使代码正常工作。