ConverterRTFDialogViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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 DialogResult = Prism.Services.Dialogs.DialogResult;
  22. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  23. {
  24. public class ConverterRTFDialogViewModel : BindableBase, IDialogAware
  25. {
  26. #region 文案
  27. private string T_title;
  28. public string T_Title
  29. {
  30. get { return T_title; }
  31. set
  32. {
  33. SetProperty(ref T_title, value);
  34. }
  35. }
  36. private string T_yes;
  37. public string T_YES
  38. {
  39. get { return T_yes; }
  40. set
  41. {
  42. SetProperty(ref T_yes, value);
  43. }
  44. }
  45. private string T_no;
  46. public string T_No
  47. {
  48. get { return T_no; }
  49. set
  50. {
  51. SetProperty(ref T_no, value);
  52. }
  53. }
  54. private string T_pageRange;
  55. public string T_PageRange
  56. {
  57. get { return T_pageRange; }
  58. set
  59. {
  60. SetProperty(ref T_pageRange, value);
  61. }
  62. }
  63. private string _TextUpgrade;
  64. public string TextUpgrade
  65. {
  66. get { return _TextUpgrade; }
  67. set { _TextUpgrade = value; }
  68. }
  69. private void IntString()
  70. {
  71. TextUpgrade = App.ServiceLoader.GetString("TextUpgrade");
  72. T_Title = App.MainPageLoader.GetString("Convert_PDFToRTFTitle");
  73. T_YES = App.MainPageLoader.GetString("Convert_Yes");
  74. T_No = App.MainPageLoader.GetString("Convert_No");
  75. T_PageRange = App.MainPageLoader.GetString("Convert_PDFPageRange");
  76. }
  77. #endregion
  78. public string Title => "";
  79. public event Action<IDialogResult> RequestClose;
  80. #region 参数和属性
  81. public ConverterRTFDialogModel ConverterRTFModel = new ConverterRTFDialogModel();
  82. public CPDFViewer currentViewer;
  83. public IDialogService dialogs;
  84. public string CurrentPageIndex = "1";
  85. public bool IsCurrentPageIndex = true;
  86. public string PageRangeText { set; get; } = "0";
  87. public string PageRangeSelectIndex { set; get; } = "0";
  88. private int maxPageRange = 0;
  89. public int MaxPageRange
  90. {
  91. get { return maxPageRange; }
  92. set
  93. {
  94. SetProperty(ref maxPageRange, value);
  95. }
  96. }
  97. private bool oCRCheckBoxIsCheckBox = false;
  98. public bool OCRCheckBoxIsCheckBox
  99. {
  100. get { return oCRCheckBoxIsCheckBox; }
  101. set
  102. {
  103. SetProperty(ref oCRCheckBoxIsCheckBox, value);
  104. ConverterRTFModel.Options.IsAllowOCR = oCRCheckBoxIsCheckBox;
  105. }
  106. }
  107. private int languageSelectedIndex = 2;
  108. public int LanguageSelectedIndex
  109. {
  110. get { return languageSelectedIndex; }
  111. set
  112. {
  113. SetProperty(ref languageSelectedIndex, value);
  114. SelectLanguage(languageSelectedIndex);
  115. }
  116. }
  117. private Visibility limitationsConvertVisible = Visibility.Hidden;
  118. public Visibility LimitationsConvertVisible
  119. {
  120. get { return limitationsConvertVisible; }
  121. set
  122. {
  123. SetProperty(ref limitationsConvertVisible, value);
  124. }
  125. }
  126. #endregion
  127. #region 委托声明
  128. public DelegateCommand CancelCommand { get; set; }
  129. public DelegateCommand ConverterCommnad { get; set; }
  130. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  131. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  132. public DelegateCommand SetCustomPageRangeCommand { get; set; }
  133. public DelegateCommand<object> PreviewCancelDownCommand { get; set; }
  134. public DelegateCommand BatchConverterCommand { get; set; }
  135. public DelegateCommand UnlockMouseDownCommand { get; set; }
  136. public DelegateCommand OpenOCRCommand { get; set; }
  137. #endregion
  138. public ConverterRTFDialogViewModel(IDialogService dialogService)
  139. {
  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. IntString();
  151. }
  152. #region 逻辑函数
  153. private void openOCR()
  154. {
  155. if (OCRCheckBoxIsCheckBox == true)
  156. {
  157. bool folderMD5 = FileComparisonHelper.OCRModelItExist();
  158. if (folderMD5)
  159. {
  160. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  161. CPDFConverter.InitOcrLibrary(Path.Combine(folderPath, "x64"));
  162. CPDFConverter.SetOCRModelPath(Path.Combine(folderPath, "source", "models"));
  163. }
  164. else
  165. {
  166. AlertsMessage alertsMessage = new AlertsMessage();
  167. alertsMessage.ShowDialog("想用OCR,就去下载吧!", "", "取消", "确定");
  168. if (alertsMessage.result == ContentResult.Ok)
  169. {
  170. FileComparisonHelper.RemoveOCRModel();
  171. DialogParameters oCRDownloadProgress = new DialogParameters();
  172. dialogs.ShowDialog(DialogNames.OCRDownloadProgress, oCRDownloadProgress, e =>
  173. {
  174. if (e.Result == Prism.Services.Dialogs.ButtonResult.OK)
  175. {
  176. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  177. CPDFConverter.InitOcrLibrary(Path.Combine(folderPath, "x64"));
  178. CPDFConverter.SetOCRModelPath(Path.Combine(folderPath, "source", "models"));
  179. }
  180. else
  181. {
  182. OCRCheckBoxIsCheckBox = false;
  183. }
  184. });
  185. }
  186. else
  187. {
  188. OCRCheckBoxIsCheckBox = false;
  189. }
  190. }
  191. }
  192. }
  193. public void SetCustomPageRange()
  194. {
  195. if (PageRangeSelectIndex == "4")
  196. {
  197. List<int> PageIndexLists = new List<int>();
  198. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  199. { //TODO
  200. //AlertsMessage alertsMessage = new AlertsMessage();
  201. //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  202. //if (alertsMessage.result == ContentResult.Ok)
  203. //{
  204. // return;
  205. //}
  206. //else
  207. //{
  208. // //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  209. // //{
  210. // // Unicode = Unicode,
  211. // // Status = EnumDelete.StatusCreate
  212. // //});
  213. // return;
  214. //}
  215. }
  216. }
  217. }
  218. private void cancel()
  219. {
  220. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  221. }
  222. private void PreviewCancelDown(object e)
  223. {
  224. var PageRangeComboBox = e as WritableComboBox;
  225. if (PageRangeComboBox != null)
  226. {
  227. PageRangeComboBox.IsloseFocus = false;
  228. }
  229. }
  230. private void converter()
  231. {
  232. if (PageRangeSelectIndex == "4")
  233. {
  234. List<int> PageIndexLists = new List<int>();
  235. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  236. { //TODO
  237. AlertsMessage alertsMessage = new AlertsMessage();
  238. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  239. if (alertsMessage.result == ContentResult.Ok)
  240. {
  241. return;
  242. }
  243. else
  244. {
  245. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  246. //{
  247. // Unicode = Unicode,
  248. // Status = EnumDelete.StatusCreate
  249. //});
  250. return;
  251. }
  252. }
  253. }
  254. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  255. /*
  256. *设置这个对话框的起始保存路径
  257. */
  258. sfd.InitialDirectory = currentViewer.Document.FilePath;
  259. /*
  260. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  261. */
  262. sfd.Filter = "RTF|*.rtf;";
  263. /*
  264. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  265. */
  266. sfd.FileName = currentViewer.Document.FileName + ".rtf";
  267. /*
  268. * 做一些工作
  269. */
  270. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  271. {
  272. ConverterRTFModel.OutputPath = sfd.FileName;
  273. try { if (File.Exists(ConverterRTFModel.OutputPath)) File.Delete(ConverterRTFModel.OutputPath); }
  274. catch
  275. {
  276. AlertsMessage alertsMessage = new AlertsMessage();
  277. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  278. return;
  279. }
  280. }
  281. else
  282. {
  283. return;
  284. }
  285. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterRTFModel.PageRange, PageRangeText, true, CurrentPageIndex);
  286. if (ConverterRTFModel.PageRange == "")
  287. {
  288. Trace.WriteLine("输入不对");
  289. AlertsMessage alertsMessage = new AlertsMessage();
  290. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  291. return;
  292. }
  293. char[] enumerationSeparator = new char[] { ',' };
  294. char[] rangeSeparator = new char[] { '-' };
  295. if (!CommonHelper.GetPagesInRange(ref ConverterRTFModel.PageIndexLists, ConverterRTFModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  296. { //TODO
  297. Trace.WriteLine("输入不对");
  298. AlertsMessage alertsMessage = new AlertsMessage();
  299. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  300. return;
  301. }
  302. ConverterRTFModel.rTFOptions = ConverterRTFModel.RTFOptions();
  303. //DialogParameters value = new DialogParameters();
  304. //value.Add(ParameterNames.ConverterType, "RTF");
  305. //value.Add(ParameterNames.ConverterTypeModel, ConverterRTFModel);
  306. var dialogresult = new DialogResult(ButtonResult.OK);
  307. dialogresult.Parameters.Add(ParameterNames.ConverterType, "RTF");
  308. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterRTFModel);
  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", 3);
  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. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChinese;
  418. break;
  419. case 1:
  420. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChineseTraditional;
  421. break;
  422. case 2:
  423. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageEnglish;
  424. break;
  425. case 3:
  426. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageJapanese;
  427. break;
  428. case 4:
  429. ConverterRTFModel.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) { ConverterRTFModel.Pawssword = currentViewer.Tag.ToString(); }
  453. ConverterRTFModel.InputPath = pdfViewer.Document.FilePath;
  454. FileInfo fileinfo = new FileInfo(ConverterRTFModel.InputPath);
  455. ConverterRTFModel.OutputPath = fileinfo.DirectoryName;
  456. if (Settings.Default.UserDate.subscribestatus != 1)
  457. {
  458. LimitationsConvertVisible = Visibility.Visible;
  459. }
  460. }
  461. }
  462. #endregion
  463. }
  464. }