将pdf转成jpg格式
参考了http://www.cnblogs.com/luowanli/archive/2012/08/16/2641185.html作者发给我的源码,非常感谢他,然后稍微进行了一下修改之后,封装成了一个dll组件以供程序进行调用。这边记录一下思路。
网上有很多将pdf转为jpg的软件,不过这是个小功能,所以使用开源的软件。在http://topic.csdn.net/u/20120219/20/4888d128-3b77-47bc-aa21-cb02c014bc1f.html介绍的很详细。我使用的是其中一种,GhostScript,比较简单~
首先新建一个类库,将我们需要用到的GhostScript的gsdll32.dll和PDFView.dll放到bin目录,然后建一个PdfConvert.cs类,

1 /// <summary> 2 /// 将单页pdf转为图片 3 /// </summary> 4 /// <param name="PDFPath">PDF 文件路径</param> 5 /// <param name="Page">需要转换的页码</param> 6 /// <returns></returns> 7 private byte[] GetImgData(string PDFPath, int Page) 8 { 9 System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(PDFPath, Page, 300, "", true); 10 return GetDataByImg(img);//读取img的数据并返回 11 }

1 /// <summary> 2 /// 将图片转为字节流 3 /// </summary> 4 /// <param name="_image"></param> 5 /// <returns></returns> 6 private byte[] GetDataByImg(System.Drawing.Image _image) 7 { 8 System.IO.MemoryStream Ms = new MemoryStream(); 9 _image.Save(Ms, System.Drawing.Imaging.ImageFormat.Jpeg); 10 byte[] imgdata = new byte[Ms.Length]; 11 Ms.Position = 0; 12 Ms.Read(imgdata, 0, Convert.ToInt32(Ms.Length)); 13 Ms.Close(); 14 return imgdata; 15 }

1 /// <summary> 2 /// 将PDF 相应的页转换为图片 3 /// </summary> 4 /// <param name="strPDFpath">PDF 路径</param> 5 public string[] GetImage(string strPDFpath) 6 { 7 FileStream fs = new FileStream(strPDFpath, FileMode.Open, FileAccess.Read); 8 StreamReader r = new StreamReader(fs); 9 string pdfText = r.ReadToEnd(); 10 string filename = string.Empty;//文件后缀名 11 string[] imgpath; 12 13 if (strPDFpath.Contains("\\")) 14 { 15 string[] arr = strPDFpath.Split('\\'); 16 filename = arr[arr.Length - 1]; 17 } 18 else 19 { 20 string[] arr = strPDFpath.Split('/'); 21 filename = arr[arr.Length - 1]; 22 } 23 filename = filename.Remove(filename.Length - System.IO.Path.GetExtension(filename).Length); 24 Regex rx1 = new Regex(@"/Type\s*/Page[^s]");//用于判断pdf有多少页的正则表达式 25 MatchCollection matches = rx1.Matches(pdfText); 26 int PDFPageCount = Convert.ToInt32(matches.Count); 27 try 28 { 29 if (PDFPageCount > 0) 30 { 31 imgpath = new string[PDFPageCount]; 32 FileInfo file = new FileInfo(strPDFpath); 33 string strSavePath = file.Directory.FullName; 34 for (int i = 1; i <= PDFPageCount; i++) 35 { 36 byte[] ImgData = GetImgData(strPDFpath, i); 37 38 MemoryStream ms = new MemoryStream(ImgData, 0, ImgData.Length); 39 Bitmap returnImage = (Bitmap)Bitmap.FromStream(ms); 40 41 string strImgPath = Path.Combine(strSavePath, string.Format(filename + "{0}.jpg", i)); 42 returnImage.Save(strImgPath); 43 imgpath[i - 1] = strImgPath; 44 } 45 return imgpath; 46 } 47 return null; 48 } 49 catch (Exception ex) 50 { 51 52 throw ex; 53 } 54 55 }
点击生成解决方案,这时会生成dll文件,在新项目中引用前面2个dll加上这个dll就可以实现啦!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述