0
votes

We are making some changes to PDF files programatically using PDFsharp. We are using the latest stable version: 1.32.4334.

http://www.pdfsharp.net/NuGetPackage_PDFsharp-MigraDoc-GDI.ashx?AspxAutoDetectCookieSupport=1

I try to work with pdf files like this;

//inputStream is memory stream 
var doc = PdfReader.Open(inputStream); 

On some files we get this error when trying to open the PDF like mentioned in the code above;

Cannot handle iref streams. The current implementation of PDFsharp cannot handle this PDF feature introduced with Acrobat 6

I have googled a lot, and see a lot of people have this problem, but I have not really found any good solution. The solutions from the web are:

1.

try
{
}
catch (PdfReaderException pdfException)
{
// Do nothing
}

2. Use beta version 1.50 of PDFsharp.

A third approach could be to use it in combination with iTextSharp, but I have experienced that it gives problem using these two libraries in combination.

My problem is this: The try-catch won't work for us, because we need to manipulate the files, and using a beta version in production environment might not be a good idea - specially when it has been in beta since December 2015.

Is PDFsharp "dead", since they have been in beta with version 1.50 for more than a year?

Any other things I can try?

1
Did you ever fix this issue? - hvaughan3
Yes. Used the beta version. - Ilyas
Thanks for the response. I ended up using iTextSharp myself. Neither solution is preferred. - hvaughan3

1 Answers

0
votes

I found that the problem (in my case) specifically applies to PDF files where the cross-reference xref table is not a standalone element in the file as it was in versions up to and including 1.4, but has been promoted to an indirect object stream with /Type equal to /XRef as per version 1.5 of the specification.

PDFsharp's own FQA page http://www.pdfsharp.net/wiki/PDFsharpFAQ.ashx says this:

Some features of PDF 1.5 (Adobe Reader 6.0) are not yet implemented. Therefore PDFsharp cannot yet open all files marked for PDF 1.5 or higher (but that's on our Roadmap).

The aforementioned roadmap is "not a commitment" and at 31 Jan 2018 still says:

Support of iref streams - the most wanted feature: works with some files while other files still cause problems

Other than using their beta (I'll admit, I personally wouldn't want to do that in a production environment either) I don't see many available options that don't involve other libraries. With the goal of preserving and reusing all my existing PDFsharp compatible code, my first step (assuming that I didn't require any other 1.5+ features) would be to try and downconvert the file to an earlier version format, then re-open it in PDFsharp:

PdfDocument doc;
try
{
    doc = PdfReader.Open(path);
}
catch (PdfReaderException e) when (e.Message == "Cannot handle iref streams. The current implementation of PDFsharp cannot handle this PDF feature introduced with Acrobat 6.")
{
    // TODO: try open path with another library
    // TODO: save path to temp file with version 1.4
    doc = PdfReader.Open(temp);
    // TODO: finally delete temp file
}