PDFsharp icon indicating copy to clipboard operation
PDFsharp copied to clipboard

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

Open spl-sotax opened this issue 2 years ago • 9 comments

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 avatar Feb 01 '24 14:02 spl-sotax

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.

spl-sotax avatar Feb 02 '24 09:02 spl-sotax

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

ThomasHoevel avatar Feb 05 '24 12:02 ThomasHoevel

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:".

martinossendorf avatar Feb 20 '24 09:02 martinossendorf

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.

meenzen avatar Sep 19 '24 08:09 meenzen

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.

ThomasHoevel avatar Sep 19 '24 08:09 ThomasHoevel

I believe that this is still occurring when the Paragraph has no text at all. I will also try to create a minimal reproduction.

DanSmith avatar Mar 14 '25 00:03 DanSmith

I believe that this is still occurring when the Paragraph has no text at all.

If this still occurs with the latest version (6.2.0 Preview 3 at the moment), we will re-open this issue. An MCVE would be nice.

ThomasHoevel avatar Mar 18 '25 07:03 ThomasHoevel

Thanks Thomas,

I was able to create an MCVE for you that shows the issue using MigraDoc:

  • variable row count tables
  • empty paragraphs with a border
  • 8 point font

You can find it in the src\MigraDoc\src\IssueSubmission\Program.cs of the attached Issue.zip. Issue.zip

DanSmith avatar Mar 20 '25 00:03 DanSmith

Hi @ThomasHoevel, should we reopen this issue or should I create a new one?

DanSmith avatar Apr 10 '25 16:04 DanSmith