Reputation: 303
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:
Upvotes: 2
Views: 6882
Reputation: 4091
Just for a completion of possible causes, this also occurs when iTextSharp fails to find the path to your TTF font resource.
BaseFont.CreateFont(pathToUniFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
It remembers it when tries to close the document... (Arrrrrr...)
And here are some more unusual reasons:
http://itextsharp.10939.n7.nabble.com/document-has-no-pages-grrr-td906.html
Upvotes: 1
Reputation: 55417
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.
Dim table As New PdfPTable(12)
Related, you probably have a glitch or a copy and paste error because you keep settings the colspan
on the same variable over and over.
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.
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()
Upvotes: 3