ConverterDialogsModel.cs 16 KB

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