Blog
[Resolved] PdfSharp – invalid entry in XRef table
Intro
If you found us through Google, welcome! Api2Pdf is a REST API that helps developers generate and interact with PDF files. Among our most popular endpoints is merging or combining PDFs together. To support the merging of PDF files, behind the scenes, our API uses the MIT-licensed PdfSharpCore library. You may be using this library yourself for your own application. On our end, we noticed an increase error rate for people using this endpoint with an error similar that of below:
PdfSharp.Pdf.IO.PdfReaderException: Invalid entry in XRef table, ID=67, Generation=0, Position=0, ID of referenced object=4, Generation of referenced object=0
We had to really do some digging to find out what was going on. As it turns out, this is a result of some PDFs being in a corrupted state, and the library is throwing this error if it tries to open a corrupted PDF. However, we found and deployed the solution to our production servers.
Solution
First, make sure you are using the latest version of PdfSharpCore library. As of the writing of this article, it is up to version 1.3.7.
Next, in your code, where you are opening the PDF file into memory, there are many overload methods available on PdfReader.Open(…). What you want is to use an overload method that can use the PdfReadAccuracy enum. Choose PdfReadAccuracy.Moderate. See below.
That does the trick and it no longer throws an error on the corrupt PDFs.