pvginkel / PdfiumViewer

PDF viewer based on Google's PDFium.
Apache License 2.0
972 stars 418 forks source link

DLLNotFoundException on PdfDocument.Load breaks the whole process #132

Closed nikolayyordanov closed 7 years ago

nikolayyordanov commented 7 years ago

When the dlls are not in place the DLLNotFoundException breaks the current process. Testing this with Console app (hosting WCF). If I check if the dlls are in place and raise DllNotFoundException it works fine.

pvginkel commented 7 years ago

This is due to how P/Invoke in .NET works and is not something (I think) have control over.

What solution do you propose?

nikolayyordanov commented 7 years ago

OK, I have implemented some additional checks (checking the platform and if the file exists).

public static List<byte[]> PdfStreamToImageArr(MemoryStream ms)
        {
            var result = new List<byte[]>();
            try
            {              
                var pfpath = Path.GetDirectoryName(typeof(PdfDocument).Assembly.Location);
                string platformFolderName = "x86";
                if (Environment.Is64BitProcess)
                {
                    platformFolderName = "x64";
                }                
                pfpath = Path.Combine(pfpath, platformFolderName, "pdfium.dll");
                if (File.Exists(pfpath))
                {
                    using (PdfDocument doc = PdfDocument.Load(ms))
                    {
                        for (int i = 0; i < doc.PageCount; i++)
                        {
                            int height = (int)doc.PageSizes[i].Height;
                            int width = (int)doc.PageSizes[i].Width;
                            using (var ret = new MemoryStream())
                            {
                                using (var img = doc.Render(i, width, height, 300,300,  
                                    PdfRenderFlags.CorrectFromDpi 
                                    | PdfRenderFlags.Grayscale
                                    | PdfRenderFlags.LcdText
                                    | PdfRenderFlags.ForPrinting))
                                {
                                    //if (width > height)
                                    //{
                                    //    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                                    //}
                                    img.Save(ret, ImageFormat.Png);
                                    //img.Save(ret,)
                                    result.Add(ret.ToArray());
                                }
                            }
                        }
                        doc.Dispose();
                    }
                }
                else
                {
                    throw new DllNotFoundException();
                }
            }
            catch (DllNotFoundException ex)
            {
                throw new EX(MSG.ERR_MISSING_SERVER_DLL);
            }
            return result;
        }

Thanks.

pvginkel commented 7 years ago

No problem.