ConverterCSVDialogViewModel.cs 8.8 KB

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