多页上的ItextSharp嵌套表会导致NullReferenceException

时间:2010-02-04 18:45:53

标签: c# asp.net pdf-generation itextsharp nullreferenceexception

执行以下代码时,我收到NullReferenceException。我还注意到,当我在将代码写入主表的代码中添加时,嵌套表出现在新页面上。如果我取出将单元格写入主表的两个循环,则不会发生。

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" %>

<%@ Import Namespace="iTextSharp.text" %>
<%@ Import Namespace="iTextSharp.text.pdf" %>

<%@ Import Namespace="System.IO" %>


<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{

    Document doc = new Document(PageSize.LETTER);

    // Here is some stuff needed to put the pdf into the users response stream
    Response.Clear();
    Response.ContentType = "application/pdf";
    MemoryStream memoryStream = new MemoryStream();

    PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

    writer.CloseStream = false; // This is important, but it may work without it

    doc.SetMargins(20, 20, 36, 10);

    // write stuff to the documents
    doc.Open();

    doc.Add(GenerateNestedTableTest());


    doc.Close();

    // Now that all writing to the document is done lets send it to the user
    writer.Flush();
    Response.OutputStream.Write(memoryStream.GetBuffer(), 0, memoryStream.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.OutputStream.Close();
    Response.End();
}


private PdfPTable GenerateNestedTableTest()
{
    PdfPTable mainTable = new PdfPTable(5);

    // test adding cells to mainTable in groups of 5
    for (int i = 0; i <= 5 * 10; ++i)
    {
        PdfPCell mainTableCell = new PdfPCell(new Phrase("Test"));
        mainTable.AddCell(mainTableCell);
    }

    PdfPTable nestedTable = new PdfPTable(3);

    Font TitleFont = new Font(null, 20, Font.BOLD, Color.BLACK);

    PdfPCell TitleCell = new PdfPCell(new Phrase("This is the header of the nested table", TitleFont));
    TitleCell.Colspan = 3;

    nestedTable.AddCell(TitleCell);

    string[] headers = new string[] { "Header1", "Header2", "Header3" };

    foreach (string header in headers)
    {
        PdfPCell HeaderCell = new PdfPCell(new Phrase(header, new Font(null, 14, Font.BOLD, Color.BLUE)));
        HeaderCell.HorizontalAlignment = Element.ALIGN_CENTER;
        nestedTable.AddCell(HeaderCell);
    }




    for (int i = 0; i <= 3*556; ++i)
    {
        nestedTable.AddCell(new Phrase("Test Cell"));
    }


    PdfPCell nestedTableCell = new PdfPCell(nestedTable);
    nestedTableCell.Colspan = 4;

    mainTable.AddCell(new PdfPCell());
    mainTable.AddCell(nestedTableCell);


    // test adding cells to mainTable in groups of 5
    for (int i = 0; i <= 5 * 10; ++i)
    {
        PdfPCell mainTableCell = new PdfPCell(new Phrase("Test"));
        mainTable.AddCell(mainTableCell);
    }

    return mainTable;
}

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="PageHeadContentPlaceHolder" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageTitleContentPlaceHolder" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageBodyContentPlaceHolder" Runat="Server">
</asp:Content>

1 个答案:

答案 0 :(得分:4)

经过一段时间的努力,我终于认定这是图书馆的一个缺陷。我去看看是否有更新的版本。事实上是。我使用的是4.1.6版。现在有5.0.0版本。此代码在该版本中正确运行。

在我更换了dll后,我还添加了

mainTable.SplitLate = false;
mainTable.SplitRows = true;

之后

private PdfPTable GenerateNestedTableTest()
{
    PdfPTable mainTable = new PdfPTable(5);

修复嵌套表在新页面上的问题。现在一切似乎都很好。也许这篇文章会帮助旧版本的其他人。