ConverterTextDialogViewModel.cs 22 KB

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