星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PdfTrunImage.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(string pdfInputPath, string imageOutputPath,
  33. string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
  34. {
  35. List<string> images = new List<string>();
  36. try
  37. {
  38. PDFFile pdfFile = PDFFile.Open(pdfInputPath);
  39. if (!Directory.Exists(imageOutputPath))
  40. {
  41. Directory.CreateDirectory(imageOutputPath);
  42. }
  43. // validate pageNum
  44. if (startPageNum <= 0)
  45. {
  46. startPageNum = 1;
  47. }
  48. if (endPageNum == 0 || endPageNum > pdfFile.PageCount)
  49. {
  50. endPageNum = pdfFile.PageCount;
  51. }
  52. if (startPageNum > endPageNum)
  53. {
  54. int tempPageNum = startPageNum;
  55. startPageNum = endPageNum;
  56. endPageNum = startPageNum;
  57. }
  58. // start to convert each page
  59. for (int i = startPageNum; i <= endPageNum; i++)
  60. {
  61. Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
  62. string imgPath = imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString();
  63. pageImage.Save(imgPath, imageFormat);
  64. images.Add(imgPath);
  65. // 返回的图片绝对路径集合
  66. pageImage.Dispose();
  67. }
  68. pdfFile.Dispose();
  69. return images;
  70. }
  71. catch (System.Exception ex)
  72. {
  73. LogHelper.WriteErrLog("PDF转图片" +
  74. "【PdfTrunImage】" + ex.Message, ex);
  75. images = new List<string>();
  76. return images;
  77. }
  78. }
  79. }
  80. }