ConverterTextDialogViewModel.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 static Dropbox.Api.Sharing.ListFileMembersIndividualResult;
  21. using DialogResult = Prism.Services.Dialogs.DialogResult;
  22. namespace PDF_Master.ViewModels.Dialog.ConverterDialogs
  23. {
  24. public class ConverterTextDialogViewModel : 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_PDFToTextTitle");
  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 ConverterTextDialogModel ConverterTextModel = new ConverterTextDialogModel();
  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. ConverterTextModel.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. #endregion
  137. public ConverterTextDialogViewModel(IDialogService dialogService)
  138. {
  139. IntString();
  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. dialogs = dialogService;
  149. }
  150. #region 逻辑函数
  151. public void SetCustomPageRange()
  152. {
  153. if (PageRangeSelectIndex == "4")
  154. {
  155. List<int> PageIndexLists = new List<int>();
  156. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  157. { //TODO
  158. //AlertsMessage alertsMessage = new AlertsMessage();
  159. //alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  160. //if (alertsMessage.result == ContentResult.Ok)
  161. //{
  162. // return;
  163. //}
  164. //else
  165. //{
  166. // //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  167. // //{
  168. // // Unicode = Unicode,
  169. // // Status = EnumDelete.StatusCreate
  170. // //});
  171. // return;
  172. //}
  173. }
  174. }
  175. }
  176. private void cancel()
  177. {
  178. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  179. }
  180. private void PreviewCancelDown(object e)
  181. {
  182. var PageRangeComboBox = e as WritableComboBox;
  183. if (PageRangeComboBox != null)
  184. {
  185. PageRangeComboBox.IsloseFocus = false;
  186. }
  187. }
  188. private void converter()
  189. {
  190. if (PageRangeSelectIndex == "4")
  191. {
  192. List<int> PageIndexLists = new List<int>();
  193. if (!CommonHelper.GetPagesInRange(ref PageIndexLists, PageRangeText, currentViewer.Document.PageCount, new char[] { ',' }, new char[] { '-' }))
  194. { //TODO
  195. AlertsMessage alertsMessage = new AlertsMessage();
  196. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  197. if (alertsMessage.result == ContentResult.Ok)
  198. {
  199. return;
  200. }
  201. else
  202. {
  203. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  204. //{
  205. // Unicode = Unicode,
  206. // Status = EnumDelete.StatusCreate
  207. //});
  208. return;
  209. }
  210. }
  211. }
  212. System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
  213. /*
  214. *设置这个对话框的起始保存路径
  215. */
  216. sfd.InitialDirectory = currentViewer.Document.FilePath;
  217. /*
  218. *设置保存的文件的类型,注意过滤器的语法 例子:“文件类型|*.后缀名;*.后缀名;”
  219. */
  220. sfd.Filter = "Txt|*.txt;";
  221. /*
  222. *调用ShowDialog()方法显示该对话框,该方法的返回值代表用户是否点击了确定按钮
  223. */
  224. sfd.FileName = currentViewer.Document.FileName + ".txt";
  225. /*
  226. * 做一些工作
  227. */
  228. if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  229. {
  230. ConverterTextModel.OutputPath = sfd.FileName;
  231. try { if (File.Exists(ConverterTextModel.OutputPath)) File.Delete(ConverterTextModel.OutputPath); }
  232. catch
  233. {
  234. AlertsMessage alertsMessage = new AlertsMessage();
  235. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("FileNotExistWarning"), App.ServiceLoader.GetString("Text_ok"));
  236. return;
  237. }
  238. }
  239. else
  240. {
  241. return;
  242. }
  243. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterTextModel.PageRange, PageRangeText,true, CurrentPageIndex);
  244. if (ConverterTextModel.PageRange == "")
  245. {
  246. Trace.WriteLine("输入不对");
  247. AlertsMessage alertsMessage = new AlertsMessage();
  248. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  249. return;
  250. }
  251. char[] enumerationSeparator = new char[] { ',' };
  252. char[] rangeSeparator = new char[] { '-' };
  253. if (!CommonHelper.GetPagesInRange(ref ConverterTextModel.PageIndexLists, ConverterTextModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  254. { //TODO
  255. Trace.WriteLine("输入不对");
  256. AlertsMessage alertsMessage = new AlertsMessage();
  257. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  258. return;
  259. }
  260. ConverterTextModel.txtOptions = ConverterTextModel.TxtOptions();
  261. //DialogParameters value = new DialogParameters();
  262. //value.Add(ParameterNames.ConverterType, "Text");
  263. //value.Add(ParameterNames.ConverterTypeModel, ConverterTextModel);
  264. var dialogresult = new DialogResult(ButtonResult.OK);
  265. dialogresult.Parameters.Add(ParameterNames.ConverterType, "Text");
  266. dialogresult.Parameters.Add(ParameterNames.ConverterTypeModel, ConverterTextModel);
  267. RequestClose.Invoke(dialogresult);
  268. //RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  269. //dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  270. //{
  271. //});
  272. }
  273. private void CmbPageSelectionChangedEvent(object e)
  274. {
  275. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  276. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  277. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  278. var ConverterPreview = e as PageTurningPreview;
  279. if (ConverterPreview != null)
  280. {
  281. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  282. var currentViewer = this.currentViewer;
  283. string PageRange = "";
  284. var PageRangeText = this.PageRangeText;
  285. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3" || PageRangeSelectIndex == "4")
  286. {
  287. if (PageRangeSelectIndex == "4") { HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, true); } else { HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true); }
  288. char[] enumerationSeparator = new char[] { ',' };
  289. char[] rangeSeparator = new char[] { '-' };
  290. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  291. { //TODO
  292. ConverterPreview.PageIndexLists.Add(0);
  293. Trace.WriteLine("输入不对");
  294. AlertsMessage alertsMessage = new AlertsMessage();
  295. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  296. return;
  297. }
  298. }
  299. if (PageRangeSelectIndex == "1")
  300. {
  301. char[] enumerationSeparator = new char[] { ',' };
  302. char[] rangeSeparator = new char[] { '-' };
  303. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  304. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  305. { //TODO
  306. Trace.WriteLine("输入不对");
  307. AlertsMessage alertsMessage = new AlertsMessage();
  308. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  309. return;
  310. }
  311. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  312. }
  313. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  314. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  315. ConverterPreview.CurrentIndex = 0;
  316. }
  317. }
  318. private void CmbPageTextChangedEvent(object e)
  319. {
  320. var ConverterPreview = e as PageTurningPreview;
  321. if (ConverterPreview != null)
  322. {
  323. if (ConverterPreview != null)
  324. {
  325. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  326. var currentViewer = this.currentViewer;
  327. string PageRange = "";
  328. var PageRangeText = this.PageRangeText;
  329. 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. HomePageEditHelper.GetPagerange("0", currentViewer, ref PageRange, PageRangeText, true);
  335. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  336. { //TODO
  337. ConverterPreview.PageIndexLists.Add(0);
  338. Trace.WriteLine("输入不对");
  339. AlertsMessage alertsMessage = new AlertsMessage();
  340. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("PageRangeWarning"), App.ServiceLoader.GetString("Text_ok"));
  341. return;
  342. }
  343. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  344. ConverterPreview.CurrentIndex = 0;
  345. return;
  346. }
  347. else
  348. {
  349. // ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  350. ConverterPreview.PageIndex.Text = ConverterPreview.PageIndexLists.Count.ToString();
  351. ConverterPreview.CurrentIndex = 0;
  352. }
  353. }
  354. }
  355. }
  356. private void BatchConverter()
  357. {
  358. DialogParameters convertpdftoword = new DialogParameters();
  359. convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
  360. convertpdftoword.Add("ConverterTypeIndex", 6);
  361. HomePageBatchProcessingDialogModel.FilePaths = new List<string> { currentViewer.Document.FilePath.ToString() };
  362. HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
  363. convertpdftoword.Add(ParameterNames.FilePath, new string[] { currentViewer.Document.FilePath.ToString() });
  364. dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
  365. }
  366. private void UnlockMouseDown()
  367. {
  368. ConverterHelper.convertUnlock();
  369. }
  370. private void SelectLanguage(int SelectedIndex)
  371. {
  372. switch (SelectedIndex)
  373. {
  374. case 0:
  375. ConverterTextModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChinese;
  376. break;
  377. case 1:
  378. ConverterTextModel.Options.OCRLanguage = COCRLanguage.COCRLanguageChineseTraditional;
  379. break;
  380. case 2:
  381. ConverterTextModel.Options.OCRLanguage = COCRLanguage.COCRLanguageEnglish;
  382. break;
  383. case 3:
  384. ConverterTextModel.Options.OCRLanguage = COCRLanguage.COCRLanguageJapanese;
  385. break;
  386. case 4:
  387. ConverterTextModel.Options.OCRLanguage = COCRLanguage.COCRLanguageKorean;
  388. break;
  389. default:
  390. break;
  391. }
  392. }
  393. #endregion
  394. #region 构架行为
  395. public bool CanCloseDialog()
  396. {
  397. return true;
  398. }
  399. public void OnDialogClosed()
  400. {
  401. }
  402. public void OnDialogOpened(IDialogParameters parameters)
  403. {
  404. CPDFViewer pdfViewer = null;
  405. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  406. if (pdfViewer != null)
  407. {
  408. currentViewer = pdfViewer;
  409. MaxPageRange = currentViewer.Document.PageCount;
  410. if (currentViewer.Tag != null) { ConverterTextModel.Pawssword = currentViewer.Tag.ToString(); }
  411. ConverterTextModel.InputPath = pdfViewer.Document.FilePath;
  412. FileInfo fileinfo = new FileInfo(ConverterTextModel.InputPath);
  413. ConverterTextModel.OutputPath=fileinfo.DirectoryName;
  414. if (Settings.Default.UserDate.subscribestatus != 1)
  415. {
  416. LimitationsConvertVisible = Visibility.Visible;
  417. }
  418. }
  419. }
  420. #endregion
  421. }
  422. }