ConverterPPTDialogViewModel.cs 23 KB

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