ConverterExcelDialogViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 ConverterExcelDialogModel ConverterExcelModel = new ConverterExcelDialogModel();
  22. public CPDFViewer currentViewer;
  23. public string CurrentPageIndex = "1";
  24. public IDialogService dialogs;
  25. public string PageRangeText { set; get; } = "0";
  26. public string PageRangeSelectIndex { set; get; } = "0";
  27. public DelegateCommand<string> RadioButtonCommand { get; set; }
  28. public DelegateCommand CancelCommand { get; set; }
  29. public DelegateCommand ConverterCommnad { get; set; }
  30. public ConverterExcelDialogViewModel(IDialogService dialogService)
  31. {
  32. CancelCommand = new DelegateCommand(cancel);
  33. ConverterCommnad = new DelegateCommand(converter);
  34. RadioButtonCommand = new DelegateCommand<string>(radiobutton);
  35. dialogs = dialogService;
  36. }
  37. private void cancel()
  38. {
  39. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  40. }
  41. private void converter()
  42. {
  43. FolderBrowserDialog dlg = new FolderBrowserDialog();
  44. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  45. {
  46. ConverterExcelModel.OutputPath = dlg.SelectedPath.Trim();
  47. }
  48. HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterExcelModel.PageRange, PageRangeText, true, CurrentPageIndex);
  49. char[] enumerationSeparator = new char[] { ',' };
  50. char[] rangeSeparator = new char[] { '-' };
  51. if (!CommonHelper.GetPagesInRange(ref ConverterExcelModel.PageIndexLists, ConverterExcelModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator))
  52. { //TODO
  53. Trace.WriteLine("输入不对");
  54. MessageBoxEx.Show("输入不对");
  55. return;
  56. }
  57. if (ConverterExcelModel.excelOption == null) {
  58. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions();
  59. }
  60. DialogParameters value = new DialogParameters();
  61. value.Add(ParameterNames.ConverterType, "Excel");
  62. value.Add(ParameterNames.ConverterTypeModel, ConverterExcelModel);
  63. RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
  64. dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e =>
  65. {
  66. });
  67. }
  68. private void radiobutton(string e) {
  69. string radioButton=e;
  70. if (radioButton != null) {
  71. switch (radioButton)
  72. {
  73. case "ForEachPageRadioBtn":
  74. ConverterExcelModel.excelOption=ConverterExcelModel.ExcelOptions();
  75. break;
  76. case "ForTheDocRadioBtn":
  77. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForTheDocument);
  78. break;
  79. case "OnlyTableRadioBtn":
  80. ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForEachTable, ContentOptions.OnlyTable);
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. private void SetProgress(int pageIndex)
  88. {
  89. }
  90. public string Title => "";
  91. public event Action<IDialogResult> RequestClose;
  92. public bool CanCloseDialog()
  93. {
  94. return true;
  95. }
  96. public void OnDialogClosed()
  97. {
  98. }
  99. public void OnDialogOpened(IDialogParameters parameters)
  100. {
  101. CPDFViewer pdfViewer = null;
  102. parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out pdfViewer);
  103. if (pdfViewer != null)
  104. {
  105. currentViewer = pdfViewer;
  106. ConverterExcelModel.InputPath = pdfViewer.Document.FilePath;
  107. FileInfo fileinfo = new FileInfo(ConverterExcelModel.InputPath);
  108. ConverterExcelModel.OutputPath = fileinfo.DirectoryName;
  109. }
  110. }
  111. }
  112. }