itext7 table SetFixedPosition on Page 1 fails

2019-08-26 06:01发布

问题:

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

回答1:

This was a good hint. I set "immediateFlush" at the creation of "Document" to false. Then the page remains "editable" and I cant paste the table to the first page.



标签: itext itext7