gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:22:37 28 4
gpt4 key购买 nike

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

最佳答案

纠结了一段时间后,我最终确定这是库中的一个缺陷。我去看看有没有更新的版本。事实上有。我使用的是 4.1.6 版。现在有一个版本 5.0.0。此代码在该版本中正确运行。

替换dll后我也添加了

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

紧接着

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

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

关于c# - 多个页面上的 ItextSharp 嵌套表导致 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2202254/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com