ConverterDialogsModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using ComPDFKit_Conversion.Converter;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ComDocumentAIKit;
  8. using ComPDFKit_Conversion.Options;
  9. using Microsoft.Office.Interop.Word;
  10. using PDF_Master.Helper;
  11. using PDF_Master.CustomControl;
  12. using System.Net.Mime;
  13. using System.Threading;
  14. using System.Windows.Threading;
  15. using ImTools;
  16. using PDF_Master.Properties;
  17. namespace PDF_Master.Model.Dialog.ConverterDialogs
  18. {
  19. public class ConvertOptions
  20. {
  21. public COCRLanguage OCRLanguage = COCRLanguage.COCRLanguageEnglish;
  22. public bool IsAllowOCR = false;
  23. public bool IsContainOCRBgImage = true;
  24. public bool IsContainAnnotations = true;
  25. public bool IsContainImages = true;
  26. public ContentOptions ContentOpts = ContentOptions.AllContent;
  27. public WorksheetOptions WorksheetOpts = WorksheetOptions.ForEachPage;
  28. public PageAndNavigationPaneOptions PageAndNavigationPaneOpts = PageAndNavigationPaneOptions.SinglePageNavigationByBookmarks;
  29. public ImgType ImageType = ImgType.PNG;
  30. public uint ImageDpi = 150;
  31. public bool IsMergeCsv = false;
  32. public LayoutOptions LayoutOpts = LayoutOptions.RetainPageLayout;
  33. }
  34. public class ConverterDialogsModel
  35. {
  36. /// <summary>
  37. /// 文档地址;
  38. /// </summary>
  39. public string InputPath = "";
  40. /// <summary>
  41. /// 输出地址;
  42. /// </summary>
  43. public string OutputPath = "";
  44. /// <summary>
  45. /// 进度;
  46. /// </summary>
  47. public OnProgress GetProgress = null;
  48. /// <summary>
  49. /// 页码 页码范围;
  50. /// </summary>
  51. public List<int> PageIndexLists = new List<int> { };
  52. /// <summary>
  53. /// 页面信息
  54. /// </summary>
  55. public string PageRange = "";
  56. /// <summary>
  57. /// 文档密码;
  58. /// </summary>
  59. public string Pawssword = "";
  60. /// <summary>
  61. /// 文档密码;
  62. /// </summary>
  63. public bool IsCustomFileName = true;
  64. /// <summary>
  65. /// 文档密码;
  66. /// </summary>
  67. public string ConvertType = "";
  68. /// <summary>
  69. /// OCR,与部分功能;
  70. /// </summary>
  71. public ConvertOptions Options = new ConvertOptions();
  72. /// <summary>
  73. /// 取消进度条
  74. /// </summary>
  75. private async void TaskMethod()
  76. {
  77. await System.Threading.Tasks.Task.Run(() =>
  78. {
  79. //当进度条消失时,结束进程(由于取消时OnOpened没有完全结束,等待OnOpened结束时就会结束)
  80. for (int i = 1; App.mainWindowViewModel.IsProcessVisible != System.Windows.Visibility.Collapsed; i++)
  81. {
  82. try
  83. {
  84. Thread.Sleep(500 + i * 500);
  85. if (i < App.mainWindowViewModel.MaxValue)
  86. App.mainWindowViewModel.Value = i;
  87. }
  88. catch { }
  89. }
  90. });
  91. }
  92. private void ConverterClear()
  93. {
  94. //取消转档
  95. ConverterHelper.Clear(ConvertType);
  96. //取消转档的进度条,防止转档还没结束,导致取消后程序崩溃,添加一个取消进度条
  97. App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed;
  98. App.mainWindowViewModel.ProgressTitle = "Canceling";
  99. //判断取消进度条消失逻辑
  100. TaskMethod();
  101. //App.mainWindowViewModel.ProgressTitle = "";
  102. //App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  103. }
  104. private void SetProgress(int pageIndex)
  105. {
  106. App.mainWindowViewModel.Value = pageIndex;
  107. if (App.mainWindowViewModel.Value == App.mainWindowViewModel.MaxValue) { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  108. }
  109. /// <summary>
  110. /// 转档限制转页面前10页
  111. /// </summary>
  112. /// <param name="converterDialogsModel"></param>
  113. private void limitationConvert(ConverterDialogsModel converterDialogsModel)
  114. {
  115. if (Settings.Default.UserDate.subscribestatus!=1)
  116. {
  117. //当前文档前10页
  118. //int limitationindex = 0;
  119. ////此链表从0开始,查询大于或等于10的索引值
  120. //for (int i = 0; converterDialogsModel.PageIndexLists.Count > i; i++)
  121. //{
  122. // if (converterDialogsModel.PageIndexLists[i] >= 10)
  123. // {
  124. // limitationindex = i;
  125. // break;
  126. // }
  127. //}
  128. ////安全检查判断返回的大于或等于10的索引值是否正常
  129. //if (converterDialogsModel.PageIndexLists[limitationindex] >= 10)
  130. //{
  131. // //删除大于10或等于10之后的Item
  132. // converterDialogsModel.PageIndexLists.RemoveRange(limitationindex, converterDialogsModel.PageIndexLists.Count - limitationindex);
  133. //}
  134. //链表10页
  135. if (converterDialogsModel.PageIndexLists.Count >= 11)
  136. {
  137. converterDialogsModel.PageIndexLists.RemoveRange(10, converterDialogsModel.PageIndexLists.Count-10);
  138. }
  139. }
  140. }
  141. public async void OnOpened(Prism.Services.Dialogs.DialogResult e)
  142. {
  143. if (e.Result == Prism.Services.Dialogs.ButtonResult.Cancel || e.Result == Prism.Services.Dialogs.ButtonResult.None) { return; }
  144. bool result = false;
  145. string convertType = "";
  146. OnProgress GetProgress = null;
  147. GetProgress += SetProgress;
  148. App.mainWindowViewModel.ProcessCloseAction = ConverterClear;
  149. convertType = e.Parameters.GetValue<string>(ParameterNames.ConverterType).ToString();
  150. ConvertType = convertType;
  151. App.mainWindowViewModel.ProgressTitle = "Converting";
  152. App.mainWindowViewModel.Value = 0;
  153. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Visible;
  154. switch (convertType)
  155. {
  156. case "CSV":
  157. ConverterCSVDialogModel ConverterCSVModel = e.Parameters.GetValue<ConverterCSVDialogModel>(ParameterNames.ConverterTypeModel);
  158. if (ConverterCSVModel != null)
  159. {
  160. //先对链表做处理,在获取链表个数
  161. limitationConvert(ConverterCSVModel);
  162. App.mainWindowViewModel.MaxValue = ConverterCSVModel.PageIndexLists.Count();
  163. for (int i = 0; ConverterCSVModel.PageIndexLists.Count > i; i++)
  164. {
  165. ConverterCSVModel.PageIndexLists[i] = ConverterCSVModel.PageIndexLists[i] + 1;
  166. }
  167. result = await ConverterHelper.CSVConvert(ConverterCSVModel.InputPath, ConverterCSVModel.OutputPath, GetProgress, ConverterCSVModel.PageIndexLists, ConverterCSVModel.Pawssword, ConverterCSVModel.csvOptions, ConverterCSVModel.IsCustomFileName);
  168. }
  169. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  170. break;
  171. case "Excel":
  172. ConverterExcelDialogModel ConverterExcelModel = e.Parameters.GetValue<ConverterExcelDialogModel>(ParameterNames.ConverterTypeModel);
  173. if (ConverterExcelModel != null)
  174. {
  175. limitationConvert(ConverterExcelModel);
  176. App.mainWindowViewModel.MaxValue = ConverterExcelModel.PageIndexLists.Count();
  177. for (int i = 0; ConverterExcelModel.PageIndexLists.Count > i; i++)
  178. {
  179. ConverterExcelModel.PageIndexLists[i] = ConverterExcelModel.PageIndexLists[i] + 1;
  180. }
  181. result = await ConverterHelper.ExcelConvert(ConverterExcelModel.InputPath, ConverterExcelModel.OutputPath, GetProgress, ConverterExcelModel.PageIndexLists, ConverterExcelModel.Pawssword, ConverterExcelModel.excelOptions);
  182. }
  183. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  184. break;
  185. case "HTML":
  186. ConverterHTMLDialogModel ConverterHTMLModel = e.Parameters.GetValue<ConverterHTMLDialogModel>(ParameterNames.ConverterTypeModel);
  187. if (ConverterHTMLModel != null)
  188. {
  189. limitationConvert(ConverterHTMLModel);
  190. App.mainWindowViewModel.MaxValue = ConverterHTMLModel.PageIndexLists.Count();
  191. for (int i = 0; ConverterHTMLModel.PageIndexLists.Count > i; i++)
  192. {
  193. ConverterHTMLModel.PageIndexLists[i] = ConverterHTMLModel.PageIndexLists[i] + 1;
  194. }
  195. result = await ConverterHelper.HTMLConvert(ConverterHTMLModel.InputPath, ConverterHTMLModel.OutputPath, GetProgress, ConverterHTMLModel.PageIndexLists, ConverterHTMLModel.Pawssword, ConverterHTMLModel.htmlOptions);
  196. }
  197. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  198. break;
  199. case "Img":
  200. ConverterImgDialogModel ConverterImgModel = e.Parameters.GetValue<ConverterImgDialogModel>(ParameterNames.ConverterTypeModel);
  201. if (ConverterImgModel != null)
  202. {
  203. limitationConvert(ConverterImgModel);
  204. App.mainWindowViewModel.MaxValue = ConverterImgModel.PageIndexLists.Count();
  205. for (int i = 0; ConverterImgModel.PageIndexLists.Count > i; i++)
  206. {
  207. ConverterImgModel.PageIndexLists[i] = ConverterImgModel.PageIndexLists[i] + 1;
  208. }
  209. result = await ConverterHelper.ImgConvert(ConverterImgModel.InputPath, ConverterImgModel.OutputPath, GetProgress, ConverterImgModel.PageIndexLists, ConverterImgModel.Pawssword, ConverterImgModel.imageType, ConverterImgModel.imgOptions);
  210. }
  211. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  212. break;
  213. case "PPT":
  214. ConverterPPTDialogModel ConverterPPTModel = e.Parameters.GetValue<ConverterPPTDialogModel>(ParameterNames.ConverterTypeModel);
  215. if (ConverterPPTModel != null)
  216. {
  217. limitationConvert(ConverterPPTModel);
  218. App.mainWindowViewModel.MaxValue = ConverterPPTModel.PageIndexLists.Count();
  219. for (int i = 0; ConverterPPTModel.PageIndexLists.Count > i; i++)
  220. {
  221. ConverterPPTModel.PageIndexLists[i] = ConverterPPTModel.PageIndexLists[i] + 1;
  222. }
  223. result = await ConverterHelper.PPTConvert(ConverterPPTModel.InputPath, ConverterPPTModel.OutputPath, GetProgress, ConverterPPTModel.PageIndexLists, ConverterPPTModel.Pawssword, ConverterPPTModel.pPTOptions);
  224. }
  225. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  226. break;
  227. case "RTF":
  228. //App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed;
  229. ConverterRTFDialogModel ConverterRTFModel = e.Parameters.GetValue<ConverterRTFDialogModel>(ParameterNames.ConverterTypeModel);
  230. if (ConverterRTFModel != null)
  231. {
  232. limitationConvert(ConverterRTFModel);
  233. App.mainWindowViewModel.MaxValue = ConverterRTFModel.PageIndexLists.Count();
  234. for (int i = 0; ConverterRTFModel.PageIndexLists.Count > i; i++)
  235. {
  236. ConverterRTFModel.PageIndexLists[i] = ConverterRTFModel.PageIndexLists[i] + 1;
  237. }
  238. result = await ConverterHelper.RTFConvert(ConverterRTFModel.InputPath, ConverterRTFModel.OutputPath, GetProgress, ConverterRTFModel.PageIndexLists, ConverterRTFModel.Pawssword, ConverterRTFModel.rTFOptions);
  239. }
  240. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  241. break;
  242. case "Text":
  243. ConverterTextDialogModel ConverterTextModel = e.Parameters.GetValue<ConverterTextDialogModel>(ParameterNames.ConverterTypeModel);
  244. if (ConverterTextModel != null)
  245. {
  246. limitationConvert(ConverterTextModel);
  247. App.mainWindowViewModel.MaxValue = ConverterTextModel.PageIndexLists.Count();
  248. for (int i = 0; ConverterTextModel.PageIndexLists.Count > i; i++)
  249. {
  250. ConverterTextModel.PageIndexLists[i] = ConverterTextModel.PageIndexLists[i] + 1;
  251. }
  252. result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword, ConverterTextModel.txtOptions);
  253. }
  254. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  255. break;
  256. case "Word":
  257. ConverterWordDialogModel ConverterWordModel = e.Parameters.GetValue<ConverterWordDialogModel>(ParameterNames.ConverterTypeModel);
  258. if (ConverterWordModel != null)
  259. {
  260. limitationConvert(ConverterWordModel);
  261. App.mainWindowViewModel.MaxValue = ConverterWordModel.PageIndexLists.Count();
  262. for (int i = 0; ConverterWordModel.PageIndexLists.Count > i; i++)
  263. {
  264. ConverterWordModel.PageIndexLists[i] = ConverterWordModel.PageIndexLists[i] + 1;
  265. }
  266. result = await ConverterHelper.WordConvert(ConverterWordModel.InputPath, ConverterWordModel.OutputPath, GetProgress, ConverterWordModel.PageIndexLists, ConverterWordModel.Pawssword, ConverterWordModel.wordOptions);
  267. }
  268. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  269. break;
  270. default:
  271. break;
  272. }
  273. if (!result)
  274. {
  275. AlertsMessage alertsMessage = new AlertsMessage();
  276. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Convert_FailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  277. }
  278. App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
  279. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  280. }
  281. }
  282. }