PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PdfReaderException - "Cannot handle iref streams."
https://forum.pdfsharp.net/viewtopic.php?f=2&t=693
Page 1 of 2

Author:  Thomas [ Thu Apr 02, 2009 1:57 pm ]
Post subject:  PdfReaderException - "Cannot handle iref streams."

Hi together,

I have a little trouble with the component (previously using version 1.0.898.0 of PDFsharp, now updated to 1.2.1269.0). By reading various pdf files and merging these I have some files which seem to be created with a newer version of Adobe Acrobat. Then the component throws an exception of type PdfReaderException with the following message:

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


Looking around brought me these lines of code:

Code:
      Symbol symbol = ScanNextToken();
      // Is it an xref stream?
      if (symbol == Symbol.Integer)
        throw new PdfReaderException(PSSR.CannotHandleXRefStreams);
      // TODO: We have all code to handle them -> just do it


Just to understand that: what's the problem you have to solve here? And do you plan to make that happen next time? ;)

And what can I do to prevent that behaviour from an users point of view? For example I solved that issue one time with replacing the original pdf produced by Acrobat directly by printing it again with a pdf printer (CutePDF). But that does not seem to be a workaround for some hundred files we are working here.

Best regards,

Thomas

Author:  arifzia [ Thu Jun 04, 2009 1:49 pm ]
Post subject: 

Hi PDF Sharp Team,

We are waiting for your response on this thread. It will be better if you can either fix this issue or provide some hints to fix it. This problem will get even worse in future.

Author:  Thomas Hoevel [ Thu Jun 04, 2009 2:57 pm ]
Post subject: 

We are working on PDFsharp 1.3 (to be released later this year).

