ConverterCSVDialogViewModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. ConverterPreview.PageIndexLists.Add(0);
  74. Trace.WriteLine("输入不对");
  75. MessageBoxEx.Show("输入不对");
  76. return;
  77. }
  78. }
  79. if (PageRangeSelectIndex == "1")
  80. {
  81. char[] enumerationSeparator = new char[] { ',' };
  82. char[] rangeSeparator = new char[] { '-' };
  83. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, (currentViewer.CurrentIndex+1).ToString()
  84. , currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  85. { //TODO
  86. Trace.WriteLine("输入不对");
  87. MessageBoxEx.Show("输入不对");
  88. return;
  89. }
  90. this.CurrentPageIndex = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  91. }
  92. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Count).ToString();
  93. ConverterPreview.CurrentIndex = 0;
  94. }
  95. }
  96. private void CmbPageTextChangedEvent(object e)
  97. {
  98. var ConverterPreview = e as PageTurningPreview;
  99. if (ConverterPreview != null)
  100. {
  101. if (ConverterPreview != null)
  102. {
  103. string PageRangeSelectIndex = this.PageRangeSelectIndex;
  104. var currentViewer = this.currentViewer;
  105. string PageRange = "";
  106. var PageRangeText = this.PageRangeText;
  107. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref PageRange, PageRangeText, true);
  108. char[] enumerationSeparator = new char[] { ',' };
  109. char[] rangeSeparator = new char[] { '-' };
  110. if (!CommonHelper.GetPagesInRange(ref ConverterPreview.PageIndexLists, PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  111. { //TODO
  112. ConverterPreview.PageIndexLists.Add(0);
  113. return;
  114. }
  115. else
  116. {
  117. ConverterPreview.PageIndex.Text = (ConverterPreview.PageIndexLists.Last<int>() + 1).ToString();
  118. ConverterPreview.CurrentIndex = 0;
  119. }
  120. }
  121. }
  122. }
  123. private void cancel()
  124. {
  125. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  126. }
  127. private void converter()
  128. {
  129. FolderBrowserDialog dlg = new FolderBrowserDialog();
  130. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  131. {
  132. ConverterCSVModel.OutputPath =dlg.SelectedPath.Trim();
  133. }
  134. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterCSVModel.PageRange, PageRangeText, true, CurrentPageIndex);
  135. char[] enumerationSeparator = new char[] { ',' };
  136. char[] rangeSeparator = new char[] { '-' };
  137. if (!CommonHelper.GetPagesInRange(ref ConverterCSVModel.PageIndexLists, ConverterCSVModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  138. { //TODO
  139. Trace.WriteLine("输入不对");
  140. MessageBoxEx.Show("输入不对");
  141. return;
  142. }
  143. DialogParameters value = new DialogParameters();
  144. value.Add(ParameterNames.ConverterType,"CSV");
  145. value.Add(ParameterNames.ConverterTypeModel, ConverterCSVModel);
  146. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  147. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  148. {
  149. });
  150. }
  151. private void radiobutton(string e)
  152. {
  153. string radioButton = e;
  154. if (radioButton != null)
  155. {
  156. switch (radioButton)
  157. {
  158. case "ForEachPageRadioBtn":
  159. break;
  160. case "OnlyTableRadioBtn":
  161. break;
  162. default:
  163. break;
  164. }
  165. }
  166. }
  167. #endregion
  168. #region 构架行为
  169. public bool CanCloseDialog()
  170. {
  171. return true;
  172. }
  173. public void OnDialogClosed()
  174. {
  175. }
  176. public void OnDialogOpened(IDialogParameters parameters)
  177. {
  178. CPDFViewer pdfViewer = null;
  179. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  180. if (pdfViewer != null)
  181. {
  182. currentViewer = pdfViewer;
  183. if (currentViewer.Tag != null) { ConverterCSVModel.Pawssword = currentViewer.Tag.ToString(); }
  184. ConverterCSVModel.InputPath = pdfViewer.Document.FilePath;
  185. FileInfo fileinfo = new FileInfo(ConverterCSVModel.InputPath);
  186. ConverterCSVModel.OutputPath = fileinfo.DirectoryName;
  187. }
  188. }
  189. #endregion
  190. }
  191. }