多个页面上的 ItextSharp 嵌套表导致 NullReferenceException

发布于 2024-08-21 00:33:48 字数 3100 浏览 4 评论 0原文

执行以下代码时,我收到 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>

I am getting a NullReferenceException when the following code is executed. I have also noticed that the nested table appeared on a new page when I added in the code that wrote cells to the main table. It doesn't occur if I take out the two loops that write cells to the main table.

<%@ 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-08-28 00:33:48

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

替换完 dll 后,我还

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

立即

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

添加了修复嵌套表位于新页面上的问题。现在一切似乎都运转良好。也许这篇文章会对使用旧版本的其他人有所帮助。

After struggling with it for awhile, I finally decided it was a flaw in the library. I went to look and see if there was a newer version. There infact was. I was using version 4.1.6. There is now a version 5.0.0. This code runs correctly in that version.

After I replaced the dlls I also added

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

right after

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

to fix the problem of the nested table being on a new page. Everything seems to work really well now. Maybe this post will help someone else that is on an old version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文