Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PDF file contain iref stream?

Tags:

c#

pdf

pdfsharp

I still fight with read data from PDF file.
I use PDFsharp, how can I check if file contains iref stream without use method Open. Method Open throws exception if file contains iref stream.

like image 221
Jacek Avatar asked Oct 08 '12 13:10

Jacek


2 Answers

There is a know workaround to permit you to open ALSO the pdf files that contains iref: you can find here the complete thread about that.

Just to summarize the solution:

  1. download and include the iTextSharp 4.1.6 library
  2. paste the following code in a code file into your project:

-

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 (var fileStream = new FileStream(pdfPath, FileMode.Open, FileAccess.Read))
            {
                var len = (int)fileStream.Length;
                var 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;
            sourceStream.Position = 0;

            try
            {
                outDoc = PdfReader.Open(sourceStream, PdfDocumentOpenMode.Import);
            }
            catch (PdfReaderException)
            {
                //workaround if pdfsharp doesn't support this pdf
                sourceStream.Position = 0;
                var outputStream = new MemoryStream();
                var reader = new iTextSharp.text.pdf.PdfReader(sourceStream);
                var pdfStamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream) {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;
        }
    }
}
  1. Change all your calls to PdfReader.Open to CompatiblePdfReader.Open.

It works like a charm for me, hope this helps you.

like image 194
Tobia Zambon Avatar answered Oct 13 '22 00:10

Tobia Zambon


PDFsharp 1.32 and earlier did not support iref streams.

Since December 2015 we have PDFsharp 1.50 with support for iref streams.

like image 27
I liked the old Stack Overflow Avatar answered Oct 12 '22 22:10

I liked the old Stack Overflow