Esempio n. 1
0
        public IEnumerable <ScannedImage> Import(string filePath, ImportParams importParams, Func <int, int, bool> progressCallback)
        {
            if (!progressCallback(0, 0))
            {
                return(Enumerable.Empty <ScannedImage>());
            }
            int  passwordAttempts = 0;
            bool aborted          = false;
            int  i = 0;

            try
            {
                PdfDocument document = PdfReader.Open(filePath, PdfDocumentOpenMode.Import, args =>
                {
                    if (!pdfPasswordProvider.ProvidePassword(Path.GetFileName(filePath), passwordAttempts++, out args.Password))
                    {
                        args.Abort = true;
                        aborted    = true;
                    }
                });
                if (passwordAttempts > 0 &&
                    !document.SecuritySettings.HasOwnerPermissions &&
                    !document.SecuritySettings.PermitExtractContent)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.PdfNoPermissionToExtractContent, Path.GetFileName(filePath)));
                    return(Enumerable.Empty <ScannedImage>());
                }
                if (document.Info.Creator != MiscResources.NAPS2 && document.Info.Author != MiscResources.NAPS2)
                {
                    pdfRenderer.ThrowIfCantRender();
                    return(importParams.Slice.Indices(document.PageCount)
                           .Select(index => document.Pages[index])
                           .TakeWhile(page => progressCallback(i++, document.PageCount))
                           .Select(page => ExportRawPdfPage(page, importParams)));
                }

                return(importParams.Slice.Indices(document.PageCount)
                       .Select(index => document.Pages[index])
                       .TakeWhile(page => progressCallback(i++, document.PageCount))
                       .SelectMany(page => GetImagesFromPage(page, importParams)));
            }
            catch (ImageRenderException e)
            {
                errorOutput.DisplayError(string.Format(MiscResources.ImportErrorNAPS2Pdf, Path.GetFileName(filePath)));
                Log.ErrorException("Error importing PDF file.", e);
                return(Enumerable.Empty <ScannedImage>());
            }
            catch (Exception e)
            {
                if (!aborted)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(filePath)));
                    Log.ErrorException("Error importing PDF file.", e);
                }
                return(Enumerable.Empty <ScannedImage>());
            }
        }
Esempio n. 2
0
        public IEnumerable <IScannedImage> Import(string filePath)
        {
            int  passwordAttempts = 0;
            bool aborted          = false;

            try
            {
                PdfDocument document = PdfReader.Open(filePath, PdfDocumentOpenMode.ReadOnly, args =>
                {
                    if (!pdfPasswordProvider.ProvidePassword(Path.GetFileName(filePath), passwordAttempts++, out args.Password))
                    {
                        args.Abort = true;
                        aborted    = true;
                    }
                });
                if (document.Info.Creator != MiscResources.NAPS2 && document.Info.Author != MiscResources.NAPS2)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.ImportErrorNAPS2Pdf, Path.GetFileName(filePath)));
                    return(Enumerable.Empty <IScannedImage>());
                }
                if (passwordAttempts > 0 &&
                    !document.SecuritySettings.HasOwnerPermissions &&
                    !document.SecuritySettings.PermitExtractContent)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.PdfNoPermissionToExtractContent, Path.GetFileName(filePath)));
                    return(Enumerable.Empty <IScannedImage>());
                }

                return(document.Pages.Cast <PdfPage>().SelectMany(GetImagesFromPage));
            }
            catch (NotImplementedException e)
            {
                errorOutput.DisplayError(string.Format(MiscResources.ImportErrorNAPS2Pdf, Path.GetFileName(filePath)));
                Log.ErrorException("Error importing PDF file.", e);
                return(Enumerable.Empty <IScannedImage>());
            }
            catch (Exception e)
            {
                if (!aborted)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(filePath)));
                    Log.ErrorException("Error importing PDF file.", e);
                }
                return(Enumerable.Empty <IScannedImage>());
            }
        }
Esempio n. 3
0
        public ScannedImageSource Import(string filePath, ImportParams importParams, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(async() =>
            {
                if (cancelToken.IsCancellationRequested)
                {
                    source.Done();
                }

                int passwordAttempts = 0;
                bool aborted         = false;
                int i = 0;
                try
                {
                    PdfDocument document = PdfReader.Open(filePath, PdfDocumentOpenMode.Import, args =>
                    {
                        if (!pdfPasswordProvider.ProvidePassword(Path.GetFileName(filePath), passwordAttempts++, out args.Password))
                        {
                            args.Abort = true;
                            aborted    = true;
                        }
                    });
                    if (passwordAttempts > 0 &&
                        !document.SecuritySettings.HasOwnerPermissions &&
                        !document.SecuritySettings.PermitExtractContent)
                    {
                        errorOutput.DisplayError(string.Format(MiscResources.PdfNoPermissionToExtractContent, Path.GetFileName(filePath)));
                        source.Done();
                    }

                    var pages = importParams.Slice.Indices(document.PageCount)
                                .Select(index => document.Pages[index])
                                .TakeWhile(page =>
                    {
                        progressCallback(i++, document.PageCount);
                        return(!cancelToken.IsCancellationRequested);
                    });
                    if (document.Info.Creator != MiscResources.NAPS2 && document.Info.Author != MiscResources.NAPS2)
                    {
                        pdfRenderer.ThrowIfCantRender();
                        foreach (var page in pages)
                        {
                            source.Put(await ExportRawPdfPage(page, importParams));
                        }
                    }
                    else
                    {
                        foreach (var page in pages)
                        {
                            await GetImagesFromPage(page, importParams, source);
                        }
                    }
                }
                catch (ImageRenderException e)
                {
                    errorOutput.DisplayError(string.Format(MiscResources.ImportErrorNAPS2Pdf, Path.GetFileName(filePath)));
                    Log.ErrorException("Error importing PDF file.", e);
                }
                catch (Exception e)
                {
                    if (!aborted)
                    {
                        errorOutput.DisplayError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(filePath)));
                        Log.ErrorException("Error importing PDF file.", e);
                    }
                }
                finally
                {
                    source.Done();
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }