/// <summary> /// Convert specified PDF file to Images (1 image per pdf page) /// </summary> /// <param name="file"></param> /// <returns></returns> IEnumerable <Image> ExtractImages(string file) { Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null; Ghostscript.NET.GhostscriptVersionInfo vesion; vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), PathToDll + @"\gsdll32.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { try { rasterizer.Open(file, vesion, false); return(GetImagesFromRasterizer(rasterizer)); } catch { vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), PathToDll + @"\gsdll64.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); rasterizer.Open(file, vesion, false); return(GetImagesFromRasterizer(rasterizer)); } } }
public static IList <byte[]> PDFToImage(MemoryStream ms, int ImgWidth, int ImgHeight) { string path = Path.GetDirectoryName(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath); Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null; Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), @"C:\\Program Files\\gs\\gs9.19\\bin\\gsdll64.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); IList <byte[]> images = new List <byte[]> (); using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { rasterizer.Open(ms, vesion, false); for (int i = 1; i <= rasterizer.PageCount; i++) { MemoryStream returnImage = new MemoryStream(); Image SrcImg = rasterizer.GetPage(200, 200, i); //if (SrcImg.Width <= ImgWidth) ImgWidth = SrcImg.Width; //int NewHeight = SrcImg.Height * ImgWidth / SrcImg.Width; //if (NewHeight > ImgHeight) //{ // // Resize with height instead // ImgWidth = SrcImg.Width * ImgHeight / SrcImg.Height; // NewHeight = ImgHeight; //} SrcImg.Save(returnImage, ImageFormat.Jpeg); images.Add(returnImage.ToArray()); } rasterizer.Close(); return(images); } }
public static Dictionary<int, string> PDFToImage(string file, string outputPath, int dpi) { string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null; Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new System.Version(0, 0, 0), path + @"\gsdll64.dll", string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); Dictionary<int, string> dictionary = new Dictionary<int, string>(); using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { rasterizer.Open(file, vesion, false); for (int i = 1; i <= rasterizer.PageCount; i++) { string pageFilePath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(@file).Replace(".pdf", "") + "-p" + i.ToString() + ".tiff"); using (System.Drawing.Image img = rasterizer.GetPage(dpi, dpi, i)) { if (File.Exists(pageFilePath)) { //img.Save(pageFilePath, System.Drawing.Imaging.ImageFormat.Tiff); dictionary.Add(i, pageFilePath); } else { img.Save(pageFilePath, System.Drawing.Imaging.ImageFormat.Tiff); dictionary.Add(i, pageFilePath); } } } rasterizer.Close(); rasterizer.Dispose(); } return dictionary; }
private void ConvertToJPEG(string inputFileName) { Ghostscript.NET.GhostscriptVersionInfo gv = Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion(Ghostscript.NET.GhostscriptLicense.GPL | Ghostscript.NET.GhostscriptLicense.AFPL, Ghostscript.NET.GhostscriptLicense.GPL); string outputPath = System.IO.Path.GetDirectoryName(inputFileName) + "\\" + System.IO.Path.GetFileNameWithoutExtension(inputFileName) + ".jpg"; System.IO.StreamReader r = new System.IO.StreamReader(inputFileName); string pdfText = r.ReadToEnd(); System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]"); System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText); //GhostscriptSharp.GhostscriptWrapper.GenerateOutput(singleFilePath, outputPath, gss); using (GhostscriptProcessor gsp = new GhostscriptProcessor(gv, true)) { gsp.Processing += new GhostscriptProcessorProcessingEventHandler(ghostscriptProcessing); List <string> switches = new List <string>(); switches.Add("-empty"); switches.Add("-dSAFER"); switches.Add("-dBATCH"); switches.Add("-dNOPAUSE"); switches.Add("-dNOPROMPT"); switches.Add(@"-sFONTPATH=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts)); switches.Add("-dFirstPage=1"); switches.Add("-dLastPage=" + matches.Count.ToString()); switches.Add("-sDEVICE=jpeg"); switches.Add("-r96"); switches.Add("-dTextAlphaBits=4"); switches.Add("-dGraphicsAlphaBits=4"); switches.Add(@"-sOutputFile=" + outputPath); switches.Add(@"-f"); switches.Add(inputFileName); gsp.StartProcessing(switches.ToArray(), null); } }
public override Stream CreateThumbnail(Stream stream, Size size, int pageNumber) { const string ghostDllPath = @"C:\Development\DRS\src\Web\bin\gsdll64.dll"; //todo: banish the hardcode var version = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), ghostDllPath, string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); using (var rasterizer = new GhostscriptRasterizer()) { var path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid() + ".pdf"); try { using (var fs = File.Create(path)) { //todo: why is ghostscript requiring i save the pdf to file first?? stream.CopyTo(fs); stream.Dispose(); } rasterizer.Open(path, version, true); using (var thumbnail = rasterizer.GetPage(200, 200, pageNumber)) { using (var thumbnailStream = new MemoryStream()) { thumbnail.Save(thumbnailStream, ImageFormat.Png); thumbnail.Dispose(); thumbnailStream.Position = 0; return(ResizeAndCrop(thumbnailStream, size.Width, size.Height)); } } } finally { File.Delete(path); } } }
static public List <string> PDFToImages(string file, string outputDirectory, int dpi, bool uniqueFileNames) { //string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); //path + @"\libs\gsdll32.dll" Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null; Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), HostingEnvironment.MapPath("~/Libs/gsdll32.dll"), string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); List <string> outputFiles = new List <string>(); using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { rasterizer.Open(file, vesion, false); for (int i = 1; i <= rasterizer.PageCount; i++) { string pageFilePath = Path.Combine(outputDirectory, (uniqueFileNames ? Path.GetRandomFileName() : "") + Path.GetFileNameWithoutExtension(file) + "-p" + i.ToString() + ".jpg"); outputFiles.Add(pageFilePath); Image img = rasterizer.GetPage(dpi, dpi, i); img.Save(pageFilePath, ImageFormat.Jpeg); } rasterizer.Close(); } return(outputFiles); }
public static List<string> PDFToImages(string file, string outputDirectory, int dpi, bool uniqueFileNames) { //string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); //path + @"\libs\gsdll32.dll" Ghostscript.NET.Rasterizer.GhostscriptRasterizer rasterizer = null; Ghostscript.NET.GhostscriptVersionInfo vesion = new Ghostscript.NET.GhostscriptVersionInfo(new Version(0, 0, 0), HostingEnvironment.MapPath("~/Libs/gsdll32.dll") , string.Empty, Ghostscript.NET.GhostscriptLicense.GPL); List<string> outputFiles = new List<string>(); using (rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer()) { rasterizer.Open(file, vesion, false); for (int i = 1; i <= rasterizer.PageCount; i++) { string pageFilePath = Path.Combine(outputDirectory, (uniqueFileNames ? Path.GetRandomFileName() : "") + Path.GetFileNameWithoutExtension(file) + "-p" + i.ToString() + ".jpg"); outputFiles.Add(pageFilePath); Image img = rasterizer.GetPage(dpi, dpi, i); img.Save(pageFilePath, ImageFormat.Jpeg); } rasterizer.Close(); } return outputFiles; }
private void btnCnvert_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(this.txtSingleFilePath.Text) && string.IsNullOrEmpty(this.txtMultFilePath.Text)) { MessageBox.Show("You need to enter a file path!", "Error: No File Path"); return; } string singleFilePath = this.txtSingleFilePath.Text, multFileDir = this.txtMultFilePath.Text; string pathsSelected = "Single File Path:\t\t" + singleFilePath + "\nMultiple File Directory:\t" + multFileDir; //GhostscriptSharp.GhostscriptSettings gss = new GhostscriptSharp.GhostscriptSettings(); //gss.Device = GhostscriptSharp.Settings.GhostscriptDevices.jpeg; //gss.Page.AllPages = true; //gss.Resolution = new Size(500, 500); //gss.Size = new GhostscriptSharp.Settings.GhostscriptPageSize() { Native = GhostscriptSharp.Settings.GhostscriptPageSizes.a4, Manual = new Size(500, 500) }; Ghostscript.NET.GhostscriptVersionInfo gv = Ghostscript.NET.GhostscriptVersionInfo.GetLastInstalledVersion(Ghostscript.NET.GhostscriptLicense.GPL | Ghostscript.NET.GhostscriptLicense.AFPL, Ghostscript.NET.GhostscriptLicense.GPL); MessageBox.Show(pathsSelected); if (!string.IsNullOrEmpty(singleFilePath)) { if (!System.IO.File.Exists(singleFilePath)) { MessageBox.Show("The file you entered does not exist. Verify that it does and try again.\nYour entry:\t" + singleFilePath, "Single File does not exist"); } else if (!string.Equals(System.IO.Path.GetExtension(singleFilePath), ".pdf")) { MessageBox.Show("The file you entered is not a PDF. Verify that it is and try again.\nYour entry:\t" + singleFilePath, "Single File not PDF"); } else { Label lblWaitMsg = new Label(); lblWaitMsg.Location = new Point(13, 13); lblWaitMsg.Text = "Please wait while the Image Conversion loads.."; lblWaitMsg.AutoSize = true; System.Windows.Forms.Form frmWait = new Form(); frmWait.Text = "Loading..."; frmWait.UseWaitCursor = true; frmWait.Controls.Add(lblWaitMsg); frmWait.AutoSize = true; frmWait.Show(); ConvertToJPEG(singleFilePath); frmWait.Close(); MessageBox.Show("Single File conversion sucess!"); } } if (!string.IsNullOrEmpty(multFileDir)) { if (!System.IO.Directory.Exists(multFileDir)) { MessageBox.Show("The Directory you entered does not exist. Verify that it does and try again.\nYour entry:\t" + multFileDir, "File Directory does not exist"); return; } else { System.IO.DirectoryInfo multiDirInfo = new System.IO.DirectoryInfo(multFileDir); System.IO.FileInfo[] pdfFiles = multiDirInfo.GetFiles("*.pdf"); if (pdfFiles.Count() <= 0) { MessageBox.Show("The Directory you entered does not have any PDF files. Verify that it does and try again.\nYour entry:\t" + multFileDir, "File Directory conatins no PDFs"); } else { Label lblWaitMsg = new Label(); lblWaitMsg.Location = new Point(13, 13); lblWaitMsg.Text = "Please wait while the Image Conversion loads.."; lblWaitMsg.AutoSize = true; System.Windows.Forms.Form frmWait = new Form(); frmWait.Text = "Loading..."; frmWait.UseWaitCursor = true; frmWait.Controls.Add(lblWaitMsg); frmWait.AutoSize = true; frmWait.Show(); string multiFiles = ""; for (int counter = 0; counter < pdfFiles.Count(); counter++) { multiFiles = multiFiles + pdfFiles[counter].ToString() + "\n"; ConvertToJPEG(multiDirInfo.ToString() + "\\" + pdfFiles[counter].ToString()); } frmWait.Close(); MessageBox.Show("Multiple File sucess!\n" + multiFiles); } } } return; }