ConverterHTMLDialogViewModel.cs 19 KB

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