ConverterWordDialogViewModel.cs 20 KB

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