ITextSharp tabella nidificata su più pagine provoca NullReferenceException
-
18-09-2019 - |
Domanda
mi stanno un NullReferenceException quando viene eseguito il seguente codice. Ho anche notato che la tabella annidata è apparso su una nuova pagina quando ho aggiunto nel codice che ha scritto le cellule alla tabella principale. Non si verifica se prendo i due cicli che scrivono le cellule alla tabella principale.
<%@ 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>
Soluzione
Dopo aver lottato con lui per un po ', alla fine ho deciso che era un difetto nella biblioteca. Sono andato a guardare e vedere se ci fosse una versione più recente. C'è infatti è stato. Stavo usando la versione 4.1.6. V'è ora una versione 5.0.0. Questo codice viene eseguito correttamente in quella versione.
Dopo aver sostituito le DLL Ho anche aggiunto
mainTable.SplitLate = false;
mainTable.SplitRows = true;
dopo
private PdfPTable GenerateNestedTableTest()
{
PdfPTable mainTable = new PdfPTable(5);
per risolvere il problema della tabella nidificata essere su una nuova pagina. Tutto sembra funzionare molto bene ora. Forse questo post sarà aiutare qualcun altro che si trova su una vecchia versione.