ConverterWordDialogViewModel.cs 22 KB

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