pdf 切割成圖片的方法

 /// <summary>         /// 将PDF文档转换为图片的方法         /// </summary>         /// <param name="pdfInputPath">PDF文件路径</param>         /// <param name="imageOutputPath">图片输出路径</param>         /// <param name="imageName">生成图片的名字</param>         /// <param name="startPageNum">从PDF文档的第几页开始转换</param>         /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>         /// <param name="imageFormat">设置所需图片格式</param>         /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>         public BookInfoViewModel PdfToImages(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, PdfToPicture.ImageQuality definition, BookInfoViewModel BookInfoViewmodel)         {             int imgVerticalResolution = Convert.ToInt32(Settings.imgVerticalResolution);             //var pdfFile = PDFFile.Open(pdfInputPath);

            //if (!Directory.Exists(imageOutputPath))             //{             //    Directory.CreateDirectory(imageOutputPath);             //}

            //// validate pageNum             //if (startPageNum <= 0)             //{             //    startPageNum = 1;             //}

            //if (endPageNum > pdfFile.PageCount)             //{             //    endPageNum = pdfFile.PageCount;             //}

            //if (startPageNum > endPageNum)             //{             //    int tempPageNum = startPageNum;             //    startPageNum = endPageNum;             //    endPageNum = tempPageNum;             //}             //BookInfoViewmodel.PageNoCount = pdfFile.PageCount;             //BookInfoViewmodel.BookPdfToImgInfoModelList = new List<BookInfoViewModel.BookPdfToImgInfoModel>();             //for (int i = startPageNum; i <= endPageNum; i++)             //{             //    BookInfoViewModel.BookPdfToImgInfoModel BookPdfToImgInfomodel = new BookInfoViewModel.BookPdfToImgInfoModel();             //    var pageImage = pdfFile.GetPageImage(i - 1, 96 * (int)definition);             //    int Width = pageImage.Width;             //    int Height = pageImage.Height;             //    if (i == startPageNum)             //    {             //        BookInfoViewmodel.FileDimension = Width + "*" + Height;             //    }             //    BookPdfToImgInfomodel.ImgFileDimension = Width + "*" + Height;             //    BookPdfToImgInfomodel.ImgFileExtension = imageFormat.ToString();             //    BookPdfToImgInfomodel.ImgFileName = imageName + "_" + i.ToString();             //    BookPdfToImgInfomodel.ImgFileNo = Convert.ToInt32(Session["PageCount"]) + i;             //    pageImage.Save(imageOutputPath + "/" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageFormat);             //    pageImage.Dispose();             //    //string sourcePdfFilePath = pdfInputPath;

            //    //將上傳的圖片重新命名             //    //var destFile = GetFilePath(pdfInputPath);             //    //System.IO.File.Copy(pdfInputPath, destFile, true);             //    //切割pdf             //    //GhostscriptWrapper.GeneratePageThumb(destFile, imageOutputPath + "/" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), 1, 150, 150);             //    //刪除複製的文件             //    //System.IO.File.Delete(destFile);

            //    //生成小图             //    CompressionImage.generateImage(imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageOutputPath, "S_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), 150, 150);             //    //生成中图             //    if (Width > 850)             //    {             //        CompressionImage.generateImage(imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageOutputPath, "M_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), 850, 99999);             //    }             //    else             //    {             //        System.IO.File.Copy(Path.Combine(imageOutputPath, imageName + "_" + i.ToString() + "." + imageFormat.ToString()), Path.Combine(imageOutputPath, "M_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString()));             //    }             //    //获取图片的大小             //    FileInfo file = new FileInfo(imageOutputPath + "/" + imageName + "_" + i.ToString() + "." + imageFormat.ToString());             //    BookPdfToImgInfomodel.ImgFileSize = file.Length;             //    BookInfoViewmodel.BookPdfToImgInfoModelList.Add(BookPdfToImgInfomodel);

            //}             //Session["PageCount"] = Convert.ToInt32(Session["PageCount"]) + 1;             //pdfFile.Dispose();

            var destFile = GetFilePath(pdfInputPath);             System.IO.File.Copy(pdfInputPath, destFile, true);             try             {                 //和选择的文件并列创建一个目录                 string directoryPath = imageOutputPath;

                //定义Jpeg转换设备                 Aspose.Pdf.Document document = new Aspose.Pdf.Document(destFile);                 var device = new Aspose.Pdf.Devices.JpegDevice();                 int quality = 100;                 // directoryPath += quality;                 if (!System.IO.Directory.Exists(directoryPath))                 {                     Directory.CreateDirectory(directoryPath);                 }                 //默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩                 //device = new Aspose.Pdf.Devices.JpegDevice(quality);                 //device = new Aspose.Pdf.Devices.JpegDevice(1,, quality,);                 //遍历每一页转为jpg                 endPageNum = 1;                 BookInfoViewmodel.BookPdfToImgInfoModelList = new List<BookInfoViewModel.BookPdfToImgInfoModel>();                 BookInfoViewmodel.PageNoCount = document.Pages.Count;                 for (var i = 1; i <= endPageNum; i++)                 {                     string filePathOutPut = Path.Combine(directoryPath, string.Format("{0}_{1}.{2}", imageName, i.ToString(), imageFormat.ToString()));                     FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);                     try                     {                         BookInfoViewModel.BookPdfToImgInfoModel BookPdfToImgInfomodel = new BookInfoViewModel.BookPdfToImgInfoModel();                         BookPdfToImgInfomodel.ImgFileExtension = imageFormat.ToString();                         BookPdfToImgInfomodel.ImgFileName = imageName + "_" + i.ToString();                         BookPdfToImgInfomodel.ImgFileNo = Convert.ToInt32(Session["PageCount"]) + i;                         BookPdfToImgInfomodel.ImgFileSize = fs.Length;                         BookInfoViewmodel.BookPdfToImgInfoModelList.Add(BookPdfToImgInfomodel);

                        //var pdfFile = PDFFile.Open(pdfInputPath);                         //var pageImage = pdfFile.GetPageImage(i - 1, imgVerticalResolution * (int)definition);//设置分辨率为96dpi                         int Width = 0;                         int Height = 0;                         //設置生成圖片的分辨率                         Aspose.Pdf.Devices.Resolution pdfResolution = new Aspose.Pdf.Devices.Resolution(imgVerticalResolution, imgVerticalResolution);                         //pdfResolution.X = 96;                         //pdfResolution.Y = 96;                         //默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩                         //device = new Aspose.Pdf.Devices.JpegDevice(quality);                         device = new Aspose.Pdf.Devices.JpegDevice(Width, Height, pdfResolution, quality);                         device.Process(document.Pages[i], fs);                         fs.Close();                         //pageImage.Dispose();                         //pdfFile.Dispose();                         //BookInfoViewmodel.FileDimension = Width + "*" + Height;

                        DirectoryInfo folder = new DirectoryInfo(imageOutputPath);                         int pdffileount = 0;                         foreach (FileInfo pdffile in folder.GetFiles())                         {                             if (pdffileount == 0)                             {                                 if (pdffile.Extension.Substring(1, pdffile.Extension.Length - 1) == imageFormat.ToString())                                 {                                     Image newImage = Image.FromFile(pdffile.FullName);                                     Width = newImage.Width;                                     Height = newImage.Height;                                     pdffileount++;                                     newImage.Dispose();                                 }                             }

                        }                         BookInfoViewmodel.FileDimension = Width + "*" + Height;                         //生成小图                         CompressionImage.generateImage(imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageOutputPath, "S_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), 150, 150);                         //生成中图                         if (Width > 850)                         {                             CompressionImage.generateImage(imageName + "_" + i.ToString() + "." + imageFormat.ToString(), imageOutputPath, "M_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString(), 850, 99999);                         }                         else                         {                             System.IO.File.Copy(Path.Combine(imageOutputPath, imageName + "_" + i.ToString() + "." + imageFormat.ToString()), Path.Combine(imageOutputPath, "M_" + imageName + "_" + i.ToString() + "." + imageFormat.ToString()));                         }

                    }                     catch (Exception ex)                     {                         fs.Close();                         System.IO.Directory.Delete(filePathOutPut);                     }                 }                 document.Dispose();             }             catch (Exception ex)             {

            }             finally             {                 System.IO.File.Delete(destFile);             }             return BookInfoViewmodel;         }

原文地址:https://www.cnblogs.com/qhy1277/p/4686367.html