【问题标题】:Output in pdf format С# iTextSharp以 pdf 格式输出 С# iTextSharp
【发布时间】:2019-03-12 00:10:52
【问题描述】:

我刚刚开始探索 itextsharp C# 库并遇到了异常。 此异常的类型在此库中是特定的。

形成pdf文档的代码如下:

private static void returnPdf(IEnumerable<object> reportItemList)
{
    var suggestedFileName = "Sales_By_Payment_Type_Report" + ".pdf";
    using (var doc = new Document(PageSize.A4, 60, 60, 30, 30))
    {
        PdfWriter.GetInstance(doc, new FileStream(suggestedFileName, FileMode.Create));
        doc.Open();
        doc.NewPage(); 

        var totalList = reportItemList as IEnumerable<ReportItem>;
        if (totalList != null)
        {
          //filter by 14
          var members = typeof(ReportItem).GetMembers().Where(memb =>memb.MemberType == 
            System.Reflection.MemberTypes.Property && memb.GetCustomAttributes(false).Where(att => (att as ReportProperty)?.PropertyName != String.Empty).Count() != 0);
          var itemNumber = members.Count();
          if (itemNumber != 0)
          { 
            PdfPTable table = new PdfPTable(itemNumber);
            PdfPCell[] itemArray = new PdfPCell[itemNumber];
            for (int i = 0; i < itemArray.Length - 1; i++)
            {
              var customList = members.ElementAt(i).CustomAttributes.Where(t => t.AttributeType == typeof(ReportProperty)).FirstOrDefault()?.ConstructorArguments;
              if (customList.Count != 0) {
                itemArray[i] = new PdfPCell();
                itemArray[i].Phrase = new Phrase(customList[0].Value.ToString());
              }
            }
            PdfPRow pdfRow = new PdfPRow(itemArray);
            table.Rows.Add(pdfRow);                
            //footer
            table.Rows.Add(new PdfPRow(new PdfPCell[itemNumber]));
            try
            {                      

              **doc.Add(table);**

            }
            catch (DocumentException ex)
            {
              throw ex;

            }           

          }
        }

    }
}

在 try 块中,来自 iTextSharp 的 DocumenException 引发了异常

doc,表格不是空值 请帮忙,谢谢

iTextSharp.text.DocumentException:  reference to an object does not indicate an object instance.
    в PdfTesting.Program.returnPdf(IEnumerable`1 reportItemList) в D:\FranPosTest\iconnect-web\PdfTesting\Program.cs:строка 70   
    в PdfTesting.Program.Main(String[] args) в D:\FranPosTest\iconnect-web\PdfTesting\Program.cs:строка 107
    в System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
    в System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    в Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    в System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    в System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
    в System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    в System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 请用英文提供异常信息。
  • 请提供异常消息作为文本。我看不懂。
  • 对象引用不表示对象实例。异常的类型是 - iTextSharp.text.DocumentException
  • 为什么不在这里发布“ex.ToString()”的输出?
  • 不要在catch 块中使用throw ex;。请改用throw;

标签: c# itext


【解决方案1】:

你这样做

//footer
table.Rows.Add(new PdfPRow(new PdfPCell[itemNumber]));

即您基于未设置任何值的新 PdfPCell 数组创建新的 PdfPRow,因此该数组中的所有条目都是 null

当 iText 在 doc.Add(table) 期间尝试布局该表格时,它最终也会尝试布局这一行,并绊倒了所有这些 null 单元格。

【讨论】:

  • 谢谢,非常感谢
猜你喜欢
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
  • 2010-12-05
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多