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