Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'PDFsharp cannot handle this PDF feature introduced with Acrobat 6' error while opening PDF file

I use PDFsharp (v1.32) for merging several PDF files. I open documents using this code:

PdfDocument inputDocument = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);

And while opening one document (with PDF version 1.5 (Acrobat 6.x)) I receive an exception:

An unhandled exception of type 'PdfSharp.Pdf.IO.PdfReaderException' occurred in PdfSharp.dll Additional information: Cannot handle iref streams. The current implementation of PDFsharp cannot handle this PDF feature introduced with Acrobat 6.

What can I do with it? I need to merge all files, I cannot just skip it. I tried to find solution, but found not answered or just very old feedback from PDFsharp Team that they are going to "fix it".

like image 608
user2216 Avatar asked Apr 22 '16 08:04

user2216


People also ask

Why am I getting the error message when opening a PDF?

The “Failed to Load PDF Document” error message indicates that the web browser you are using, Google Chrome, is trying to open the electronic transcript within its own native PDF viewer. Because the transcript is a secured PDF, it must be opened with Adobe Acrobat Reader.

Is PDFsharp free?

PDFsharp and MigraDoc Foundation are published Open Source and under the MIT License and are free to use.


2 Answers

Use PDFsharp 1.50 beta 3 from December 2015 or a newer version.

https://www.nuget.org/packages/PdfSharp/1.50.4820-RC1
https://www.nuget.org/packages/PDFsharp-gdi/1.50.4820-RC1
https://www.nuget.org/packages/PDFsharp-wpf/1.50.4820-RC1

https://github.com/empira/PDFsharp

like image 124
I liked the old Stack Overflow Avatar answered Oct 25 '22 09:10

I liked the old Stack Overflow


You can use iText5 or iText7 to remove the iref streams.

iText5 block below is pulled from http://forum.pdfsharp.net/viewtopic.php?f=2&t=693

static public PdfDocument Open(MemoryStream sourceStream)
  {
     PdfDocument outDoc = null;
     sourceStream.Position = 0;

     try
     {
        outDoc = PdfReader.Open(sourceStream, PdfDocumentOpenMode.Import);
     }
     catch (PdfSharp.Pdf.IO.PdfReaderException)
     {
        //workaround if pdfsharp doesn't support this pdf
        sourceStream.Position = 0;
        MemoryStream outputStream = new MemoryStream();
        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sourceStream);
        iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream);
        pdfStamper.FormFlattening = true;
        pdfStamper.Writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
        pdfStamper.Writer.CloseStream = false;
        pdfStamper.Close();

        outDoc = PdfReader.Open(outputStream, PdfDocumentOpenMode.Import);
     }

     return outDoc;
  }

I had to re-write it for iText7 (still using the old PDFSharp):

static PdfDocument CompatibleOpen(MemoryStream inputStream, PdfDocumentOpenMode openMode)
{
 PdfDocument pdfDocument = null;
 inputStream.Position = 0;

 try
 {
    pdfDocument = PdfReader.Open(inputStream, openMode);
 }
 catch (PdfSharp.Pdf.IO.PdfReaderException)
 {
    inputStream.Position = 0;
    MemoryStream outputStream = new MemoryStream();

    iText.Kernel.Pdf.WriterProperties writerProperties = new iText.Kernel.Pdf.WriterProperties();
    writerProperties.SetPdfVersion(iText.Kernel.Pdf.PdfVersion.PDF_1_4);

    iText.Kernel.Pdf.PdfReader pdfReader = new iText.Kernel.Pdf.PdfReader(inputStream);

    iText.Kernel.Pdf.PdfDocument pdfStamper = new iText.Kernel.Pdf.PdfDocument(pdfReader, new iText.Kernel.Pdf.PdfWriter(outputStream, writerProperties)); 

    iText.Forms.PdfAcroForm pdfForm = iText.Forms.PdfAcroForm.GetAcroForm(pdfStamper, true);
    if (!pdfForm.IsNull())
    {
       pdfForm.FlattenFields();
    }
    writerProperties.SetFullCompressionMode(false);

    pdfStamper.GetWriter().SetCloseStream(false);           
    pdfStamper.Close();

    pdfDocument = PdfReader.Open(outputStream, openMode);
 }
 return pdfDocument;
}

I hope this helps someone out there going through the same pain I was, and saves them a few days!!!

like image 6
knitTheCode Avatar answered Oct 25 '22 10:10

knitTheCode