'PDFsharp cannot handle this PDF feature introduced with Acrobat 6' error while opening PDF file

前端 未结 2 796
终归单人心
终归单人心 2020-12-15 20:42

I use PDFsharp (v1.32) for merging several PDF files. I open documents using this code:

PdfDocument inputDocument = PdfReader.Open(pdfFilePath, PdfDocumentOp         


        
相关标签:
2条回答
  • 2020-12-15 20:59

    Use PDFsharp 1.50 beta 3 from December 2015 or a newer version.

    https://www.nuget.org/packages/PdfSharp/1.50.4820-RC1
    https://www.nuget.org/packages/PDFsharp-gdi/1.50.4820-RC1
    https://www.nuget.org/packages/PDFsharp-wpf/1.50.4820-RC1

    https://github.com/empira/PDFsharp

    0 讨论(0)
  • 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!!!

    0 讨论(0)
提交回复
热议问题