ConverterWordDialogViewModel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.Helper;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.Dialog.ConverterDialogs;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Windows.Forms;
  15. using DialogResult = Prism.Services.Dialogs.DialogResult;
  16. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  17. {
  18. public class ConverterWordDialogViewModel : BindableBase, IDialogAware
  19. {
  20. public string Title => "";
  21. public event Action<IDialogResult> RequestClose;
  22. #region 参数和属性
  23. public ConverterWordDialogModel ConverterWordModel = new ConverterWordDialogModel();
  24. public CPDFViewer currentViewer;
  25. public IDialogService dialogs;
  26. public string CurrentPageIndex = "1";
  27. public string PageRangeText { set; get; } = "0";
  28. public string PageRangeSelectIndex { set; get; } = "0";
  29. #endregion
  30. #region 委托声明
  31. public DelegateCommand<string> RadioButtonCommand { get; set; }
  32. public DelegateCommand CancelCommand { get; set; }
  33. public DelegateCommand ConverterCommnad { get; set; }
  34. public DelegateCommand<object> CmbPageSelectionChanged { get; set; }
  35. public DelegateCommand<object> CmbPageTextChanged { get; set; }
  36. #endregion
  37. public ConverterWordDialogViewModel(IDialogService dialogService)
  38. {
  39. CancelCommand = new DelegateCommand(cancel);
  40. ConverterCommnad = new DelegateCommand(converter);
  41. RadioButtonCommand = new DelegateCommand<string>(radiobutton);
  42. CmbPageSelectionChanged = new DelegateCommand<object>(CmbPageSelectionChangedEvent);
  43. CmbPageTextChanged = new DelegateCommand<object>(CmbPageTextChangedEvent);
  44. dialogs = dialogService;
  45. }
  46. #region 逻辑函数
  47. private void cancel()
  48. {
  49. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  50. }
  51. private void converter()
  52. {
  53. FolderBrowserDialog dlg = new FolderBrowserDialog();
  54. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  55. {
  56. ConverterWordModel.OutputPath = dlg.SelectedPath.Trim();
  57. }
  58. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterWordModel.PageRange, PageRangeText, true, CurrentPageIndex);
  59. char[] enumerationSeparator = new char[] { ',' };
  60. char[] rangeSeparator = new char[] { '-' };
  61. if (!CommonHelper.GetPagesInRange(ref ConverterWordModel.PageIndexLists, ConverterWordModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  62. { //TODO
  63. MessageBoxEx.Show("输入不对");
  64. return;
  65. }
  66. DialogParameters value = new DialogParameters();
  67. value.Add(ParameterNames.ConverterType, "Word");
  68. value.Add(ParameterNames.ConverterTypeModel, ConverterWordModel);
  69. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  70. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  71. {
  72. });
  73. }
  74. private void CmbPageSelectionChangedEvent(object e)
  75. {
  76. ///这里采用的是将预览UI控件传递过来的方式,为下下策
  77. ///正确的方式应该是 通过声明一些属性,再通过绑定来更新预览控件对应值的形式
  78. ///但是目前发现自定义控件的依赖属性绑定有些问题,因此先用此方法,将业务逻辑代码先调整到VM里
  79. var ConverterPreview = e as PageTurningPreview;
  80. if (ConverterPreview != null)
  81. {
  82. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  83. var currentViewer = this.currentViewer;
  84. string PageRange = "";
  85. var PageRangeText = this.PageRangeText;
  86. if (PageRangeSelectIndex == "0" || PageRangeSelectIndex == "2" || PageRangeSelectIndex == "3")
  87. {
  88. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  89. char[] enumerationSeparator = new char[] { ',' };
  90. char[] rangeSeparator = new char[] { '-' };
  91. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  92. { //TODO
  93. ConverterPreview.PageIndexLists.Add(0);
  94. Trace.WriteLine("输入不对");
  95. MessageBoxEx.Show("输入不对");
  96. return;
  97. }
  98. }
  99. if (PageRangeSelectIndex == "1")
  100. {
  101. char[] enumerationSeparator = new char[] { ',' };
  102. char[] rangeSeparator = new char[] { '-' };
  103. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex + 1).ToString()
  104. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  105. { //TODO
  106. Trace.WriteLine("输入不对");
  107. MessageBoxEx.Show("输入不对");
  108. return;
  109. }
  110. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  111. }
  112. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  113. ConverterPreview.CurrentIndex = 0;
  114. }
  115. }
  116. private void CmbPageTextChangedEvent(object e)
  117. {
  118. var ConverterPreview = e as PageTurningPreview;
  119. if (ConverterPreview != null)
  120. {
  121. if (ConverterPreview != null)
  122. {
  123. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  124. var currentViewer = this.currentViewer;
  125. string PageRange = "";
  126. var PageRangeText = this.PageRangeText;
  127. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  128. char[] enumerationSeparator = new char[] { ',' };
  129. char[] rangeSeparator = new char[] { '-' };
  130. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  131. { //TODO
  132. ConverterPreview.PageIndexLists.Add(0);
  133. return;
  134. }
  135. else
  136. {
  137. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  138. ConverterPreview.CurrentIndex = 0;
  139. }
  140. }
  141. }
  142. }
  143. private void radiobutton(string e)
  144. {
  145. string radioButton = e;
  146. if (radioButton != null)
  147. {
  148. switch (radioButton)
  149. {
  150. case "FlowingTextRadioBtn":
  151. break;
  152. case "PageLayoutRadioBtn":
  153. break;
  154. default:
  155. break;
  156. }
  157. }
  158. }
  159. #endregion
  160. #region 构架行为
  161. public bool CanCloseDialog()
  162. {
  163. return true;
  164. }
  165. public void OnDialogClosed()
  166. {
  167. }
  168. public void OnDialogOpened(IDialogParameters parameters)
  169. {
  170. CPDFViewer pdfViewer = null;
  171. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  172. if (pdfViewer != null)
  173. {
  174. currentViewer = pdfViewer;
  175. if (currentViewer.Tag != null) { ConverterWordModel.Pawssword = currentViewer.Tag.ToString(); }
  176. ConverterWordModel.InputPath = pdfViewer.Document.FilePath;
  177. FileInfo fileinfo = new FileInfo(ConverterWordModel.InputPath);
  178. ConverterWordModel.OutputPath = fileinfo.DirectoryName;
  179. }
  180. }
  181. #endregion
  182. }
  183. }