Exemple #1
0
        public Pdf2Image(string pdfFileName, Pdf2ImageSettings settings)
            : this(pdfFileName)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("setting");
            }

            this.Settings = settings;
        }
Exemple #2
0
 //Конструктор формы
 public MainForm()
 {
     InitializeComponent();
     //Настройки рендеринга pdf
     _settings = new Pdf2ImageSettings
     {
         AntiAliasMode = AntiAliasMode.High,
         Dpi           = 96,
         GridFitMode   = GridFitMode.Topological,
         ImageFormat   = ImageFormat.Png24
     };
     //Инициализация
     _zoom                  = 1f;
     _canRender             = true;
     _mouseDown             = false;
     _resize                = false;
     _currentPage           = 0;
     tscbZoom.SelectedIndex = 1;
     _list                  = new List <ImageProp>();
 }
Exemple #3
0
        protected virtual IDictionary <GhostScriptCommand, object> GetConversionArguments(string pdfFileName, string outputImageFileName, int pageNumber, string password, Pdf2ImageSettings settings)
        {
            Dictionary <GhostScriptCommand, object> arguments;

            arguments = new Dictionary <GhostScriptCommand, object>();

            // basic GhostScript setup
            arguments.Add(GhostScriptCommand.Silent, null);
            arguments.Add(GhostScriptCommand.Safer, null);
            arguments.Add(GhostScriptCommand.Batch, null);
            arguments.Add(GhostScriptCommand.NoPause, null);

            // specify the output
            arguments.Add(GhostScriptCommand.Device, GhostScriptAPI.GetDeviceName(settings.ImageFormat));
            arguments.Add(GhostScriptCommand.OutputFile, outputImageFileName);

            // page numbers
            arguments.Add(GhostScriptCommand.FirstPage, pageNumber);
            arguments.Add(GhostScriptCommand.LastPage, pageNumber);

            // graphics options
            arguments.Add(GhostScriptCommand.UseCIEColor, null);

            if (settings.AntiAliasMode != AntiAliasMode.None)
            {
                arguments.Add(GhostScriptCommand.TextAlphaBits, settings.AntiAliasMode);
                arguments.Add(GhostScriptCommand.GraphicsAlphaBits, settings.AntiAliasMode);
            }

            arguments.Add(GhostScriptCommand.GridToFitTT, settings.GridFitMode);

            // image size
            if (settings.TrimMode != PdfTrimMode.PaperSize)
            {
                arguments.Add(GhostScriptCommand.Resolution, settings.Dpi.ToString());
            }

            switch (settings.TrimMode)
            {
            case PdfTrimMode.PaperSize:
                if (settings.PaperSize != PaperSize.Default)
                {
                    arguments.Add(GhostScriptCommand.FixedMedia, true);
                    arguments.Add(GhostScriptCommand.PaperSize, settings.PaperSize);
                }
                break;

            case PdfTrimMode.TrimBox:
                arguments.Add(GhostScriptCommand.UseTrimBox, true);
                break;

            case PdfTrimMode.CropBox:
                arguments.Add(GhostScriptCommand.UseCropBox, true);
                break;
            }

            // pdf password
            if (!string.IsNullOrEmpty(password))
            {
                arguments.Add(GhostScriptCommand.PDFPassword, password);
            }

            // pdf filename
            arguments.Add(GhostScriptCommand.InputFile, pdfFileName);

            return(arguments);
        }
Exemple #4
0
        private bool ConvertDocumentPdf(int id)
        {
            TaiLieu document = db.TaiLieux.Where(x => x.MaTaiLieu == id).FirstOrDefault();

            if (document != null)
            {
                string strPdfPath = Path.Combine(Server.MapPath("~/Upload/TaiLieuGoc/"), document.fileGOC);
                if (System.IO.File.Exists(strPdfPath) && strPdfPath.ToLower().EndsWith(".pdf"))
                {
                    Pdf2ImageSettings convertSetting = new Pdf2ImageSettings();
                    convertSetting.Dpi = 150;

                    Pdf2Image pdf = new Pdf2Image(strPdfPath, convertSetting);
                    if (pdf.PageCount > 0)
                    {
                        List <PdfPage> lstPage = new List <PdfPage>();

                        for (int j = 1; j <= pdf.PageCount; j++)
                        {
                            try
                            {
                                System.Drawing.Bitmap bitmap = pdf.GetImage(j);
                                //MemoryStream ms = new MemoryStream();
                                //bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                                string strFileName = id + "-" + j + ".png";
                                string strFilePath = Path.Combine(Server.MapPath("~/Upload/Image/"), strFileName);
                                if (System.IO.File.Exists(strFilePath))
                                {
                                    System.IO.File.Delete(strFilePath);
                                }
                                bitmap.Save(strFilePath, System.Drawing.Imaging.ImageFormat.Png);

                                PdfPage page = new PdfPage();
                                page.DocumentId = id;
                                page.Page       = j;
                                page.ImageUrl   = "/Upload/Image/" + strFileName;
                                lstPage.Add(page);
                            }
                            catch (IndexOutOfRangeException)
                            {
                                break;
                            }
                            catch (Exception ex)
                            {
                                using (StreamWriter writer = new StreamWriter(Path.Combine(Server.MapPath("~/Upload/Image/"), "Log.txt"), true, System.Text.UTF8Encoding.Unicode))
                                {
                                    writer.WriteLine(ex.ToString());
                                }
                                break;
                            }
                        }

                        if (lstPage.Count > 0)
                        {
                            using (TransactionScope scope = new TransactionScope())
                            {
                                var lstOld = db.PdfPages.Where(x => x.DocumentId == id).ToList();
                                if (lstOld != null)
                                {
                                    foreach (var item in lstOld)
                                    {
                                        db.PdfPages.Remove(item);
                                    }
                                }

                                db.PdfPages.AddRange(lstPage);
                                db.SaveChanges();

                                scope.Complete();
                            }
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }