ConverterWordDialogViewModel.cs 21 KB

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