2016-11-08 120 views
0

當我將多個表格添加到iText7 pdf並且表格的數量超過一頁時,會拋出以下異常。itext7不會自動創建新的pdf頁面

異常:「未將對象引用設置爲對象的實例」。 來源: 「itext.layout」 堆棧跟蹤:「在 iText.Layout.Renderer.TableRenderer.Layout(LayoutContext layoutContext) 在iText.Layout.Renderer.RootRenderer.AddChild(IRenderer渲染器) 在iText.Layout.RootElement 1.Add[T2](BlockElement 1元素) at iText.Layout.Document.Add C:\ Users \ me \ Documents \ Visual Studio中的[T](BlockElement`1元素) at iTextSharp7_Test.Controllers.PdfController.ReplicateBug(String pdfFile) 2015 \ Projects \ App_Test \ iTextSharp7_Test \ Controllers \ PdfController.cs:line 443「

在提供的示例中,當即將添加第6個表時,會引發異常。

我一直能夠處理這個問題的唯一方法是通過假設通過向pdf中添加更多數據而不是適合一個頁面並在try catch塊中添加一個新頁面來引發異常從長遠來看非常醜陋和不好。

實施例:

public void ReplicateBug(string pdfFile) 
{ 
    iText.Kernel.Pdf.PdfWriter writer = null; 
    iText.Kernel.Pdf.PdfDocument pdf = null; 
    iText.Layout.Document document = null; 

    try 
    { 
     writer = new iText.Kernel.Pdf.PdfWriter(pdfFile); 
     pdf = new iText.Kernel.Pdf.PdfDocument(writer); 
     document = new iText.Layout.Document(pdf, iText.Kernel.Geom.PageSize.A4); 
     document.Add(new iText.Layout.Element.Paragraph("*** PRODUCTS ***")); 
     iText.Kernel.Pdf.Canvas.Draw.DashedLine dashedLine = new iText.Kernel.Pdf.Canvas.Draw.DashedLine(); 
     document.Add(new iText.Layout.Element.LineSeparator(dashedLine)); 

     iText.Layout.Element.Table table = null; 

     for (int i = 0; i < 10; ++i) 
     { 
      iText.Layout.Element.Cell cell = null; 

      table = new iText.Layout.Element.Table(2); 
      table.SetMarginTop(10); 

      cell = new iText.Layout.Element.Cell().Add("-- PRODUCT --"); 
      table.AddCell(cell); 
      cell = new iText.Layout.Element.Cell(3, 1).Add("image"); 
      cell.SetHorizontalAlignment(iText.Layout.Properties.HorizontalAlignment.CENTER); 
      cell.SetVerticalAlignment(iText.Layout.Properties.VerticalAlignment.MIDDLE); 
      table.AddCell(cell); 
      cell = new iText.Layout.Element.Cell().Add("Product text." + Environment.NewLine + "Welcome"); 
      cell.SetHeight(75); 
      table.AddCell(cell); 
      cell = new iText.Layout.Element.Cell().Add((i + 1).ToString().PadLeft(10, '0')); 
      table.AddCell(cell); 

      try 
      { 
       document.Add(table); 
       document.Add(new iText.Layout.Element.LineSeparator(dashedLine)); // Default LineWidth is 1 
      } 
      catch 
      { 
       // NOTE: After adding 5 tables to the pdf an exception is thrown of the following kind. 

       // Exception: "Object reference not set to an instance of an object." 
       // Source: "itext.layout" 
       // StackTrace: "at iText.Layout.Renderer.TableRenderer.Layout(LayoutContext layoutContext) 
       //    at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer) 
       //    at iText.Layout.RootElement`1.Add[T2](BlockElement`1 element) 
       //    at iText.Layout.Document.Add[T](BlockElement`1 element) 
       //    at iTextSharp7_Test.Controllers.PdfController.ReplicateBug(String pdfFile) in C:\\Users\\me\\Documents\\Visual Studio 2015\\Projects\\App_Test\\iTextSharp7_Test\\Controllers\\PdfController.cs:line 443" 
      } 
     } 

     document.Close(); 
     pdf.Close(); 
     writer.Close(); 
    } 
    catch 
    { 
     if (document != null) 
     { 
      document.Close(); 
     } 

     if (pdf != null) 
     { 
      pdf.Close(); 
     } 

     if (writer != null) 
     { 
      writer.Close(); 
     } 

     throw; 
    } 
} 

由於提前

回答

1

此問題已經被固定在7.0.2(和7.0.2-SNAPSHOT,這被稱爲在7.0.1.1.NET版本)。快照NuGet包可以從Artifactory下載。 7.0.2發佈版本將在最近幾周內在NuGet上提供。