ConverterCSVDialogViewModel.cs 10 KB

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