ConverterHTMLDialogViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using ComDocumentAIKit;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Master.CustomControl;
  4. using PDF_Master.Helper;
  5. using PDF_Master.Model;
  6. using PDF_Master.Model.Dialog.ConverterDialogs;
  7. using PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Services.Dialogs;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Windows.Forms;
  17. using DialogResult = Prism.Services.Dialogs.DialogResult;
  18. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  19. {
  20. public class ConverterHTMLDialogViewModel : BindableBase, IDialogAware
  21. {
  22. #region 文案
  23. private string T_title;
  24. public string T_Title
  25. {
  26. get { return T_title; }
  27. set
  28. {
  29. SetProperty(ref T_title, value);
  30. }
  31. }
  32. private string T_yes;
  33. public string T_YES
  34. {
  35. get { return T_yes; }
  36. set
  37. {
  38. SetProperty(ref T_yes, value);
  39. }
  40. }
  41. private string T_no;
  42. public string T_No
  43. {
  44. get { return T_no; }
  45. set
  46. {
  47. SetProperty(ref T_no, value);
  48. }
  49. }
  50. private void IntString()
  51. {
  52. T_Title = App.MainPageLoader.GetString("Convert_PDFToHTMLTitle");
  53. T_YES = App.MainPageLoader.GetString("Convert_Yes");
  54. T_No = App.MainPageLoader.GetString("Convert_No");
  55. }
  56. #endregion
  57. public string Title => "";
  58. public event Action<IDialogResult> RequestClose;
  59. #region 参数和属性
  60. public ConverterHTMLDialogModel ConverterHTMLModel = new ConverterHTMLDialogModel();
  61. public CPDFViewer currentViewer;
  62. public IDialogService dialogs;
  63. public string CurrentPageIndex = "1";
  64. public bool IsCurrentPageIndex = true;
  65. public string PageRangeText { set; get; } = "0";
  66. public string PageRangeSelectIndex { set; get; } = "0";
  67. private int maxPageRange = 0;
  68. public int MaxPageRange
  69. {
  70. get { return maxPageRange; }
  71. set
  72. {
  73. SetProperty(ref maxPageRange, value);
  74. }
  75. }
  76. private bool oCRCheckBoxIsCheckBox = true;
  77. public bool OCRCheckBoxIsCheckBox
  78. {
  79. get { return oCRCheckBoxIsCheckBox; }
  80. set
  81. {
  82. SetProperty(ref oCRCheckBoxIsCheckBox, value);
  83. ConverterHTMLModel.Options.IsAllowOCR = oCRCheckBoxIsCheckBox;
  84. }
  85. }
  86. private int languageSelectedIndex = 2;
  87. public int LanguageSelectedIndex
  88. {
  89. get { return languageSelectedIndex; }
  90. set
  91. {
  92. SetProperty(ref languageSelectedIndex, value);
  93. SelectLanguage(languageSelectedIndex);
  94. }
  95. }
  96. #endregion
  97. #region 委托声明
  98. public DelegateCommand CancelCommand { get; set; }
  99. public DelegateCommand ConverterCommnad { get; set; }
  100. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  101. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  102. public DelegateCommand SetCustomPageRangeCommand { get; set; }
  103. public DelegateCommand BatchConverterCommand { get; set; }
  104. #endregion
  105. public ConverterHTMLDialogViewModel(IDialogService dialogService)
  106. {
  107. IntString();
  108. CancelCommand = new DelegateCommand(cancel);
  109. ConverterCommnad = new DelegateCommand(converter);
  110. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  111. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  112. SetCustomPageRangeCommand = new DelegateCommand(SetCustomPageRange);
  113. BatchConverterCommand = new DelegateCommand(BatchConverter);
  114. dialogs = dialogService;
  115. }
  116. #region 逻辑函数
  117. public void SetCustomPageRange()
  118. {
  119. if (PageRangeSelectIndex == "4")
  120. {
  121. List<int> PageIndexLists = new List<int>();
  122. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  123. { //TODO
  124. AlertsMessage alertsMessage = new AlertsMessage();
  125. alertsMessage.ShowDialog("页面范围输入错误", "", "确定");
  126. if (alertsMessage.result == ContentResult.Ok)
  127. {
  128. return;
  129. }
  130. else
  131. {
  132. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  133. //{
  134. // Unicode = Unicode,
  135. // Status = EnumDelete.StatusCreate
  136. //});
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. private void cancel()
  143. {
  144. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  145. }
  146. private void converter()
  147. {
  148. if (PageRangeSelectIndex == "4")
  149. {
  150. List<int> PageIndexLists = new List<int>();
  151. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  152. { //TODO
  153. AlertsMessage alertsMessage = new AlertsMessage();
  154. alertsMessage.ShowDialog("页面范围输入错误", "", "确定");
  155. if (alertsMessage.result == ContentResult.Ok)
  156. {
  157. return;
  158. }
  159. else
  160. {
  161. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  162. //{
  163. // Unicode = Unicode,
  164. // Status = EnumDelete.StatusCreate
  165. //});
  166. return;
  167. }
  168. }
  169. }
  170. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  171. /*
  172. *设置这个对话框的起始保存路径
  173. */
  174. sfd.InitialDirectory = currentViewer.Document.FilePath;
  175. /*
  176. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  177. */
  178. sfd.Filter = "PDF|*.html;";
  179. /*
  180. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  181. */
  182. sfd.FileName = currentViewer.Document.FileName + ".html";
  183. /*
  184. * 做一些工作
  185. */
  186. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  187. {
  188. ConverterHTMLModel.OutputPath = sfd.FileName;
  189. try { if (File.Exists(ConverterHTMLModel.OutputPath)) File.Delete(ConverterHTMLModel.OutputPath); }
  190. catch
  191. {
  192. AlertsMessage alertsMessage = new AlertsMessage();
  193. alertsMessage.ShowDialog("文件已被占用", "请重新选则目标路径", "确定");
  194. return;
  195. }
  196. }
  197. else
  198. {
  199. return;
  200. }
  201. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterHTMLModel.PageRange, PageRangeText, true, CurrentPageIndex);
  202. if (ConverterHTMLModel.PageRange == "")
  203. {
  204. Trace.WriteLine("输入不对");
  205. MessageBoxEx.Show("输入不对");
  206. return;
  207. }
  208. char[] enumerationSeparator = new char[] { ',' };
  209. char[] rangeSeparator = new char[] { '-' };
  210. if (!CommonHelper.GetPagesInRange(ref ConverterHTMLModel.PageIndexLists, ConverterHTMLModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  211. { //TODO
  212. Trace.WriteLine("输入不对");
  213. MessageBoxEx.Show("输入不对");
  214. return;
  215. }
  216. ConverterHTMLModel.htmlOptions=ConverterHTMLModel.HTMLOptions();
  217. //result = await ConverterHelper.TxtConvert(ConverterTextModel.InputPath, ConverterTextModel.OutputPath, ConverterTextModel.GetProgress, ConverterTextModel.PageIndexLists, ConverterTextModel.Pawssword);
  218. //DialogParameters value = new DialogParameters();
  219. //value.Add(ParameterNames.ConverterType, "HTML");
  220. //value.Add(ParameterNames.ConverterTypeModel, ConverterHTMLModel);
  221. var dialogresult = new DialogResult(ButtonResult.OK);
  222. dialogresult.Parameters.Add(ParameterNames.ConverterType, "HTML");
  223. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterHTMLModel);
  224. RequestClose.Invoke(dialogresult);
  225. //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  226. //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  227. //{
  228. //});
  229. }
  230. private void CmbPageSelectionChangedEvent(object e)
  231. {
  232. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  233. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  234. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  235. var ConverterPreview = e as PageTurningPreview;
  236. if (ConverterPreview != null)
  237. {
  238. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  239. var currentViewer = this.currentViewer;
  240. string PageRange = "";
  241. var PageRangeText = this.PageRangeText;
  242. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  243. {
  244. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  245. char[] enumerationSeparator = new char[] { ',' };
  246. char[] rangeSeparator = new char[] { '-' };
  247. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  248. { //TODO
  249. ConverterPreview.PageIndexLists.Add(0);
  250. Trace.WriteLine("输入不对");
  251. MessageBoxEx.Show("输入不对");
  252. return;
  253. }
  254. }
  255. if (PageRangeSelectIndex == "1")
  256. {
  257. char[] enumerationSeparator = new char[] { ',' };
  258. char[] rangeSeparator = new char[] { '-' };
  259. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  260. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  261. { //TODO
  262. Trace.WriteLine("输入不对");
  263. MessageBoxEx.Show("输入不对");
  264. return;
  265. }
  266. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  267. }
  268. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  269. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  270. ConverterPreview.CurrentIndex = 0;
  271. }
  272. }
  273. private void CmbPageTextChangedEvent(object e)
  274. {
  275. var ConverterPreview = e as PageTurningPreview;
  276. if (ConverterPreview != null)
  277. {
  278. if (ConverterPreview != null)
  279. {
  280. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  281. var currentViewer = this.currentViewer;
  282. string PageRange = "";
  283. var PageRangeText = this.PageRangeText;
  284. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  285. char[] enumerationSeparator = new char[] { ',' };
  286. char[] rangeSeparator = new char[] { '-' };
  287. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  288. { //TODO
  289. ConverterPreview.PageIndexLists.Add(0);
  290. return;
  291. }
  292. else
  293. {
  294. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  295. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  296. ConverterPreview.CurrentIndex = 0;
  297. }
  298. }
  299. }
  300. }
  301. private void BatchConverter()
  302. {
  303. DialogParameters convertpdftoword = new DialogParameters();
  304. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  305. convertpdftoword.Add("ConverterTypeIndex", 5);
  306. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
  307. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  308. convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
  309. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  310. }
  311. private void SelectLanguage(int SelectedIndex)
  312. {
  313. switch (SelectedIndex)
  314. {
  315. case 0:
  316. ConverterHTMLModel.Options.OCRLanguage = COCRLanguage.Chinese;
  317. break;
  318. case 1:
  319. ConverterHTMLModel.Options.OCRLanguage = COCRLanguage.ChineseTraditional;
  320. break;
  321. case 2:
  322. ConverterHTMLModel.Options.OCRLanguage = COCRLanguage.English;
  323. break;
  324. case 3:
  325. ConverterHTMLModel.Options.OCRLanguage = COCRLanguage.Japanese;
  326. break;
  327. case 4:
  328. ConverterHTMLModel.Options.OCRLanguage = COCRLanguage.Korean;
  329. break;
  330. default:
  331. break;
  332. }
  333. }
  334. #endregion
  335. #region 构架行为
  336. public bool CanCloseDialog()
  337. {
  338. return true;
  339. }
  340. public void OnDialogClosed()
  341. {
  342. }
  343. public void OnDialogOpened(IDialogParameters parameters)
  344. {
  345. CPDFViewer pdfViewer = null;
  346. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  347. if (pdfViewer != null)
  348. {
  349. currentViewer = pdfViewer;
  350. MaxPageRange = currentViewer.Document.PageCount;
  351. if (currentViewer.Tag != null) { ConverterHTMLModel.Pawssword = currentViewer.Tag.ToString(); }
  352. ConverterHTMLModel.InputPath = pdfViewer.Document.FilePath;
  353. FileInfo fileinfo = new FileInfo(ConverterHTMLModel.InputPath);
  354. ConverterHTMLModel.OutputPath = fileinfo.DirectoryName;
  355. }
  356. }
  357. #endregion
  358. }
  359. }