ConverterHTMLDialogViewModel.cs 22 KB

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