PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Oct 16, 2019 5:40 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Aug 30, 2018 4:00 pm 
Offline

Joined: Thu Aug 30, 2018 3:28 pm
Posts: 2
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!


Top
 Profile  
Reply with quote  
PostPosted: Fri Aug 31, 2018 3:32 pm 
Offline

Joined: Thu Aug 30, 2018 3:28 pm
Posts: 2
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 356 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group