ConverterRTFDialogViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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.Controls;
  19. using System.Windows.Forms;
  20. using DialogResult = Prism.Services.Dialogs.DialogResult;
  21. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  22. {
  23. public class ConverterRTFDialogViewModel : 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_PDFToRTFTitle");
  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 ConverterRTFDialogModel ConverterRTFModel = new ConverterRTFDialogModel();
  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. ConverterRTFModel.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. #endregion
  136. public ConverterRTFDialogViewModel(IDialogService dialogService)
  137. {
  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. IntString();
  148. }
  149. #region 逻辑函数
  150. public void SetCustomPageRange()
  151. {
  152. if (PageRangeSelectIndex == "4")
  153. {
  154. List<int> PageIndexLists = new List<int>();
  155. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  156. { //TODO
  157. //AlertsMessage alertsMessage = new AlertsMessage();
  158. //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  159. //if (alertsMessage.result == ContentResult.Ok)
  160. //{
  161. // return;
  162. //}
  163. //else
  164. //{
  165. // //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  166. // //{
  167. // // Unicode = Unicode,
  168. // // Status = EnumDelete.StatusCreate
  169. // //});
  170. // return;
  171. //}
  172. }
  173. }
  174. }
  175. private void cancel()
  176. {
  177. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  178. }
  179. private void PreviewCancelDown(object e)
  180. {
  181. var PageRangeComboBox = e as WritableComboBox;
  182. if (PageRangeComboBox != null)
  183. {
  184. PageRangeComboBox.IsloseFocus = false;
  185. }
  186. }
  187. private void converter()
  188. {
  189. if (PageRangeSelectIndex == "4")
  190. {
  191. List<int> PageIndexLists = new List<int>();
  192. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  193. { //TODO
  194. AlertsMessage alertsMessage = new AlertsMessage();
  195. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  196. if (alertsMessage.result == ContentResult.Ok)
  197. {
  198. return;
  199. }
  200. else
  201. {
  202. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  203. //{
  204. // Unicode = Unicode,
  205. // Status = EnumDelete.StatusCreate
  206. //});
  207. return;
  208. }
  209. }
  210. }
  211. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  212. /*
  213. *设置这个对话框的起始保存路径
  214. */
  215. sfd.InitialDirectory = currentViewer.Document.FilePath;
  216. /*
  217. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  218. */
  219. sfd.Filter = "RTF|*.rtf;";
  220. /*
  221. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  222. */
  223. sfd.FileName = currentViewer.Document.FileName + ".rtf";
  224. /*
  225. * 做一些工作
  226. */
  227. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  228. {
  229. ConverterRTFModel.OutputPath = sfd.FileName;
  230. try { if (File.Exists(ConverterRTFModel.OutputPath)) File.Delete(ConverterRTFModel.OutputPath); }
  231. catch
  232. {
  233. AlertsMessage alertsMessage = new AlertsMessage();
  234. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  235. return;
  236. }
  237. }
  238. else
  239. {
  240. return;
  241. }
  242. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterRTFModel.PageRange, PageRangeText, true, CurrentPageIndex);
  243. if (ConverterRTFModel.PageRange == "")
  244. {
  245. Trace.WriteLine("输入不对");
  246. AlertsMessage alertsMessage = new AlertsMessage();
  247. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  248. return;
  249. }
  250. char[] enumerationSeparator = new char[] { ',' };
  251. char[] rangeSeparator = new char[] { '-' };
  252. if (!CommonHelper.GetPagesInRange(ref ConverterRTFModel.PageIndexLists, ConverterRTFModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  253. { //TODO
  254. Trace.WriteLine("输入不对");
  255. AlertsMessage alertsMessage = new AlertsMessage();
  256. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  257. return;
  258. }
  259. ConverterRTFModel.rTFOptions = ConverterRTFModel.RTFOptions();
  260. //DialogParameters value = new DialogParameters();
  261. //value.Add(ParameterNames.ConverterType, "RTF");
  262. //value.Add(ParameterNames.ConverterTypeModel, ConverterRTFModel);
  263. var dialogresult = new DialogResult(ButtonResult.OK);
  264. dialogresult.Parameters.Add(ParameterNames.ConverterType, "RTF");
  265. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterRTFModel);
  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", 3);
  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. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChinese;
  375. break;
  376. case 1:
  377. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChineseTraditional;
  378. break;
  379. case 2:
  380. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageEnglish;
  381. break;
  382. case 3:
  383. ConverterRTFModel.Options.OCRLanguage = COCRLanguage.COCRLanguageJapanese;
  384. break;
  385. case 4:
  386. ConverterRTFModel.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) { ConverterRTFModel.Pawssword = currentViewer.Tag.ToString(); }
  410. ConverterRTFModel.InputPath = pdfViewer.Document.FilePath;
  411. FileInfo fileinfo = new FileInfo(ConverterRTFModel.InputPath);
  412. ConverterRTFModel.OutputPath = fileinfo.DirectoryName;
  413. if (Settings.Default.UserDate.subscribestatus != 1)
  414. {
  415. LimitationsConvertVisible = Visibility.Visible;
  416. }
  417. }
  418. }
  419. #endregion
  420. }
  421. }