星火微课系统客户端
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZAsposeUtil.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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="pdfInputPath">Word文件路径</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. string wordInputPath,
  23. string imageOutputPath,
  24. string imageName,
  25. int startPageNum = 0,
  26. int endPageNum = 0,
  27. Aspose.Words.SaveFormat imageFormat = Aspose.Words.SaveFormat.Png,
  28. float resolution = 128
  29. )
  30. {
  31. // 返回的图片绝对路径集合
  32. List<string> images = new List<string>();
  33. try
  34. {
  35. // open word file
  36. Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
  37. // validate parameter
  38. if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); }
  39. if (imageOutputPath.Trim().Length == 0) { imageOutputPath = System.IO.Path.GetDirectoryName(wordInputPath); }
  40. if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); }
  41. if (imageName.Trim().Length == 0) { imageName = System.IO.Path.GetFileNameWithoutExtension(wordInputPath); }
  42. if (startPageNum <= 0) { startPageNum = 1; }
  43. if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; }
  44. if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; }
  45. if (resolution <= 0) { resolution = 128; }
  46. ImageSaveOptions imageSaveOptions = new ImageSaveOptions(imageFormat)
  47. {
  48. Resolution = resolution
  49. };
  50. // start to convert each page
  51. for (int i = startPageNum; i <= endPageNum; i++)
  52. {
  53. imageSaveOptions.PageIndex = i - 1;
  54. string savepath = System.IO.Path.Combine(imageOutputPath, imageName) + "_" + i + "." + imageFormat.ToString();
  55. doc.Save(savepath, imageSaveOptions);
  56. images.Add(savepath);
  57. }
  58. imageSaveOptions = null;
  59. doc = null;
  60. }
  61. catch (Exception ex)
  62. {
  63. MessageWindow.Show("该文件无法使用!");
  64. LogHelper.WriteErrLog("【导入方法(ConvertWordToImage)】错误日志:" + ex.Message, ex);
  65. }
  66. return images;
  67. }
  68. }
  69. }