empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 588 forks source link

System.OverflowException: 'Value was either too large or too small for an Int64 On Reopening a PDF saved using PDFSharp #160

Closed umairamin closed 1 year ago

umairamin commented 2 years ago

Reporting an Issue Here

There appear to be a bug in PDFSharp, when a specific PDF is opened using PDF sharp then saved without any modifications, upon reopening this error occurs "System.OverflowException: 'Value was either too large or too small for an Int64.'"

I was able to determine that issue occurs because inside that PDF we had an array with this numeric value "-9223372036854775808" when PDFSharp reads the file first time it reads that value as Real Value, but upon saving the saved value is a value less than minimum value of Int64 therefore on reopening file throws an error

The original file that had this issue was confidential therefore I have generated a test file with same issue

Expected Behavior

The file should open when saved

Actual Behavior

After file is saved and we try to reopen it we get an exception

Steps to Reproduce the Behavior

Open this file using PDFSharp, save it and then try to reopen TEST_ReproduceIssue.pdf

            string File =  "TEST_ReproduceIssue.pdf";
            using (PdfDocument one = PdfReader.Open(File, PdfDocumentOpenMode.Modify))
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    one.Save(ms, false);
                    ms.Seek(0, System.IO.SeekOrigin.Begin);
                    using (PdfDocument two = PdfReader.Open(ms, PdfDocumentOpenMode.Modify))//We'll get exception here
                    {

                    }
                }
            }
ThomasHoevel commented 1 year ago

This issue can be resolved by using "Config.SignificantFigures1Plus9" as shown here in class PdfWriter:

public void Write(PdfReal value) { WriteSeparator(CharCat.Character); // Ensure that value is written with decimal point. WriteRaw(value.Value.ToString(Config.SignificantFigures1Plus9, CultureInfo.InvariantCulture)); _lastCat = CharCat.Character; }

Fix will be included in the next release of PDFsharp. I cannot give an ETA now.

ThomasHoevel commented 1 year ago

As of 6.0.0-preview-3 there is a fix in ScanNumber and the hack in Write is no longer needed.