星火微课系统客户端
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ZAsposeUtil.cs 3.8KB

il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
il y a 1 an
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Aspose.Words.Saving;
  2. using Common.system;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. namespace XHWK.WKTool.Utils
  7. {
  8. class ZAsposeUtil
  9. {
  10. /// <summary>
  11. /// 将Word文档转换为图片的方法(该方法基于第三方DLL),
  12. /// 你可以像这样调用该方法: ConvertPDF2Image("F:\\PdfFile.doc", "F:\\","ImageFile", 1, 20);
  13. /// </summary>
  14. /// <param name="wordInputPath"></param>
  15. /// <param name="imageOutputPath">图片输出路径,如果为空,默认值为Word所在路径</param>
  16. /// <param name="imageName">图片的名字,不需要带扩展名,如果为空,默认值为Word的名称</param>
  17. /// <param name="startPageNum">从PDF文档的第几页开始转换,如果为0,默认值为1</param>
  18. /// <param name="endPageNum">从PDF文档的第几页开始停止转换,如果为0,默认值为Word总页数</param>
  19. /// <param name="imageFormat">设置所需图片格式,如果为null,默认格式为PNG</param>
  20. /// <param name="resolution">设置图片的像素,数字越大越清晰,如果为0,默认值为128,建议最大值不要超过1024</param>
  21. public static List<string> ConvertWordToImage
  22. (
  23. string wordInputPath,
  24. string imageOutputPath,
  25. string imageName,
  26. int startPageNum = 0,
  27. int endPageNum = 0,
  28. Aspose.Words.SaveFormat imageFormat = Aspose.Words.SaveFormat.Png,
  29. float resolution = 128
  30. )
  31. {
  32. // 返回的图片绝对路径集合
  33. List<string> images = new List<string>();
  34. try
  35. {
  36. // open word file
  37. Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
  38. // validate parameter
  39. if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
  40. if (imageOutputPath.Trim().Length == 0) { imageOutputPath = System.IO.Path.GetDirectoryName(wordInputPath); }
  41. if (!Directory.Exists(imageOutputPath))
  42. {
  43. if (imageOutputPath != null)
  44. {
  45. Directory.CreateDirectory(imageOutputPath);
  46. }
  47. }
  48. if (imageName.Trim().Length == 0) { imageName = System.IO.Path.GetFileNameWithoutExtension(wordInputPath); }
  49. if (startPageNum <= 0) { startPageNum = 1; }
  50. if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; }
  51. if (startPageNum > endPageNum)
  52. {
  53. startPageNum = endPageNum;
  54. endPageNum = startPageNum;
  55. }
  56. if (resolution <= 0) { resolution = 128; }
  57. ImageSaveOptions imageSaveOptions = new ImageSaveOptions(imageFormat)
  58. {
  59. Resolution = resolution
  60. };
  61. // start to convert each page
  62. for (int i = startPageNum; i <= endPageNum; i++)
  63. {
  64. imageSaveOptions.PageIndex = i - 1;
  65. if (imageOutputPath != null)
  66. {
  67. string savepath = System.IO.Path.Combine(imageOutputPath, imageName) + "_" + i + "." + imageFormat.ToString();
  68. doc.Save(savepath, imageSaveOptions);
  69. images.Add(savepath);
  70. }
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageWindow.Show("该文件无法使用!");
  76. LogHelper.Logerror.Error("【导入方法(ConvertWordToImage)】错误日志:" + ex.Message, ex);
  77. }
  78. return images;
  79. }
  80. }
  81. }