Closed dkoehler69 closed 1 year ago
Files opened without the Modify flag cannot be saved.
The workaround demonstrates that the PDF can be saved just by evaluating PdfDocument.PageCount
before calling PdfDocument.Save()
. Calling PdfDocument.PageCount
initialises _pages
. Then the if (_pages == null || _pages.Count == 0)
check is passed. This seems to indicate a flaw in the whole process to me.
(A caveat: My bug report was from 2018. I did not check, whether it is obselete meanwhile.)
The bug report is obsolete with PDFsharp 6.0.0-preview-3 coming next month or so.
I discovered a bug in the PdfDocument.Save functions.
Expected Behavior
Saving without exception.
Actual Behaviour
An InvalidOperationException("Cannot save a PDF document with no pages.") is triggered on a freshly opened PdfDocument, even if it contains pages.
This behaviour is only triggered in the compiled programme or during uninterrupted code runs in the debugger (I am using VS 2017). When using step by step debugging (F10), the exception is not triggered.
Steps to Reproduce the Behavior
Calling the following Test() function with a valid PDF byte array triggers the InvalidOperationException when reaching the pdfDoc.Save(pdfFilePath) instruction. All the other checks before are passed successfully.
Workaround
Insert the following line in front of
pdfDoc.Save(pdfFilePath);
:if (pdfDoc.PageCount == 0) { throw new FormatException("PDF is empty"); }
Possible Fix
The exception is thrown when in the
void DoSave(PdfWriter writer)
function of thePdfDocument
class the following condition is met:if (_pages == null || _pages.Count == 0)
The bug is caused by the_pages
variable not being initialised. This also explains the workaround: CallingPdfDocument.PageCount
triggers the initialisation of_pages
.A possible fix of the bug is to replace
_pages
withPages
in the incriminated line, which triggers the initialisation. Here is a compact version of the fix that avoids to attempt the initialisation twice:if ((Pages?.Count ?? 0) == 0)