ConverterPPTDialogViewModel.cs 21 KB

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