ConverterCSVDialogViewModel.cs 9.8 KB

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