|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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;
- }
- }
- }
|