public Pdf2Image(string pdfFileName, Pdf2ImageSettings settings) : this(pdfFileName) { if (settings == null) throw new ArgumentNullException("setting"); this.Settings = settings; }
public Pdf2Image(string pdfFileName, Pdf2ImageSettings settings) : this(pdfFileName) { if (settings == null) { throw new ArgumentNullException("setting"); } this.Settings = settings; }
private void Button_Click_1(object sender, RoutedEventArgs e) { //txtScanResult.Text = ReadImage(txtFilePath.Text); Pdf2ImageSettings settings; settings = new Pdf2ImageSettings(); settings.AntiAliasMode = AntiAliasMode.High; settings.Dpi = 300; settings.GridFitMode = GridFitMode.Topological; settings.ImageFormat = ImageFormat.Png24; settings.TrimMode = PdfTrimMode.CropBox; Bitmap firstPage = new Pdf2Image(txtFilePath.Text).GetImage(1); string outputfilePath = System.IO.Path.Combine(@"D:\temp\", System.IO.Path.GetFileNameWithoutExtension(txtFilePath.Text)); firstPage.Save(outputfilePath, System.Drawing.Imaging.ImageFormat.Jpeg); }
private string GetPage(string projectName, string pdfPath, int pageNumber, out int pageCount) { byte[] imageBytes = null; string response = null; string prefix = "data:image/png;base64,"; Pdf2ImageSettings settings; settings = new Pdf2ImageSettings(); settings.AntiAliasMode = AntiAliasMode.High; settings.Dpi = 300; settings.GridFitMode = GridFitMode.Topological; settings.ImageFormat = ImageFormat.Png24; settings.TrimMode = PdfTrimMode.CropBox; if (File.Exists(pdfPath)) { var converter = new Pdf2Image(pdfPath); Bitmap pageImage = converter.GetImage(pageNumber); pageCount = converter.PageCount; var jpegFilePath = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/pages"), projectName + pageNumber + ".jpg"); pageImage.Save(jpegFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); ImageConverter imgConverter = new ImageConverter(); imageBytes = (byte[])imgConverter.ConvertTo(pageImage, typeof(byte[])); response = Convert.ToBase64String(imageBytes); return prefix + response; } // if the file path is invalid set the page count to zero pageCount = 0; return ""; }
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); }
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; }