C#中PDF转图片

把 O2S.Components.PDFRender4NET.dll(下载地址,密码hep9) 添加到引用中

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Windows.Forms;
 5 using O2S.Components.PDFRender4NET;
 6 using System.Drawing;
 7 using System.Drawing.Imaging;
 8 using System.IO;
 9 namespace O2S.Components.PDFRender4NET.pdf2image
10 {
11     public static class Program
12     {
13         public enum Definition
14         {
15             One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
16         }
17         /// <summary>
18         /// 将PDF文档转换为图片的方法
19         /// </summary>
20         /// <param name="pdfInputPath">PDF文件路径</param>
21         /// <param name="imageOutputPath">图片输出路径</param>
22         /// <param name="imageName">生成图片的名字</param>
23         /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
24         /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
25         /// <param name="imageFormat">设置所需图片格式</param>
26         /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
27         public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,
28             string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
29         {
30             PDFFile pdfFile = PDFFile.Open(pdfInputPath);
31             if (!Directory.Exists(imageOutputPath))
32             {
33                 Directory.CreateDirectory(imageOutputPath);
34             }
35             // validate pageNum
36             if (startPageNum <= 0)
37             {
38                 startPageNum = 1;
39             }
40             if (endPageNum > pdfFile.PageCount)
41             {
42                 endPageNum = pdfFile.PageCount;
43             }
44             if (startPageNum > endPageNum)
45             {
46                 int tempPageNum = startPageNum;
47                 startPageNum = endPageNum;
48                 endPageNum = startPageNum;
49             }
50             // start to convert each page
51             for (int i = startPageNum; i <= endPageNum; i++)
52             {
53                 Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
54                 pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
55                 pageImage.Dispose();
56             }
57             pdfFile.Dispose();
58         }
59         public static void Main(string[] args)
60         {
61             ConvertPDF2Image("F:\\Events.pdf", "F:\\", "A", 1, 5, ImageFormat.Jpeg, Definition.One);
62         }
63     }
64 }

 

posted @ 2017-08-03 17:17  Cein  阅读(361)  评论(0编辑  收藏  举报