ConverterRTFDialogViewModel.cs 19 KB

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