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;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 namespace pdf2image.O2S.Components.PDFRender4NET 2 { 3 public static class Program 4 { 5 public enum Definition 6 { 7 One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10 8 } 9 10 11 public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, 12 string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition) 13 { 14 PDFFile pdfFile = PDFFile.Open(pdfInputPath); 15 16 if (!Directory.Exists(imageOutputPath)) 17 { 18 Directory.CreateDirectory(imageOutputPath); 19 } 20 21 // validate pageNum 22 if (startPageNum <= 0) 23 { 24 startPageNum = 1; 25 } 26 27 if (endPageNum > pdfFile.PageCount) 28 { 29 endPageNum = pdfFile.PageCount; 30 } 31 32 if (startPageNum > endPageNum) 33 { 34 int tempPageNum = startPageNum; 35 startPageNum = endPageNum; 36 endPageNum = startPageNum; 37 } 38 39 // start to convert each page 40 for (int i = startPageNum; i <= endPageNum; i++) 41 { 42 Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition); 43 pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat); 44 pageImage.Dispose(); 45 } 46 47 pdfFile.Dispose(); 48 } 49 50 public static void Main(string[] args) 51 { 52 ConvertPDF2Image("C:\FileServer\test.pdf", "C:\FileServer\", "test", 1, 5, ImageFormat.Jpeg, Definition.One); 53 } 54 55 } 56 }
参考资料:
CSDN