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