将pdf转成jpg格式

时间:2022-08-13 08:57:07

  参考了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类,

将pdf转成jpg格式将pdf转成jpg格式View Code
 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         }
将pdf转成jpg格式将pdf转成jpg格式View Code
 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         }
将pdf转成jpg格式将pdf转成jpg格式View Code
 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就可以实现啦!