PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Invalid Predictor in Array
http://forum.pdfsharp.com/viewtopic.php?f=2&t=3840
Page 1 of 1

Author:  chiprickers [ Thu Aug 30, 2018 4:00 pm ]
Post subject:  Invalid Predictor in Array

Hi, folks.

I was hoping someone could point me in the right direction. I apologize if I'm asking anything that's ignorant. I'm very new to C#.

I'm using the nuget package PDFsharp-gdi, v.150.4845-RC2a.

I'm using PDF Sharp and ExcelDna to merge a number of pre-existing pdfs together. And I'm getting the following exception on some (not all) of the PDFs that are being merged:

-----
System.ArgumentException: Invalid predictor in array.
at PdfSharp.Pdf.IO.Parser.DecodeCrossReferenceStream(Byte[] bytes, Int32 columns, Int32 predictor)
at PdfSharp.Pdf.IO.Parser.ReadXRefStream(PdfCrossReferenceTable xrefTable)
at PdfSharp.Pdf.IO.Parser.ReadXRefTableAndTrailer(PdfCrossReferenceTable xrefTable)
at PdfSharp.Pdf.IO.Parser.ReadTrailer()
at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
at PdfSharp.Pdf.IO.PdfReader.Open(String path, PdfDocumentOpenMode openmode)
at xlDnaTools.PdfSharpTools.MergeDocs(String outputFilename, Object inputFileArray, String dataPath)
-----

The C# code that's generating that is:

Code:
        public string[] MergeDocs(string filename, object fileArray, string dataPath)
        {
            string[] files = (string[])fileArray;

            List<string> errors = new List<string>();

            PdfDocument outputDocument = new PdfDocument();

            foreach (string file in files)
            {
                try
                {
                    PdfDocument inputDocument = PdfReader.Open(dataPath + file, PdfDocumentOpenMode.Import);

                    int count = inputDocument.PageCount;
                    for (int idx = 0; idx < count; idx++)
                    {
                        PdfPage page = inputDocument.Pages[idx];
                        outputDocument.AddPage(page);

                    }
                }
                catch(ArgumentException e)
                {
                    // Catching invalid predictor problem. I think it's a problem with fillable fields in pdfs.
                    errors.Add(string.Format("File '{0}' had an exception caught: {1}", file, e));
                }

            }

            outputDocument.Save(filename);

            return errors.ToArray();
        }



I think the problem lies with the PDFs themselves. I've re-saved/re-printed some of the PDFs that have had the problem, and that seems to fix things. However, there are ~160 PDFs, and that list can change and grow. So, I'd rather not just re-print all of them (or need to make sure each new one is re-saved/re-printed).

I found this here:

viewtopic.php?f=3&t=3713

That seems to suggest it's a signature problem. But, I don't think there are any signatures on some of the documents that are having trouble.

Can someone point me in the right direction?

Ideally, I'd like to change my code so that it can handle this exception without just giving up on the file. Less ideally, I'd at least like to track down what's wrong with these PDFs, so I can fix the PDFs that need them, instead of just re-creating all of them whenever there's a hint of an issue.

Thanks!

Author:  chiprickers [ Fri Aug 31, 2018 3:32 pm ]
Post subject:  Re: Invalid Predictor in Array

So, a followup.

I was able to open up the files I was having trouble with on a friend's machine who had Adobe Acrobat Pro on their computer, and did some of the "preflight" checks that are available to the software (which I think checks the internal structure of the file?). There were problems with the compatibility with some of the PDF standards.

I then did end up re-creating the problem files and all was well.

So, maybe there is a way to handle the problems with the PDFs through code (after all, they opened and displayed correctly in Adobe Acrobat and Adobe Reader), but my solution was to re-create the files that were generating errors.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/