Convert pdf to jpg format

Source: Internet
Author: User

Refer to idea. Record your ideas here.

There are a lot of software for converting pdf files into jpg files on the Internet, but this is a small feature, so open-source software is used. In. I use one of them, GhostScript, which is relatively simple ~

First, create a new class library, put the gsdll32.dll and consumer view. dll of the GhostScript we need to use in the bin directory, and then create a consumer convert. cs class,

View Code

1 // <summary> 2 // convert a single-page pdf file to an image 3 /// </summary> 4 /// <param name = "PDFPath"> PDF file path </param> 5 // <param name = "Page"> Page number to be converted </param> 6 /// <returns> </returns> 7 private byte [] GetImgData (string PDFPath, int Page) 8 {9 System. drawing. image img = lateral view. convertPDF. PDFConvert. getPageFromPDF (PDFPath, Page, 300, "", true); 10 return GetDataByImg (img); // read img data and return 11}
View Code

1 /// <summary> 2 /// convert the image into a byte stream 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}
View Code

1 // <summary> 2 // convert the corresponding PDF page to image 3 /// </summary> 4 /// <param name = "strPDFpath"> PDF path </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 plain text = r. readToEnd (); 10 string filename = string. empty; // file suffix 11 string [] imgpath; 12 13 if (strPDFpath. contains ("\") 14 {15 string [] arr = strPDFpath. split ('\'); 16 filename = arr [arr. length-1]; 17} 18 else19 {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]"); // determine the number of pages of a pdf. The regular expression 25 MatchCollection matches = rx1.Matches (plain text); 26 int PDFPageCount = Convert. toInt32 (matches. count); 27 try28 {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 + "201702.16.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}

Click Generate solution. The dll file will be generated. In the new project, you can add this dll to reference the first two dll files!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.