ConverterPPTDialogViewModel.cs 21 KB

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