empira / PDFsharp-1.5

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

PdfReader.Open throws System.ArgumentOutOfRangeException: Non-negative number required #89

Closed theFra985 closed 1 year ago

theFra985 commented 5 years ago

Expected Behavior

Opening the file without errors

Actual Behavior

Throws an ArgumentOutOfRangeException on PdfSharp.Pdf.IO.Parser.MoveToObject

Unhandled Exception: System.ArgumentOutOfRangeException: Non-negative number required.
Parameter name: value
   at System.IO.FileStream.set_Position(Int64 value)
   at PdfSharp.Pdf.IO.Lexer.set_Position(Int32 value)
   at PdfSharp.Pdf.IO.Parser.MoveToObject(PdfObjectID objectID)
   at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream)
   at PdfSharp.Pdf.IO.Parser.GetStreamLength(PdfDictionary dict)
   at PdfSharp.Pdf.IO.Parser.ReadObject(PdfObject pdfObject, PdfObjectID objectID, Boolean includeReferences, Boolean fromObjecStream)
   at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
   at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
   at PdfSharp.Pdf.IO.PdfReader.Open(String path, PdfDocumentOpenMode openmode)
   at IssueSubmission.Program.Main() in C:\[PathToSolution]\PDFsharp-IssueSubmission\IssueSubmission\Program.cs:line 20

Steps to Reproduce the Behavior

You only need to open a PdfReader with the given PDF I've found the error with more than a single PDF PDFsharp-ArgumentOutOfRange.zip

theFra985 commented 5 years ago

I don't know if it can be of any help but the error can be avoided with this process:

ThomasHoevel commented 5 years ago

Problems caused by corrupt PDF files can usually be worked around by simply opening the file with Adobe Reader and invoking Save As from the menu. Adding and removing a password most likely has the same effect, but "saving the file" is the step that makes a difference..

theFra985 commented 5 years ago

I've tried saving it before but it didn't work

Alexey2040 commented 1 year ago

I have the same issue with several documents. ReSaving files in adobe reader does not solve the issue.

ThomasHoevel commented 1 year ago

Thank you for your feedback and for providing the PDF file. I was able to replicate the issue. The PDF file is not corrupt. Beginning with PDFsharp 6.0.0-preview-3 (ETA not yet set), PDFsharp is able to read this file and other files with the same arrangement.