我在使用iTextSharp dll从Windows窗体生成pdf时遇到问题。我在数据集中一次填充超过70条记录,并通过循环将其绑定到新表(pdfptable)。但是它说对象引用没有设置为对象的实例。当我将行数减少到68时,它可以正常工作。经过分析,我发现表格无法沿多页分割。
我正在使用以下代码。请帮忙
float[] widths1 = new float[] { 1f, 2f,1f,1f,1f,1f,1f};
PdfPTable grd = new PdfPTable(GrdAnoSecList.Columns.Count);
grd.SetWidths(widths1);
for (int i = 0; i < GrdAnoSecList.Columns.Count; i++)
{
PdfPCell ph;
if (Convert.ToString(GrdAnoSecList.Columns[i].HeaderText) == "Job Name")
{
ph = new PdfPCell(new Phrase(GrdAnoSecList.Columns[i].HeaderText, grdfontHdr));
ph.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
ph.BorderWidthTop = 0;
ph.BorderWidthRight = 0;
ph.HorizontalAlignment = 1;
}
else
{
ph= new PdfPCell(new Phrase(GrdAnoSecList.Columns[i].HeaderText, grdfontHdr));
ph.BackgroundColor = new iTextSharp.text.Color(240, 240, 240);
ph.BorderWidthTop = 0;
ph.BorderWidthRight = 0;
ph.HorizontalAlignment = 1;
//ph.Width = 100f;
}
grd.AddCell(ph);
}
int ct = 0;
foreach (DataGridViewRow row in GrdAnoSecList.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
ct++;
PdfPCell ph2;
if (Convert.ToString(cell.Value) == "Total")
{
ph2 = new PdfPCell(new Phrase(cell.Value.ToString(), grdfontHdr));
}
else
{
ph2 = new PdfPCell(new Phrase(cell.Value.ToString(), myfont));
}
if (cell.ValueType == typeof(decimal) || cell.ValueType == typeof(Int32))
{
ph2.HorizontalAlignment = 2;
}
else if (cell.ColumnIndex == 1 && Convert.ToString(cell.Value) != "Total")
{
ph2.HorizontalAlignment = 0;
}
else
{
ph2.HorizontalAlignment = 1;
}
grd.AddCell(ph2);
}
}
PdfPCell cellAmountText1 = new PdfPCell(new PdfPTable(grd));
cellAmountText1.Colspan = 6;
table.AddCell(cellAmountText1);