empira / PDFsharp-1.5

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

NullReferenceException in PdfTrailer.cs #85

Open Azkarell opened 5 years ago

Azkarell commented 5 years ago

Hi, I recently had to write a simple Application to merge some PDFs. In most cases PDFSharp worked like a charm but I had one PDF where PDFReader.Open threw a NullReferenceException.

bei PdfSharp.Pdf.Advanced.PdfTrailer.Finish() in C:\repos\PDFsharp\src\PdfSharp\Pdf.Advanced\PdfTrailer.cs:Zeile 196.
   bei PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider) in C:\repos\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:Zeile 506.
   bei PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider) in C:\repos\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:Zeile 216.
   bei PdfSharp.Pdf.IO.PdfReader.Open(String path, PdfDocumentOpenMode openmode) in C:\repos\PDFsharp\src\PdfSharp\Pdf.IO\PdfReader.cs:Zeile 186.

Sadly I can't give you the pdf. But i will try to reproduce the error with other pdfs.

my workaround was: instead of

  if (iref != null && iref.Value == null)
  {
       iref = _document._irefTable[iref.ObjectID];
       Debug.Assert(iref.Value != null);
       _document._trailer.Elements[Keys.Info] = iref;
  }

i used:

 if (iref != null && iref.Value == null)
 {
    iref = _document._irefTable[iref.ObjectID];
    if(iref != null) { 
         _document._trailer.Elements[Keys.Info] = iref;
    }
 }

but I'm really not sure if that is a good idea :)

Azkarell commented 5 years ago

I spent some time learning how pdf works and i found that the pdf seems to be broken, because in one obj

/Info 3 0 R

is defined as a reference but the whole pdf does not inculde a

3 0 obj

how to handle such cases is up to you but since most readers can still open it. pdfsharp should in my opinion at least not crash.