Does the pdf file support iref stream?

I am still struggling with reading data from a PDF file.
I use PDFsharp how to check if a file contains an iref stream without using the Open method. The Open throws exception method if the file contains an iref stream .

+7
source share
4 answers

There is a well-known workaround allowing you to open ALSO pdf files containing iiref: you can find the full stream about it here.

Just to summarize the solution:

-

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 it helps you.

+20
source

PDFsharp 1.32 and earlier did not support IRE streams.

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

+10
source

Although a late answer, but may be useful.

I am in one situation (C # project using pdfSharp). I have a PowerShell script that ignores files with iref stream when merging (therefore it does not throw an exception).

 Function Merge-PDF { Param($path, $filename) $output = New-Object PdfSharp.Pdf.PdfDocument $PdfReader = [PdfSharp.Pdf.IO.PdfReader] $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode] foreach($i in (gci $path *.pdf -Recurse)) { $input = New-Object PdfSharp.Pdf.PdfDocument $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import) $input.Pages | %{$output.AddPage($_)} } $output.Save($filename) } Merge-PDF -path c:\reports -filename c:\reports\zzFull_deck.pdf 

Will definitely post the C # equivalent of the above function later.

0
source

The work around is to catch PdfSharp.Pdf.IO.PdfReaderException and ignore files that cause such exceptions.

 PdfDocument inputPDFDocument = new PdfDocument(); try { inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import); } catch (PdfSharp.Pdf.IO.PdfReaderException) { // } 
0
source

All Articles