ConverterExcelDialogViewModel.cs 9.6 KB

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