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.
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:
- download and include the iTextSharp 4.1.6 library
- 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;
}
}
}
- Change all your calls to
PdfReader.Open
toCompatiblePdfReader.Open
.
It works like a charm for me, hope this helps you.
PDFsharp 1.32 and earlier did not support iref streams.
Since December 2015 we have PDFsharp 1.50 with support for iref streams.
PdfReader.Open()
hangs. It is not fixed and still present in version 1.50.4000-beta3b, which is the last one currently. See Bug: PdfReader.Open() (PDFsharp 1.5) thread. –
Pertinacious Although a late reply but might be useful.
I am on a same situation (C# Project using pdfSharp). I've a PowerShell script, which ignores the files with iref stream while merging (Thus not throwing the 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 above function later.
The work around is to catch the PdfSharp.Pdf.IO.PdfReaderException
, and ignore the files causing such exceptions.
PdfDocument inputPDFDocument = new PdfDocument();
try
{
inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
}
catch (PdfSharp.Pdf.IO.PdfReaderException)
{
//
}
© 2022 - 2024 — McMap. All rights reserved.