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

2

20

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".

Finalize answered 22/4, 2016 at 8:28 Comment(0)
I
32

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

Irk answered 22/4, 2016 at 13:28 Comment(1)
@SiyavashHamdi The method FromGdiPlusImage is only available with the GDI+ build, not with the Core build or the WPF build. Use the GDI projects or the GDI NuGet package.Irk
T
6

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!!!

Tied answered 21/11, 2017 at 18:3 Comment(2)
The op clearly uses pdfsharp, not iText. In such a case it is considered bad style to answer with a solution based on that different pdf library.Collier
Answer from knitTheCode is relevant as there are no ways to solve this problem using only PDFSharp (except for beta versions but whose can't be used in production environments), as stated by PDFSharp admin in link provided. I also ended up implemented this solution and, even if you have to use another library, it works nicelyPsychogenesis

© 2022 - 2024 — McMap. All rights reserved.