ConverterExcelDialogViewModel.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using ComPDFKit_Conversion.Options;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Model.Dialog.ConverterDialogs;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Windows.Forms;
  16. using DialogResult = Prism.Services.Dialogs.DialogResult;
  17. namespace PDF_Office.ViewModels.Dialog.ConverterDialogs
  18. {
  19. public class ConverterExcelDialogViewModel : BindableBase, IDialogAware
  20. {
  21. public string Title => "";
  22. public event Action<IDialogResult> RequestClose;
  23. #region 参数和属性
  24. public ConverterExcelDialogModel ConverterExcelModel = new ConverterExcelDialogModel();
  25. public CPDFViewer currentViewer;
  26. public string CurrentPageIndex = "1";
  27. public IDialogService dialogs;
  28. public string PageRangeText { set; get; } = "0";
  29. public string PageRangeSelectIndex { set; get; } = "0";
  30. #endregion
  31. #region 委托声明
  32. public DelegateCommand<string> RadioButtonCommand { get; set; }
  33. public DelegateCommand CancelCommand { get; set; }
  34. public DelegateCommand ConverterCommnad { get; set; }
  35. #endregion
  36. public ConverterExcelDialogViewModel(IDialogService dialogService)
  37. {
  38. CancelCommand = new DelegateCommand(cancel);
  39. ConverterCommnad = new DelegateCommand(converter);
  40. RadioButtonCommand = new DelegateCommand<string>(radiobutton);
  41. dialogs = dialogService;
  42. }
  43. #region 逻辑函数
  44. private void cancel()
  45. {
  46. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  47. }
  48. private void converter()
  49. {
  50. FolderBrowserDialog dlg = new FolderBrowserDialog();
  51. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  52. {
  53. ConverterExcelModel.OutputPath = dlg.SelectedPath.Trim();
  54. }
  55. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterExcelModel.PageRange, PageRangeText, true, CurrentPageIndex);
  56. char[] enumerationSeparator = new char[] { ',' };
  57. char[] rangeSeparator = new char[] { '-' };
  58. if (!CommonHelper.GetPagesInRange(ref ConverterExcelModel.PageIndexLists, ConverterExcelModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  59. { //TODO
  60. Trace.WriteLine("输入不对");
  61. MessageBoxEx.Show("输入不对");
  62. return;
  63. }
  64. if (ConverterExcelModel.excelOption == null) {
  65. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions();
  66. }
  67. DialogParameters value = new DialogParameters();
  68. value.Add(ParameterNames.ConverterType, "Excel");
  69. value.Add(ParameterNames.ConverterTypeModel, ConverterExcelModel);
  70. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  71. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  72. {
  73. });
  74. }
  75. private void radiobutton(string e) {
  76. string radioButton=e;
  77. if (radioButton != null) {
  78. switch (radioButton)
  79. {
  80. case "ForEachPageRadioBtn":
  81. ConverterExcelModel.excelOption=ConverterExcelModel.ExcelOptions();
  82. break;
  83. case "ForTheDocRadioBtn":
  84. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForTheDocument);
  85. break;
  86. case "OnlyTableRadioBtn":
  87. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForEachTable, ContentOptions.OnlyTable);
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. }
  94. #endregion
  95. #region 构架行为
  96. public bool CanCloseDialog()
  97. {
  98. return true;
  99. }
  100. public void OnDialogClosed()
  101. {
  102. }
  103. public void OnDialogOpened(IDialogParameters parameters)
  104. {
  105. CPDFViewer pdfViewer = null;
  106. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  107. if (pdfViewer != null)
  108. {
  109. currentViewer = pdfViewer;
  110. ConverterExcelModel.InputPath = pdfViewer.Document.FilePath;
  111. FileInfo fileinfo = new FileInfo(ConverterExcelModel.InputPath);
  112. ConverterExcelModel.OutputPath = fileinfo.DirectoryName;
  113. }
  114. }
  115. #endregion
  116. }
  117. }