iTextSharp - 使用表格时,文档没有页面

时间:2014-09-10 17:26:26

标签: .net vb.net itextsharp

    Dim doc As New Document()
    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("Invoice.pdf", FileMode.Create))
    doc.Open()

    Dim table As New PdfPTable(6)
    table.WidthPercentage = 110

    Dim cellDate As New PdfPCell(New Phrase("9/10/14"))
    cellDate.Colspan = 2
    table.AddCell(cellDate)

    Dim cellCompany As New PdfPCell(New Phrase("Company Inc." & vbNewLine & _
                                                   "Sales Person" & vbNewLine & _
                                                   "123 S 150th Road" & vbNewLine & _
                                                   "Somecity, NA 12345" & vbNewLine & _
                                                    "405.555.9999"))
    cellDate.Colspan = 2
    table.AddCell(cellCompany)

    Dim cellInvoiceNo As New PdfPCell(New Phrase("12345"))
    cellDate.Colspan = 2
    table.AddCell(cellInvoiceNo)

    doc.Add(table)
    doc.Close()

执行此代码后,我得到一个“文档没有页面”错误。 我发现其他一些人有这个错误但他们的问题是他们忘了将表添加到文档中,所以我不确定为什么我会收到此错误。

更多细节:

System.IO.IOException was unhandled
  HResult=-2146232800
  Message=The document has no pages.
  Source=itextsharp
  StackTrace:
       at iTextSharp.text.pdf.PdfPages.WritePageTree()
       at iTextSharp.text.pdf.PdfWriter.Close()
       at iTextSharp.text.pdf.PdfDocument.Close()
       at iTextSharp.text.Document.Close()
       at {Project}.Form1.btnDOIT_Click(Object sender, EventArgs e) in C:\Users\James\documents\visual studio 2013\Projects\{Project}\Form1.vb:line 78
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at {Project}.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

2 个答案:

答案 0 :(得分:3)

您要求iTextSharp创建一个12列表,但您只使用其中4列,3个单元格,1个单元格跨越2列。

Dim table As New PdfPTable(12)

相关的,您可能会出现故障或复制并粘贴错误,因为您会一遍又一遍地对同一个变量设置colspan

cellDate.Colspan = 2 ''//This is in the code three times

iTextSharp默认忽略不完整的行,因为您只有一行而且它没有完整,所以内容为零。

您可以将构造函数中的列数更改为您实际要使用的列数:

Dim table As New PdfPTable(4)

如果我对你的拼写错误的假设是正确的,那么你可能在修复时确实想要这个:

Dim table As New PdfPTable(6)

如果您有充分的理由不实际使用指定的相同数量的列,则可以在将表格添加到文档中以“填充空白”之前使用此权限。

table.CompleteRow()

答案 1 :(得分:0)

只是为了完成可能的原因,当iTextSharp无法找到TTF字体资源的路径时,也会发生这种情况。

BaseFont.CreateFont(pathToUniFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

在尝试关闭文档时会记住它...(Arrrrrr ...)

以下是一些不寻常的原因:

http://itextsharp.10939.n7.nabble.com/document-has-no-pages-grrr-td906.html