/// <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;         }

pdf 切割成圖片的方法的更多相关文章

  1. C#pdf 切割成图片

    引用 using Ghostscript.NET;using Ghostscript.NET.Rasterizer; 需要安装 exe文件 public static GhostscriptVersi ...

  2. PdfReader按页将PDF切割成多个PDF

    private MemoryStream GetNewPdfByPageNum(PdfReader pdfReader, int pageNum) { MemoryStream memoryStrea ...

  3. 如何用ABBYY把PDF转换成PPT

    在电子科技迅速发展的今天,文件格式转换并不是什么稀罕事,因为现在都是电子化办公,出现很多文件格式,但是不同的场合需要的格式不同,所以常常需要进行文件格式的转换.PDF转换成PPT也是众多文件格式转换中 ...

  4. oracle 字符串切割成结果集方法

    oracle字符串切割几种方式 方法一: SELECT COLUMN_VALUE FROM TABLE(SYS.ODCIVARCHAR2LIST('1','2','3','4','5')); 方法二: ...

  5. ABBYY把pdf转换成word的方法

    有时候我们在网上下载的资料文献是PDF格式文档,遇到喜欢的字句总忍不住想要收藏起来,但是PDF文档不同于普通的Word文档可以直接进行复制粘贴,需要下载安装相关的编辑工具,才能对文字内容进行编辑.倒不 ...

  6. 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片

    项目中运用到pdf文件转换成image图片,开始时使用pdfbox开源库进行图片转换,但是转换出来的文件中含有部分乱码的情况.下面是pdfBox 的pdf转换图片的代码示例. try{ String ...

  7. C#技术分享【PDF转换成图片——13种方案】(2013-07-25重新整理)

    原文:C#技术分享[PDF转换成图片--13种方案](2013-07-25重新整理) 重要说明:本博已迁移到 石佳劼的博客,有疑问请到 文章新地址 留言!!! 写在最前面:为了节约大家时间,撸主把最常 ...

  8. php 将pdf转成图片且将图片拼接

    说明: 1.pdf转图片通过安装php扩展imagick实现. 2.由于windows扩展安装的一系列问题,建议在linux环境开发,windows大伙可以尝试安装. 3.为Centos 安装Imag ...

  9. Android中将一个图片切割成多个图片

    有种场景,我们想将一个图片切割成多个图片.比如我们在开发一个拼图的游戏,就首先要对图片进行切割. 以下是封装好的两个类,可以实现图片的切割.仅供参考和学习. 一个是ImagePiece类,此类保存了一 ...

随机推荐

  1. filesort是通过相应的排序算法

    filesort是通过相应的排序算法,将取得的数据在max_length_for_sort_data的默认值是1024. --------------------------------------- ...

  2. 在CentOS中安装中文支持

    执行命令: # yum groupinstall chinese-support 修改配置文件: # vim /etc/sysconfig/i18n LANG="zh_CN.UTF-8&qu ...

  3. [2014.01.27]wfPng 水印贴图组件 2.1

    支持载入bmp,jpg,gif,png四种格式等. 支持图片水印使用png格式,支持区域透明,半透明的png图片. 支持图片缩放. 支持画线.画矩形.画椭圆,画正圆等图形. 支持在图片上输出文字. 能 ...

  4. 动态linq to list排序

    public class QeurySort { public static IList<T> Sort<T>(IList<T> list,string sidx, ...

  5. 测试家庭流媒体服务器Windows7

    测试首先选择了Darwin Streaming Server (DSS) for Windows 下载地址:http://dss.macosforge.org/downloads/DarwinStre ...

  6. 简单设置eworkflow条件的方式

    在eworkflow自定义工作流产品中,设置条件节点,是在节点的后续连线上设置的.每一个处理节点(除结束节点外)都至少有一条连线连接到下一个节点,当有多条连线连接到其他节点的时候,就需要在多出的连线上 ...

  7. mac-文本编辑器

    windows时代最喜欢的文本编辑器一直是ultraedit,但到了mac下,破解的ultraedit退出时会异常,于是琢磨着换编辑器,最终选择了sublime text2,百度下载,不注册也可以用. ...

  8. upgrade to ubuntu14.04, ibus input

    升级后ibus拼音输入很奇怪,可能和其他输入法产生混淆.解决很简单,只要打开终端输入 ibus-daemon -drx 不用重启或者登出问题便解决.

  9. Arch Linux LibreOffice 中文输入法不能切换

    From: http://blog.csdn.net/shallowgrave/article/details/8501629 卸载libreoffice-kde4 # pacman -R libre ...

  10. 外壳exe通过反射调用dll时

    外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...