ConverterDialogsModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. App.mainWindowViewModel.MaxValue = ConverterCSVModel.PageIndexLists.Count();
  161. limitationConvert(ConverterCSVModel);
  162. for (int i = 0; ConverterCSVModel.PageIndexLists.Count > i; i++)
  163. {
  164. ConverterCSVModel.PageIndexLists[i] = ConverterCSVModel.PageIndexLists[i] + 1;
  165. }
  166. result = await ConverterHelper.CSVConvert(ConverterCSVModel.InputPath, ConverterCSVModel.OutputPath, GetProgress, ConverterCSVModel.PageIndexLists, ConverterCSVModel.Pawssword, ConverterCSVModel.csvOptions, ConverterCSVModel.IsCustomFileName);
  167. }
  168. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  169. break;
  170. case "Excel":
  171. ConverterExcelDialogModel ConverterExcelModel = e.Parameters.GetValue<ConverterExcelDialogModel>(ParameterNames.ConverterTypeModel);
  172. if (ConverterExcelModel != null)
  173. {
  174. App.mainWindowViewModel.MaxValue = ConverterExcelModel.PageIndexLists.Count();
  175. limitationConvert(ConverterExcelModel);
  176. for (int i = 0; ConverterExcelModel.PageIndexLists.Count > i; i++)
  177. {
  178. ConverterExcelModel.PageIndexLists[i] = ConverterExcelModel.PageIndexLists[i] + 1;
  179. }
  180. result = await ConverterHelper.ExcelConvert(ConverterExcelModel.InputPath, ConverterExcelModel.OutputPath, GetProgress, ConverterExcelModel.PageIndexLists, ConverterExcelModel.Pawssword, ConverterExcelModel.excelOptions);
  181. }
  182. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  183. break;
  184. case "HTML":
  185. ConverterHTMLDialogModel ConverterHTMLModel = e.Parameters.GetValue<ConverterHTMLDialogModel>(ParameterNames.ConverterTypeModel);
  186. if (ConverterHTMLModel != null)
  187. {
  188. App.mainWindowViewModel.MaxValue = ConverterHTMLModel.PageIndexLists.Count();
  189. limitationConvert(ConverterHTMLModel);
  190. for (int i = 0; ConverterHTMLModel.PageIndexLists.Count > i; i++)
  191. {
  192. ConverterHTMLModel.PageIndexLists[i] = ConverterHTMLModel.PageIndexLists[i] + 1;
  193. }
  194. result = await ConverterHelper.HTMLConvert(ConverterHTMLModel.InputPath, ConverterHTMLModel.OutputPath, GetProgress, ConverterHTMLModel.PageIndexLists, ConverterHTMLModel.Pawssword, ConverterHTMLModel.htmlOptions);
  195. }
  196. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  197. break;
  198. case "Img":
  199. ConverterImgDialogModel ConverterImgModel = e.Parameters.GetValue<ConverterImgDialogModel>(ParameterNames.ConverterTypeModel);
  200. if (ConverterImgModel != null)
  201. {
  202. App.mainWindowViewModel.MaxValue = ConverterImgModel.PageIndexLists.Count();
  203. limitationConvert(ConverterImgModel);
  204. for (int i = 0; ConverterImgModel.PageIndexLists.Count > i; i++)
  205. {
  206. ConverterImgModel.PageIndexLists[i] = ConverterImgModel.PageIndexLists[i] + 1;
  207. }
  208. result = await ConverterHelper.ImgConvert(ConverterImgModel.InputPath, ConverterImgModel.OutputPath, GetProgress, ConverterImgModel.PageIndexLists, ConverterImgModel.Pawssword, ConverterImgModel.imageType, ConverterImgModel.imgOptions);
  209. }
  210. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  211. break;
  212. case "PPT":
  213. ConverterPPTDialogModel ConverterPPTModel = e.Parameters.GetValue<ConverterPPTDialogModel>(ParameterNames.ConverterTypeModel);
  214. if (ConverterPPTModel != null)
  215. {
  216. App.mainWindowViewModel.MaxValue = ConverterPPTModel.PageIndexLists.Count();
  217. limitationConvert(ConverterPPTModel);
  218. for (int i = 0; ConverterPPTModel.PageIndexLists.Count > i; i++)
  219. {
  220. ConverterPPTModel.PageIndexLists[i] = ConverterPPTModel.PageIndexLists[i] + 1;
  221. }
  222. result = await ConverterHelper.PPTConvert(ConverterPPTModel.InputPath, ConverterPPTModel.OutputPath, GetProgress, ConverterPPTModel.PageIndexLists, ConverterPPTModel.Pawssword, ConverterPPTModel.pPTOptions);
  223. }
  224. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  225. break;
  226. case "RTF":
  227. //App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Collapsed;
  228. ConverterRTFDialogModel ConverterRTFModel = e.Parameters.GetValue<ConverterRTFDialogModel>(ParameterNames.ConverterTypeModel);
  229. if (ConverterRTFModel != null)
  230. {
  231. App.mainWindowViewModel.MaxValue = ConverterRTFModel.PageIndexLists.Count();
  232. limitationConvert(ConverterRTFModel);
  233. for (int i = 0; ConverterRTFModel.PageIndexLists.Count > i; i++)
  234. {
  235. ConverterRTFModel.PageIndexLists[i] = ConverterRTFModel.PageIndexLists[i] + 1;
  236. }
  237. result = await ConverterHelper.RTFConvert(ConverterRTFModel.InputPath, ConverterRTFModel.OutputPath, GetProgress, ConverterRTFModel.PageIndexLists, ConverterRTFModel.Pawssword, ConverterRTFModel.rTFOptions);
  238. }
  239. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  240. break;
  241. case "Text":
  242. ConverterTextDialogModel ConverterTextModel = e.Parameters.GetValue<ConverterTextDialogModel>(ParameterNames.ConverterTypeModel);
  243. if (ConverterTextModel != null)
  244. {
  245. App.mainWindowViewModel.MaxValue = ConverterTextModel.PageIndexLists.Count();
  246. limitationConvert(ConverterTextModel);
  247. for (int i = 0; ConverterTextModel.PageIndexLists.Count > i; i++)
  248. {
  249. ConverterTextModel.PageIndexLists[i] = ConverterTextModel.PageIndexLists[i] + 1;
  250. }
  251. result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword, ConverterTextModel.txtOptions);
  252. }
  253. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  254. break;
  255. case "Word":
  256. ConverterWordDialogModel ConverterWordModel = e.Parameters.GetValue<ConverterWordDialogModel>(ParameterNames.ConverterTypeModel);
  257. if (ConverterWordModel != null)
  258. {
  259. App.mainWindowViewModel.MaxValue = ConverterWordModel.PageIndexLists.Count();
  260. limitationConvert(ConverterWordModel);
  261. for (int i = 0; ConverterWordModel.PageIndexLists.Count > i; i++)
  262. {
  263. ConverterWordModel.PageIndexLists[i] = ConverterWordModel.PageIndexLists[i] + 1;
  264. }
  265. result = await ConverterHelper.WordConvert(ConverterWordModel.InputPath, ConverterWordModel.OutputPath, GetProgress, ConverterWordModel.PageIndexLists, ConverterWordModel.Pawssword, ConverterWordModel.wordOptions);
  266. }
  267. else { App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed; }
  268. break;
  269. default:
  270. break;
  271. }
  272. if (!result)
  273. {
  274. AlertsMessage alertsMessage = new AlertsMessage();
  275. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Convert_FailedWarning"), App.ServiceLoader.GetString("Text_ok"));
  276. }
  277. App.mainWindowViewModel.ProcessCloseBtnVisible = System.Windows.Visibility.Visible;
  278. App.mainWindowViewModel.IsProcessVisible = System.Windows.Visibility.Collapsed;
  279. }
  280. }
  281. }