星火微课系统客户端
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PdfTrunImage.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using O2S.Components.PDFRender4NET;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. namespace Common.system
  7. {
  8. /// <summary>
  9. ///
  10. /// Class to convert a pdf to an image using GhostScript DLL
  11. /// Credit for this code go to:Rangel Avulso
  12. /// i only fix a little bug and refactor a little
  13. /// http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c/
  14. /// </summary>
  15. /// <seealso cref="http://www.hrangel.com.br/index.php/2006/12/04/converter-pdf-para-imagem-jpeg-em-c/"/>
  16. public class PdfTrunImage
  17. {
  18. public enum Definition
  19. {
  20. One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
  21. }
  22. /// <summary>
  23. /// 将PDF文档转换为图片的方法
  24. /// </summary>
  25. /// <param name="pdfInputPath">PDF文件路径</param>
  26. /// <param name="imageOutputPath">图片输出路径</param>
  27. /// <param name="imageName">生成图片的名字</param>
  28. /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
  29. /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
  30. /// <param name="imageFormat">设置所需图片格式</param>
  31. /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
  32. public static List<string> ConvertPdf2Image
  33. (
  34. string pdfInputPath,
  35. string imageOutputPath,
  36. string imageName,
  37. int startPageNum,
  38. int endPageNum,
  39. ImageFormat imageFormat,
  40. Definition definition
  41. )
  42. {
  43. List<string> images = new List<string>();
  44. try
  45. {
  46. PDFFile pdfFile = PDFFile.Open(pdfInputPath);
  47. if (!Directory.Exists(imageOutputPath))
  48. {
  49. Directory.CreateDirectory(imageOutputPath);
  50. }
  51. // validate pageNum
  52. if (startPageNum <= 0)
  53. {
  54. startPageNum = 1;
  55. }
  56. if (endPageNum == 0 || endPageNum > pdfFile.PageCount)
  57. {
  58. endPageNum = pdfFile.PageCount;
  59. }
  60. if (startPageNum > endPageNum)
  61. {
  62. startPageNum = endPageNum;
  63. endPageNum = startPageNum;
  64. }
  65. // start to convert each page
  66. for (int i = startPageNum; i <= endPageNum; i++)
  67. {
  68. Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
  69. string imgPath = imageOutputPath + imageName + i.ToString() + "." + imageFormat;
  70. pageImage.Save(imgPath, imageFormat);
  71. images.Add(imgPath);
  72. // 返回的图片绝对路径集合
  73. pageImage.Dispose();
  74. }
  75. pdfFile.Dispose();
  76. return images;
  77. }
  78. catch (System.Exception ex)
  79. {
  80. LogHelper.Logerror.Error("PDF转图片" + "【PdfTrunImage】" + ex.Message, ex);
  81. images = new List<string>();
  82. return images;
  83. }
  84. }
  85. }
  86. }