Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I can´t believe, what I see.... I try to print a table with itext7 into a PDF on a defined page. The table-creation is made as last statement before finishing the PDF.

The PDF has (after finishing) 5 pages. I can print the table to every page except the first page.

This is my code:

    If placeOnPage = 0 Then
        table.SetFixedPosition(200, 250, 100)
        table.SetFixedPosition(placeOnPage, 200, 250, 100)
    End If

If placeOnPage = 0, then the table is printed correctly on the last page. If I set placeOnPage to values between 2 and 5, the table is correctly placed on page 2, 3, 4 or 5. But if I set placeOnPage to 1, I get an error at the line

doc.add(table)

The error says:

System.NullReferenceException: "object reference not set to an instance of an object"

I don´t know which object is NULL. Everythin else is exactly the same in all my tries....

Can anybody give me a hint?

Regards,

Benni

StackTrace:

   bei KernelExtensions.Get[TKey,TValue](IDictionary´2 col, TKey key)
   bei iText.Kernel.Pdf.PdfDictionary.Get(PdfName key, Boolean asDirect)
   bei iText.Kernel.Pdf.PdfDictionary.Get(PdfName key)
   bei iText.Kernel.Pdf.PdfPage.GetContentStreamCount()
   bei iText.Kernel.Pdf.PdfPage.GetLastContentStream()
   bei iText.Kernel.Pdf.Canvas.PdfCanvas.GetPageStream(PdfPage page)
   bei iText.Kernel.Pdf.Canvas.PdfCanvas..ctor(PdfPage page, Boolean wrapOldContent)
   bei iText.Layout.Renderer.DocumentRenderer.FlushSingleRenderer(IRenderer resultRenderer)
   bei iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
   bei iText.Layout.RootElement´1.CreateAndAddRendererSubTree(IElement element)
   bei iText.Layout.RootElement´1.Add(IBlockElement element)
   bei iText.Layout.Document.Add(IBlockElement element)
   bei iTextPDF.class_iTextPDF.TableEnd() in D:\Documents\15 - Visual Studio Projects\iTextPDF\iTextPDF\iTextPDF.vb:Zeile 1668.
   bei iTextPDF.class_iTextPDF.addTextFrame(String text, String posLowerLeftPointFromLeft, String posLowerLeftPointFromBottom, String width, String height, PdfFont font, Single fontSize, Color fontColor, Single strokeWidth, Color strokeColor, Color fillColor, TextAlignment alignment, String innerSpacing, Int32 placeOnPage) in D:\Documents\15 - Visual Studio Projects\iTextPDF\iTextPDF\iTextPDF.vb:Zeile 1360.
   bei iTextPDFTest.Form1.Button1_Click(Object sender, EventArgs e) in D:\Documents\15 - Visual Studio Projects\iTextPDFTest\iTextPDFTest\Form1.vb:Zeile 225.
   bei System.Windows.Forms.Control.OnClick(EventArgs e)
   bei System.Windows.Forms.Button.OnClick(EventArgs e)
   bei System.Windows.Forms.Button.PerformClick()
   bei iTextPDFTest.Form1.Form1_Load(Object sender, EventArgs e) in D:\Documents\15 - Visual Studio Projects\iTextPDFTest\iTextPDFTest\Form1.vb:Zeile 9.
   bei System.EventHandler.Invoke(Object sender, EventArgs e)
   bei System.Windows.Forms.Form.OnLoad(EventArgs e)
   bei System.Windows.Forms.Form.OnCreateControl()
   bei System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   bei System.Windows.Forms.Control.CreateControl()
   bei System.Windows.Forms.Control.WmShowWindow(Message& m)
   bei System.Windows.Forms.Control.WndProc(Message& m)
   bei System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   bei System.Windows.Forms.Form.WmShowWindow(Message& m)
   bei System.Windows.Forms.Form.WndProc(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
                "I don´t know which object is NULL" please show the stack trace of the error. Chances are, though, that the pdf in question and enough code to reproduce the error with it also are required to analyze the issue.
– mkl
                Nov 21, 2018 at 18:50
                A NullReferenceException there sounds like the page dictionary object has already been released at the time this happens. iText usually attempts to flush generated pages to output as early as possible and then releases their data. Probably your code triggers that flushing before adding that table to page 1? To analyze this better more code is necessary.
– mkl
                Nov 22, 2018 at 10:47
                Yes, this is shurely the reason. What I try is to add the table to the first page at the very end of the generation of the PDF document. The document finally has 5 pages, and just before doc.close I want to add the table to page 1.  So this is not possible? And what about opening an existing document and adding a table to the first page?
– BeSt
                Nov 22, 2018 at 10:56
                But now I have another question following on the first:  I open a existing PDF with this code snippet: reader = New PdfReader(filenameSource)    writer = New PdfWriter(destFile)  ///  pdf = New PdfDocument(reader, writer)  ///   doc = New Document(pdf, pdf.GetDefaultPageSize, False) I can add a parapgrah now via doc.add(new Paragraph(...))  But when I try to place a table with table.setFixedPosition(...), the table doesn´t show on the page.
– BeSt
                Nov 22, 2018 at 11:30
                "immediateFlush" - yes, this is what I would have proposed now. "But now I have another question" - please make that an actual new stack overflow question.
– mkl
                Nov 22, 2018 at 11:33
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.