vb.net iTextSharp - 使用表格时文档没有页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25771493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:10:35  来源:igfitidea点击:

iTextSharp - Document has no pages when working with table

.netvb.netitextsharp

提问by jgetrost

    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()

after executing this code I get a "document has no pages" error. I have found a few others with this error but their issue was that they forgot to add the table to the document, So I'm not sure why I'm getting this error.

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

More detail:

更多详情:

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: 

回答by Chris Haas

You are asking iTextSharp to create a 12 column table but you are only using 4 of those columns, 3 cells with 1 cell spanned 2 columns.

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

Dim table As New PdfPTable(12)

Related, you probably have a glitch or a copy and paste error because you keep settings the colspanon the same variable over and over.

相关的,您可能会遇到故障或复制和粘贴错误,因为您colspan一遍又一遍地将设置设置在同一个变量上。

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

iTextSharp ignores incomplete rows by default and since you only have a single row and it isn't complete you have zero content.

默认情况下,iTextSharp 会忽略不完整的行,并且由于您只有一行并且不完整,因此您的内容为零。

You can change your column count in the constructor to what you're actually going to use:

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

Dim table As New PdfPTable(4)

If my assumption about your typo is correct then you probably actually want this when fixed:

如果我对你的错字的假设是正确的,那么你可能真的想要这个:

Dim table As New PdfPTable(6)

If you've got a valid reason to not actually use the same number of columns that you specify you can use this right before adding the table to the document to "fill in the blanks".

如果您有正当理由不实际使用您指定的相同数量的列,则可以在将表格添加到文档以“填空”之前使用它。

table.CompleteRow()

回答by NoOne

Just for a completion of possible causes, this also occurs when iTextSharp fails to find the path to your TTF font resource.

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

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

It remembers it when tries to close the document... (Arrrrrr...)

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

And here are some more unusual reasons:

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

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

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