ConverterHTMLDialogViewModel.cs 20 KB

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