empira / PDFsharp

PDFsharp and MigraDoc Foundation for .NET 6 and .NET Framework
https://docs.pdfsharp.net/
Other
492 stars 114 forks source link

Rendering a document throws an `InvalidOperationException` if a paragraph gets close to the bottom of a page #83

Closed spl-sotax closed 7 months ago

spl-sotax commented 8 months ago

We observed the following exception while rendering a document using MigraDoc:

Unhandled exception. System.InvalidOperationException: 'Rectangle' must not be null here.
   at PdfSharp.Diagnostics.NRT.ThrowOnNull[TResult](String message)
   at MigraDoc.Rendering.ParagraphRenderer.StoreBottomBorderInformation()
   at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
   at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
   at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()

I do have a code state that can reproduce the issue reliably, I was not able to extract the needed steps in a way I can share that code yet. I'll add that, once I managed to create an appropriate example.

Expected Behavior

The document is rendered.

Actual Behavior

The following exception is thrown:

Unhandled exception. System.InvalidOperationException: 'Rectangle' must not be null here.
   at PdfSharp.Diagnostics.NRT.ThrowOnNull[TResult](String message)
   at MigraDoc.Rendering.ParagraphRenderer.StoreBottomBorderInformation()
   at MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)
   at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
   at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
   at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
   at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
   at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()

Steps to Reproduce the Behavior

I'll try to create a project that can reproduce the issue, but I was not successful yet.

spl-sotax commented 8 months ago

I was able to create a minimal document and export the MDDDL. I attached the issue template zip where the MigraDoc project reads the MDDDL and tries to render it, raising the mentioned exception on my system. Issue.zip

I will provide a better example if I manage to recreate the issue within the template project.

ThomasHoevel commented 8 months ago

Thanks for your submission. I can replicate the issue, but don't have a solution yet.

martinossendorf commented 7 months ago

We were able to reproduce the issue and to isolate the cause for the exception. In the latest version of MigraDoc, there's a problem getting the correct bottom border offset for the paragraph for page break calculation, if the paragraph ends with some kind of invisible elements like a tab or a text containing only one or more spaces. We fixed this bug and You'll get the fix with the next prerelease. Meanwhile, You can avoid the problem by removing unnecessary tabs or spaces at the end of a paragraph. In Your MDDDL example, You should remove the tabs and spaces in line 214 after "Signature:".

meenzen commented 2 weeks ago

Should this be fixed by now?

Because I'm seeing this exception with PDFsharp-MigraDoc versions 6.2.0-preview-1 and 6.1.1:

System.InvalidOperationException: 'Rectangle' must not be null here.                                                                                                                                    
    at TResult PdfSharp.Diagnostics.NRT.ThrowOnNull<TResult>(string message)                                                                                                                              
    at void MigraDoc.Rendering.ParagraphRenderer.StoreBottomBorderInformation()                                                                                                                           
    at void MigraDoc.Rendering.ParagraphRenderer.Format(Area area, FormatInfo previousFormatInfo)                                                                                                         
    at void MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, bool topLevel)                                                                                                               
    at void MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)                                                                                                                                    
    at void MigraDoc.Rendering.DocumentRenderer.PrepareDocument(RenderEvents renderEvents)                                                                                                                
    at void MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(bool prepareCompletely)                                                                                                        
    at void MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()                                                                                                                                   
    at void MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()

I will try to provide a reproduction.

ThomasHoevel commented 2 weeks ago

Should this be fixed by now?

Yes, I think the issue here should be fixed. But maybe you found a new issue that triggers the same NRT check, but for a different reason.

I will try to provide a reproduction.

That would be good.