ConverterTextDialogViewModel.cs 19 KB

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