ConverterCSVDialogViewModel.cs 9.8 KB

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