Author:  researcher [ Mon Aug 03, 2009 10:03 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Just when I thought I had got PDFSharp to do everything I needed... I came across error this trying to open a PDF made in Acrobat.

Any ideas on a time frame for when 1.3 will be released?

Don't really want to have to redo my project with iTextSharp or similar after getting it working so well with PDFSharp.

Author:  Thomas Hoevel [ Tue Aug 04, 2009 8:42 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

PDFsharp 1.3 will be released this week or next week.

Iref streams are still not implemented.
We'll publish a roadmap for future versions of PDFsharp after the release of PDFsharp 1.3.

PDF files compatible with Adobe Reader 5.0 (PDF 1.4) don't contain iref streams.
Not all files that require a newer version of Adobe Reader can be read with PDFsharp.

As a workaround: save the files in Adobe Reader 5.0 format.

Author:  Siva [ Tue Aug 11, 2009 2:10 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Hi,

I am trying to merge the pdf files. Unfortunately I don't have software to save as lower version pdf. I assume in my situation, I have to wait until pdfsharp supports iref.

Just curious is this change complicated, if it is not complicated and if you can give the pointer I will be happy to try it out.

Thanks

Author:  Thomas Hoevel [ Tue Aug 11, 2009 2:30 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Siva wrote:
Just curious is this change complicated, if it is not complicated and if you can give the pointer I will be happy to try it out.

Stefan Lange told me it is complicated.

Author:  davimack [ Thu Sep 24, 2009 8:07 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Perhaps you could give us an option to ignore this? I'm not interested in any of the cross-references, and I doubt that most people are. So ... perhaps it would be nice to provide us with a compiler constant to be able to NOT throw these errors, and just keep on with life? The document would look the same without the cross-references, so just let them go until you can support them, please.

As it is, this is totally unusable unless one has the full version of Adobe, because it's impossible to save things back out without it (or without going through some elaborate process whereby documents are "printed" to an older version).

I appreciate this tool. I would like to keep using it. If it keeps throwing errors like this, for nothing important that I can see, I'll have to scrap it.

Author:  Thomas Hoevel [ Thu Sep 24, 2009 8:25 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

davimack wrote:
Perhaps you could give us an option to ignore this? I'm not interested in any of the cross-references, and I doubt that most people are.

I'm afraid you don't understand what this problem is about. It's not about "cross-references" (are there cross-references in PDF?), it's about how internal structures of PDF are linked.

It's not about new extra information that can be skipped, it's a new internal file structure.
It is complicated.

You don't need the full version of Acrobat.
Require PDF files to be compatible with Adobe Acrobat Reader 5.x and PDFsharp 1.3 will be able to process them.
Normally PDF generators allow you to set the required Acrobat version.

Using Acrobat Professional to reset the required PDF version is one option (you can choose if all others fail).

PDFsharp cannot read the PDF files that use the new structure. But Adobe Reader 5.0 or older can't read them either ("Adobe Acrobat: There was an error opening this document. The file is damaged and could not be repaired.").
If you use the Standard setting of e. g. Adobe Professional 8.1 it will create files compatible with Reader 5.0 (and compatible with PDFsharp).

Author:  davimack [ Fri Sep 25, 2009 9:10 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thomas Hoevel wrote:
It's not about new extra information that can be skipped, it's a new internal file structure.


Thank you for the explanation, Thomas.

My understanding, then, is that you do not intend to provide any workaround (down-versioning pdf's, for example) in the event that the structure is incompatible.

Do you have any suggestion for a tool which would do so? Perhaps iTextSharp?

I ask because I am unable to control the version of pdf coming into our organisation (from British Telecom, for instance), yet need to bundle pdf's together in order to pass them along as part of a larger document.

Author:  Thomas Hoevel [ Mon Sep 28, 2009 7:50 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

davimack wrote:
My understanding, then, is that you do not intend to provide any workaround (down-versioning pdf's, for example) in the event that the structure is incompatible.

It is complicated. A "workaround" would be complicated, the real implementation will be complicated.
We intend to support the new format, but we can't implement it right now.

It's on our RoadMap but I can't tell you when it'll come.

Author:  gfahd [ Tue Oct 06, 2009 5:22 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Hello Thomas,
I am new to PDFSharp, but really like it in comparison to ItextSharp. In order to implement it in our product, we need it to open Adobe 6 and later PDF's. our next release of product is January 2010. Would you have a new release of PDFSharp by then? I understand that you don't know for sure, but any ideas for an estimate of next release? and what are the chances this problem is fixed? We don't have control over the PDF's we open, so we cannot re-save them. Thanks very much for a nice product. It will replace Adobe Acrobat and ImagePDF for us.

Author:  davimack [ Wed Nov 04, 2009 11:53 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thomas Hoevel wrote:
davimack wrote:
...
It is complicated. A "workaround" would be complicated, the real implementation will be complicated.
...


OK, I see it: I read the PDFb specification's description of the structures, and I don't envy you having to support PDFb to the same level that you support PDFa. I look forward to the support, though, because I'd like to be able to manipulate either version using your library. Now, I've had to cobble together something which uses iText instead, and it's not ideal, but I'm stuck: I'm at the mercy of whichever version gets sent to me, and have to provide the functionality to "roll it up" into a larger pdf, to pass on as an invoice with supporting docs. :?

Author:  kdlc [ Mon Nov 23, 2009 5:19 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Just got this error on a workstation ....
Any news/changes on this issue?

Thank you.

Author:  bergamot [ Thu Dec 17, 2009 2:24 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

davimack wrote:
OK, I see it: I read the PDFb specification's description of the structures, and I don't envy you having to support PDFb to the same level that you support PDFa.?


Hi!
So, is this iref streams feature is part of PDF/B? Could you tell how it is called in the specification?
I could not find it as "iref stream"

Alexey

Author:  rasepretrep [ Tue Feb 23, 2010 12:30 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

First of all, let me say that i really love the pdfsharp library. I use it extensively in my current project, which involves merging pdf files and
adding page counters etc. to it.

Unfortunately I have no control over the pdf files that are to be merged, so I sometimes got the "iref stream" error message. At the same time I really want to stick with pdfsharp because it is imho a pdf api with a very nice and clear api.

So I developed an (ugly) workaround which I post here to give back something to this community.
the idea is simple: If pdfsharp fails to import a pdf file, use Itextsharp to open the document and write a compatible pdf, which can then be imported again with pdfsharp.
Here is the code :

Code:
 static public class CompatiblePdfReader
    {

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static public PdfDocument Open(string sFilename)
        {

            PdfDocument reader = new PdfDocument();
           
            try {
                reader = PdfReader.Open(sFilename, PdfDocumentOpenMode.Import);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException) {
                //workaround if pdfsharp doesnt dupport this pdf
                string newName = WriteCompatiblePdf(sFilename);
                reader = PdfReader.Open(newName, PdfDocumentOpenMode.Import);
            }

            return reader;
        }


        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static private string WriteCompatiblePdf(string sFilename)
        {
            string sNewPdf = Globals.TempPath + Guid.NewGuid().ToString() + ".pdf";

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sFilename);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;

            int rotation;

            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            // step 5: we close the document
            document.Close();
            return sNewPdf;
        }
    }


You will have to modify the code a bit to make it work (this is jsut a "copy and patse" from my current project).
I chose itextsharp 4.1.6 because version 5 had problems with some pdfs in my "testsuite".

Author:  ssc [ Mon Mar 08, 2010 3:16 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thanks rasepretrep.

Not that I like adding a new library just to plug a hole, but that worked a charm.

I used your code on v5 of iTextSharp. Only modifications I made were to open the PdfReader from a byte[] (as my pdfs come from db) and then change the writer to write to a MemoryStream.

Cheers for the solution.
Simon.

Author:  tcartwright [ Wed Apr 07, 2010 5:28 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

This works, except I lost all of the form fields from the original pdf which is what I really wanted.

I am not familiar with either library, does anyone know how to modify the conversion code to convert the form fields as well?

Author:  atherton01 [ Wed Apr 28, 2010 10:37 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

For those of you who want to use Visual Basic, and want to use MemoryStreams, this is the implementation that I am using which solves a problem we have had for years now...

Can't tell you how relieved I am! Thanks for the post, "rasepretrep"! It's not ugly! To me, this work around is absolutely beautiful!

Code:
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports System.IO

Public Module CompatiblePdfReader

    ''' <summary>
    ''' uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
    ''' </summary>
    ''' <param name="sFilename"></param>
    ''' <returns></returns>
    Public Function Open(ByVal sFilename As String) As PdfDocument

        Dim reader As New PdfDocument()

        Try
            reader = PdfReader.Open(sFilename, PdfDocumentOpenMode.Import)
        Catch generatedExceptionName As PdfSharp.Pdf.IO.PdfReaderException
            'workaround if pdfsharp doesnt dupport this pdf
            Dim newName As MemoryStream = ReturnCompatiblePdf(sFilename)
            reader = PdfReader.Open(newName, PdfDocumentOpenMode.Import)
        End Try

        Return reader
    End Function


    ''' <summary>
    ''' uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf
    ''' </summary>
    ''' <param name="sFilename"></param>
    ''' <returns></returns>
    Private Function ReturnCompatiblePdf(ByVal sFilename As String) As MemoryStream

        Dim reader As New iTextSharp.text.pdf.PdfReader(sFilename)
        Dim output_stream As New MemoryStream

        ' we retrieve the total number of pages
        Dim n As Integer = reader.NumberOfPages
        ' step 1: creation of a document-object
        Dim document As New iTextSharp.text.Document(reader.GetPageSizeWithRotation(1))
        ' step 2: we create a writer that listens to the document
        Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, output_stream)
        'write pdf that pdfsharp can understand
        writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4)
        ' step 3: we open the document
        document.Open()
        Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
        Dim page As iTextSharp.text.pdf.PdfImportedPage

        Dim rotation As Integer

        Dim i As Integer = 0
        While i < n
            i += 1
            document.SetPageSize(reader.GetPageSizeWithRotation(i))
            document.NewPage()
            page = writer.GetImportedPage(reader, i)
            rotation = reader.GetPageRotation(i)
            If rotation = 90 OrElse rotation = 270 Then
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
                reader.GetPageSizeWithRotation(i).Height)
            Else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
                0)
            End If
        End While

        '---- Keep the stream open!
        writer.CloseStream = False

        ' step 5: we close the document
        document.Close()

        Return output_stream

    End Function
End Module

Author:  rasepretrep [ Wed May 05, 2010 7:43 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thanks atherton01 :wink:

I like the idea to use a memorystream.
Ill modify my code accordingly.

Author:  atherton01 [ Wed May 05, 2010 4:24 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Glad I could contribute - but your solution saved my bacon!

Author:  gismatters2 [ Sun Jul 18, 2010 7:48 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thanks so much to rasepretrep and atherton01 for posting their workarounds...
this more than adequately solves a problem that's been plaguing us with
increasing frequency over time. Cheers,

Chris

Author:  jliebman [ Thu Aug 19, 2010 1:27 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

I have been following these threads because I have customers who send in PDF Forms via email, which I must process. I have been successfully handling them until I started to get the IREF error message. I tried the solution of using ITextSharp, and that worked, except the posted code does not successfully handle Forms (which come out devoid of form content), only documents. I am now looking into further use of ITextSharp features that may aid me in handling "advanced" forms. If anyone has information about that, please post it. If I succeed, I will post my results.

TIA

Jory Liebman
Stair Systems Inc

Author:  bkos [ Mon Oct 18, 2010 4:29 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

I too have customers who are increasingly using PDF's in versions that cause the iref stream error. I have successfully implemented the iText solution within our development code but would rather not add that library to our production code for various reasons. So...will the next version of PDFSharp include the fix for this issue and when will the next version be available in at least beta?

Thanks!

Author:  minime [ Thu Jan 06, 2011 9:39 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Hi,
I see the code but is there someone who can tell me how to open the pdf?
I dont have Visual basic and want maual to open one file with the ïre"virus..


http://www.codeproject.com/KB/files/PDFDeSecurer.aspx
more code


thx
Greetings
minime
:lol:

Author:  atherton01 [ Wed Apr 06, 2011 10:43 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

tcartwright wrote:
This works, except I lost all of the form fields from the original pdf which is what I really wanted.

I am not familiar with either library, does anyone know how to modify the conversion code to convert the form fields as well?


I think I just found out the answer to this question. Form fields in iTextSharp are manipulated using the "PDFStamper" not the "PDFWriter".... consequently, using the following code to get a compatible PDF is a lot less code, and I now realize why rasepretrep originally called his solution "ugly". The following code could be considered to be a much more elegant work-around, and I just tested it with our system and it seems to work great!

I hope others can benefit from this, as I would have loved to have found such a post a few months ago.

- Matthew

Code:
    Public Function ReturnCompatiblePdfFromInputStream(ByVal FileUpload1 As HtmlInputFile) As MemoryStream
        Dim fileLen As Integer

        Dim myStream As System.IO.Stream

        ' Get the length of the file.
        fileLen = FileUpload1.PostedFile.ContentLength

        ' Create a byte array to hold the contents of the file.
        Dim Input(fileLen) As Byte

        ' Initialize the stream to read the uploaded file.
        myStream = FileUpload1.PostedFile.InputStream

        ' Read the file into the byte array.
        myStream.Read(Input, 0, fileLen)

        ' Reset to position 0 before we pass it
        myStream.Position = 0

        ' Create a reader to read the stream of the original PDF
        Dim reader As New iTextSharp.text.pdf.PdfReader(myStream)

        ' Create the output stream
        Dim output_stream As New MemoryStream

        ' Create a PDFStamper, which will grab the original contents of the file and flatten it
        Dim pdfStamper As iTextSharp.text.pdf.PdfStamper = New iTextSharp.text.pdf.PdfStamper(reader, output_stream)

        ' This allows us to flatten the annotation and the signature
        pdfStamper.FormFlattening = True

        ' We have to set the version of the PDF to be 1.4 so that PDFSharp can do what it needs to do
        pdfStamper.Writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4)

        ' Don't close the stream, as we'll need to access it once the pdfStamper closes
        pdfStamper.Writer.CloseStream = False

        ' Close the pdfStamper
        pdfStamper.Close()

        ' Reset the file stream to position 0 before we pass it
        output_stream.Position = 0

        Return output_stream

    End Function

Author:  nick_5454 [ Tue Apr 12, 2011 5:44 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

I took y'alls code and needed it for a Manager pattern. Here's the code I use to test and convert.

I do this

Passing a memory stream of the filee.
Try and open with PDFSharp
Try and open with iTextSharp

Thanks.

PDFController is just a controller pattern. Nothing but a class.

Calling code
Code:
           FileStream stream = File.OpenRead(txtSource.Text);

            byte[] fileArray = new byte[stream.Length];
            stream.Read(fileArray, 0, fileArray.Length);
            MemoryStream memStream = new MemoryStream(fileArray);

            stream.Close();
            stream.Dispose();

            string filename = pdfController.CreateTiffFromPdf(memStream, txtDestFolder.Text, "3334-34");



The actual method

Code:
        public string CreateTiffFromPdf(MemoryStream sourceStream, string destinationFolder, string frNumber)
        {
            MemoryStream outputStream = null;
            PdfDocument pdfDoc = null;
            sourceStream.Position = 0;

            try
            {
                pdfDoc = PdfReader.Open(sourceStream);
            }
            catch (Exception ex1)
            {
                sourceStream.Position = 0;
                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();

                pdfDoc = PdfReader.Open(outputStream);
                //pdfDoc.Save(destinationFolder + "\\testfile.pdf");

                //pdfDoc = PdfReader.Open(destinationFolder + "\\testfile.pdf");
            }

            return CreateTiffFromPdf(pdfDoc, destinationFolder, frNumber);
        }


I forgot to say. Hope this helps!
Nick

Author:  atherton01 [ Tue Apr 12, 2011 6:23 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

By the way, I ran into a problem with iTextSharp, having to do with some security settings. Just be sure to use the 4.03 build instead of the newest build to work around the issue. Otherwise, if a PDF is password-protected, you can't use the pdfStamper to create a PDF that is compatible with PDFSharp. Just remember: "Four Point Three" = "Password Free"

Author:  GratefulZugore [ Fri Apr 15, 2011 8:04 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Folks,

Thanks for the workaround I am excited to finally have this. I am looking for the earlier version of iTextSharp (4.03) All I can find to download are later versions. My earliest version is 4.1.6 and of couorse I have the later ones already.

Any suggestions?

Author:  atherton01 [ Sat Apr 16, 2011 3:13 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Okay, the file size is too big to upload directly as an attachment to this post.

Here's a link:

http://www.forcefeedback.tv/itextsharp-4.0.3-dll.zip

Let me know once you've downloaded it and I'll delete it from my site.

Matthew

Author:  vidamj [ Fri Aug 12, 2011 2:14 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Guess I'll just bring this thread back from the dead. I've been searching around and have seen posts from 2009 stating this issue was being worked on. I believe I have the lastest version of the PDFSharp toolkit. I was just wondering if this has been or will be fixed soon or will I have to attempt one of the workarounds listed above?

At this time I do not have access to Acrobat to convert the document back to 5.0.

Also what is causing the real issue it must be a setting on the particular PDF. I reviewed files that I can process and the PDF Version of the file says 1.5 Acrobat 6.x. How are those working but others not?

Author:  vidamj [ Wed Aug 17, 2011 1:31 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Just wanted to post one more time to determine what the exact cause of this issue is as most documents work and a few don't?

Author:  Thomas Hoevel [ Mon Aug 22, 2011 8:42 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Not my area of expertise but AFAIK iref streams allow a much better compression by applying compression at a higher level. Not just one object is compressed, several objects are compressed together. Requires a different handling.

You can have PDF files marked for 10.0 that do not contain iref streams - they will work with PDFsharp.

Author:  vidamj [ Mon Aug 22, 2011 1:21 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thomas Hoevel wrote:
Not my area of expertise but AFAIK iref streams allow a much better compression by applying compression at a higher level. Not just one object is compressed, several objects are compressed together. Requires a different handling.

You can have PDF files marked for 10.0 that do not contain iref streams - they will work with PDFsharp.



I noted above this issue has been on going. Do any of the PDFSharp releases solve this problem?

Author:  hgirma [ Wed Feb 29, 2012 9:46 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Hello,

Just wondering if this has been resolved by the latest release?

Author:  Thomas Hoevel [ Thu Mar 01, 2012 8:24 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

hgirma wrote:
Just wondering if this has been resolved by the latest release?
No, not yet. Will not come with 1.3x (currently planned for 1.40).

We're busy with projects we get paid for - we cannot live from the donations we get for PDFsharp.

Author:  kforslund [ Tue Mar 13, 2012 3:02 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

"No, not yet. Will not come with 1.3x (currently planned for 1.40).

We're busy with projects we get paid for - we cannot live from the donations we get for PDFsharp."

I can appreciate that. But this problem is 3 years old. Acrobat 10 is out now. There hasn't been an update to PDFsharp for that long.

Here's the problem I have:
It's illegal to automate Adobe DLLs on a server
iTextSharp costs money for a business to use
GhostScript costs money for a business to use

PDFSharp is free for business, works great for older format PDFs, but hasn't been updated in 3 years.

This API is the last refuge of people needing to get work done for business and not get raked over the coals for licensing fees.

How can we help?

Do you need more hands working on the code (I imagine a steep enough learning curve to work with Acrobat's file structure)?
Money to justify time on the project?


If something doesn't change, PDFSharp is going to become a dead API.

Author:  rasepretrep [ Mon Mar 19, 2012 9:31 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

>iTextSharp costs money for a business to use

Actually iTextSharp is LGPL up to v 4.1.6 which also supports iref streams. :D
So the workaround posted above is also save for business use if you do not use v 5

Author:  greenfire27 [ Tue May 29, 2012 8:08 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

The following is the latest evolution of the work around. I've included the entire file. Just cut and past the whole thing into a file in your project and include the iTextSharp DLL (4.1.6, a.k.a. the free version). Then just replace all your calls to PdfReader.Open with CompatiblePdfReader.Open. Simple.

You can get version 4.1.6 of iTextSharp from here. I got that link from StackOverflow and it's worked for me.

If the code looks a little long it's just because the open function is overloaded a few times. You can pass in a string with the pdf path, a memory stream with the pdf in it, or even a byte array for those of us getting the pdf out of a database.

Here's the code in C#:

Code:
using System;
using System.IO;

namespace PdfSharp.Pdf.IO
{
   static public class CompatiblePdfReader
   {
      /// <summary>
      /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
      /// </summary>
      static public PdfDocument Open(string PdfPath)
      {
         using (FileStream fileStream = new FileStream(PdfPath, FileMode.Open, FileAccess.Read))
         {
            int len = (int)fileStream.Length;
            Byte[] fileArray = new Byte[len];
            fileStream.Read(fileArray, 0, len);
            fileStream.Close();

            return Open(fileArray);
         }
      }
      
      /// <summary>
      /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
      /// </summary>
      static public PdfDocument Open(byte[] fileArray)
      {
         return Open(new MemoryStream(fileArray));
      }

      /// <summary>
      /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
      /// </summary>
      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 hope this helps future generations. You should be able to put this patch in place in just a few minutes. Good luck!

Author:  dunwan [ Mon Jul 09, 2012 2:31 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

@greenfire27 that is a GENIUS solution. :shock:

I made two changes based on your code;

1. To get rid of the "Missing XML comment for publicly visible type or member" error by copying the XML comment within the class to the space above it
2. Modifying the Open method to include the document open mode

As a reminder, "You can get version 4.1.6 of iTextSharp from http://pdfviewernet.googlecode.com/svn-history/r55/trunk/PDFView/PDFView/lib/itextsharp.dll".

The final code for the "CompatiblePdfReader" class is below:

Code:
using System;
using System.IO;

namespace PdfSharp.Pdf.IO
{
    /// <summary>
    /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
    /// </summary>

    static public class CompatiblePdfReader
    {
        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(string PdfPath, PdfDocumentOpenMode openmode)
        {
            using (FileStream fileStream = new FileStream(PdfPath, FileMode.Open, FileAccess.Read))
            {
                int len = (int)fileStream.Length;
                Byte[] fileArray = new Byte[len];
                fileStream.Read(fileArray, 0, len);
                fileStream.Close();

                return Open(fileArray, openmode);
            }
        }

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(byte[] fileArray, PdfDocumentOpenMode openmode)
        {
            return Open(new MemoryStream(fileArray), openmode);
        }

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        static public PdfDocument Open(MemoryStream sourceStream, PdfDocumentOpenMode openmode)
        {
            PdfDocument outDoc = null;
            sourceStream.Position = 0;

            try
            {
                outDoc = PdfReader.Open(sourceStream, openmode);
            }
            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, openmode);
            }

            return outDoc;
        }
    }
}

Author:  ClermontSoftware [ Tue Jul 10, 2012 11:23 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Thanks for the workaround. My project compiles fine. However, when I try to use iTextSharp, I get the exception below. I have the iTextSharp dll in the same folder as my dll. Not sure if it is relevant, but the dll I am building is 32bit so I can run it from Microsoft Access, and the dll works fine on pdf's that do not use iref streams. I even tried merging the itextsharp dll using ILMerge, and I get the same error.

System.IO.FileNotFoundException: Could not load file or assembly 'itextsharp, Version=4.1.6.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. The system cannot find the file specified.
File name: 'itextsharp, Version=4.1.6.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca'
at PdfSharp.Pdf.IO.CompatiblePdfReader.Open(MemoryStream sourceStream, PdfDocumentOpenMode openmode)
at PdfSharp.Pdf.IO.CompatiblePdfReader.Open(String PdfPath, PdfDocumentOpenMode openmode)
at PdfSharp32.PdfSharp32.pdfDocumentMerge(String strMergePdf1, String strMergePdf2, String strMergeTo, String& strException)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Author:  ClermontSoftware [ Tue Jul 10, 2012 11:46 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Followup: I turned logging on, and got the information below. So, how do I get it to look in the local folder for itextsharp.dll? I don't want a fixed location, since different users may put it in different places. My output dll is unmanaged so I can call it from Microsoft Access.

LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: itextsharp, Version=4.1.6.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Office14/itextsharp.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Office14/itextsharp/itextsharp.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Office14/itextsharp.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Office/Office14/itextsharp/itextsharp.EXE.

Author:  ClermontSoftware [ Wed Jul 11, 2012 9:25 am ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

I got it figured out. I found the iTextSharp source at https://github.com/itextsharper/iTextSharp-4.1.6 and skipped creating a separate dll.

Author:  Benbo [ Tue Nov 13, 2012 7:21 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Can anyone supply a sample PDF containing the IREF structure for test purposes or suggest how to create one?

Thanks

Author:  ndrewh [ Sun Dec 16, 2012 11:57 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Hi,

I tried to use this to render PDF on windows phone. However I got an error: "Rebuild failed: trailer not found.; Original message: xref subsection not found at file". I used iTextSharp from here

Anybody have any ideas why? Thanks.

Author:  ghendric [ Thu Feb 07, 2013 6:06 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

I tried this with the latest version of iTextSharp (v5.3.5) and it worked perfectly. Thanks!

rasepretrep wrote:
First of all, let me say that i really love the pdfsharp library. I use it extensively in my current project, which involves merging pdf files and
adding page counters etc. to it.

Unfortunately I have no control over the pdf files that are to be merged, so I sometimes got the "iref stream" error message. At the same time I really want to stick with pdfsharp because it is imho a pdf api with a very nice and clear api.

So I developed an (ugly) workaround which I post here to give back something to this community.
the idea is simple: If pdfsharp fails to import a pdf file, use Itextsharp to open the document and write a compatible pdf, which can then be imported again with pdfsharp.
Here is the code :

Code:
 static public class CompatiblePdfReader
    {

        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf, called instead of PdfReader.open
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static public PdfDocument Open(string sFilename)
        {

            PdfDocument reader = new PdfDocument();
           
            try {
                reader = PdfReader.Open(sFilename, PdfDocumentOpenMode.Import);
            }
            catch (PdfSharp.Pdf.IO.PdfReaderException) {
                //workaround if pdfsharp doesnt dupport this pdf
                string newName = WriteCompatiblePdf(sFilename);
                reader = PdfReader.Open(newName, PdfDocumentOpenMode.Import);
            }

            return reader;
        }


        /// <summary>
        /// uses itextsharp 4.1.6 to convert any pdf to 1.4 compatible pdf
        /// </summary>
        /// <param name="sFilename"></param>
        /// <returns></returns>
        static private string WriteCompatiblePdf(string sFilename)
        {
            string sNewPdf = Globals.TempPath + Guid.NewGuid().ToString() + ".pdf";

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(sFilename);

            // we retrieve the total number of pages
            int n = reader.NumberOfPages;
            // step 1: creation of a document-object
            iTextSharp.text.Document document = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
            // step 2: we create a writer that listens to the document
            iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(sNewPdf, FileMode.Create));
            //write pdf that pdfsharp can understand
            writer.SetPdfVersion(iTextSharp.text.pdf.PdfWriter.PDF_VERSION_1_4);
            // step 3: we open the document
            document.Open();
            iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
            iTextSharp.text.pdf.PdfImportedPage page;

            int rotation;

            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }
            }
            // step 5: we close the document
            document.Close();
            return sNewPdf;
        }
    }


You will have to modify the code a bit to make it work (this is jsut a "copy and patse" from my current project).
I chose itextsharp 4.1.6 because version 5 had problems with some pdfs in my "testsuite".

Author:  martyndev [ Fri Dec 20, 2013 2:12 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

Just used the latest work-arround posted by rasepretrep and it worked fine in Vs 2010 Solution under .Net Framework 4.0. Thanks! :mrgreen:

Author:  miroslavmandl [ Wed Jan 29, 2014 4:50 pm ]
Post subject:  Re: PdfReaderException - "Cannot handle iref streams."

The workaround is great, but based on http://itextpdf.com/pricing, you need to purchase iText in order to use it in your commercial project.
Therefore I'd appreciate an estimate date when the PdfSharp authors plan to implement it.

Author:  James [ Fri Jun 27, 2014 10:39 am ]
Post subject:  Cannot handle iref streams.

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

When will this be handled?

Author:  Thomas Hoevel [ Mon Jun 30, 2014 9:02 am ]
Post subject:  Re: Cannot handle iref streams.

James wrote:
When will this be handled?
I cannot currently give you any ETA.

Page 1 of 2 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/