Пример #1
0
        public static int get_pages_count(Guid applicationId, DocFileInfo pdf, string password, ref bool invalidPassword)
        {
            try
            {
                PdfReader reader = PDFUtil.open_pdf_file(applicationId, pdf, password, ref invalidPassword);

                return(reader == null ? 0 : reader.NumberOfPages);
            }
            catch { return(0); }
        }
Пример #2
0
        public static byte[] get_pdf_content(Guid applicationId, byte[] fileBytes, string password, ref bool invalidPassword)
        {
            PdfReader reader = PDFUtil.open_pdf_file(applicationId, fileBytes, password, ref invalidPassword);

            if (reader != null)
            {
                return(PDFUtil.to_byte_array(reader));
            }
            else
            {
                return(new byte[0]);
            }
        }
Пример #3
0
        public static void pdf2image(Guid applicationId,
                                     DocFileInfo pdf, string password, DocFileInfo destFile, ImageFormat imageFormat, bool repair)
        {
            //MagickNET.SetGhostscriptDirectory("[GhostScript DLL Dir]");
            //MagickNET.SetTempDirectory("[a temp dir]");

            if (!pdf.FileID.HasValue)
            {
                return;
            }
            else if (PDF2ImageProcessing.ContainsKey(pdf.FileID.Value) &&
                     (!repair || PDF2ImageProcessing[pdf.FileID.Value]))
            {
                return;
            }
            else
            {
                PDF2ImageProcessing[pdf.FileID.Value] = true;
            }

            if (destFile.file_exists_in_folder(applicationId) && !repair)
            {
                PDF2ImageProcessing[pdf.FileID.Value] = false;
                return;
            }

            try
            {
                string cacheDir = PublicMethods.map_path(PublicConsts.MagickCacheDirectory, localPath: true);
                if (!Directory.Exists(cacheDir))
                {
                    Directory.CreateDirectory(cacheDir);
                }
                MagickAnyCPU.CacheDirectory = cacheDir;
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "SetMagickCacheDirectory", ex, ModuleIdentifier.DCT);
            }

            try
            {
                string tempDir = PublicMethods.map_path(PublicConsts.TempDirectory, localPath: true);
                if (!Directory.Exists(tempDir))
                {
                    Directory.CreateDirectory(tempDir);
                }
                MagickNET.SetTempDirectory(tempDir);

                if (!string.IsNullOrEmpty(RaaiVanSettings.GhostScriptDirectory))
                {
                    MagickNET.SetGhostscriptDirectory(RaaiVanSettings.GhostScriptDirectory);
                }
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "SetMagickTempDirectory", ex, ModuleIdentifier.DCT);
            }

            try
            {
                using (MagickImageCollection pages = new MagickImageCollection())
                {
                    MagickReadSettings settings = new MagickReadSettings()
                    {
                        Density = new Density(100, 100)
                    };

                    bool invalidPassword = false;

                    using (PdfReader reader = PDFUtil.open_pdf_file(applicationId, pdf, password, ref invalidPassword))
                    {
                        byte[] pdfContent = PDFUtil.to_byte_array(reader);
                        pages.Read(pdfContent, settings);
                    }

                    int  pageNum    = 0;
                    bool errorLoged = false;

                    foreach (MagickImage p in pages)
                    {
                        ++pageNum;

                        destFile.FileName  = pageNum.ToString();
                        destFile.Extension = imageFormat.ToString().ToLower();

                        if (destFile.exists(applicationId))
                        {
                            continue;
                        }

                        try
                        {
                            using (MemoryStream stream = new MemoryStream())
                            {
                                p.ToBitmap(imageFormat).Save(stream, imageFormat);
                                destFile.store(applicationId, stream.ToArray());
                            }
                        }
                        catch (Exception ex)
                        {
                            if (!errorLoged)
                            {
                                errorLoged = true;
                                LogController.save_error_log(applicationId, null,
                                                             "ConvertPDFPageToImage", ex, ModuleIdentifier.DCT);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogController.save_error_log(applicationId, null, "ConvertPDFToImage", ex, ModuleIdentifier.DCT);
            }

            PDF2ImageProcessing[pdf.FileID.Value] = false;
        }