1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| using Aspose.Words.Saving;
using Common.system;
using System; using System.Collections.Generic; using System.IO;
namespace XHWK.WKTool.Utils { class ZAsposeUtil { public static List<string> ConvertWordToImage( string wordInputPath, string imageOutputPath, string imageName, int startPageNum = 0, int endPageNum = 0, Aspose.Words.SaveFormat imageFormat = Aspose.Words.SaveFormat.Png, float resolution = 128 ) { List<string> images = new List<string>(); try { Aspose.Words.Document doc = new Aspose.Words.Document(wordInputPath);
if (doc == null) { throw new Exception("Word文件无效或者Word文件被加密!"); } if (imageOutputPath.Trim().Length == 0) { imageOutputPath = System.IO.Path.GetDirectoryName(wordInputPath); } if (!Directory.Exists(imageOutputPath)) { Directory.CreateDirectory(imageOutputPath); } if (imageName.Trim().Length == 0) { imageName = System.IO.Path.GetFileNameWithoutExtension(wordInputPath); } if (startPageNum <= 0) { startPageNum = 1; } if (endPageNum > doc.PageCount || endPageNum <= 0) { endPageNum = doc.PageCount; } if (startPageNum > endPageNum) { int tempPageNum = startPageNum; startPageNum = endPageNum; endPageNum = startPageNum; } if (resolution <= 0) { resolution = 128; }
ImageSaveOptions imageSaveOptions = new ImageSaveOptions(imageFormat) { Resolution = resolution };
for (int i = startPageNum; i <= endPageNum; i++) { imageSaveOptions.PageIndex = i - 1; string savepath = System.IO.Path.Combine(imageOutputPath, imageName) + "_" + i + "." + imageFormat.ToString(); doc.Save(savepath, imageSaveOptions); images.Add(savepath); } imageSaveOptions = null; doc = null; } catch (Exception ex) { MessageWindow.Show("该文件无法使用!"); LogHelper.WriteErrLog("【导入方法(ConvertWordToImage)】错误日志:" + ex.Message, ex); } return images; } } }
|