using ComPDFKit_Conversion.Options; using ComPDFKitViewer.PdfViewer; using PDF_Office.CustomControl; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.Dialog.ConverterDialogs; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Windows.Forms; using DialogResult = Prism.Services.Dialogs.DialogResult; namespace PDF_Office.ViewModels.Dialog.ConverterDialogs { public class ConverterExcelDialogViewModel : BindableBase, IDialogAware { public string Title => ""; public event Action RequestClose; #region 参数和属性 public ConverterExcelDialogModel ConverterExcelModel = new ConverterExcelDialogModel(); public CPDFViewer currentViewer; public string CurrentPageIndex = "1"; public IDialogService dialogs; public string PageRangeText { set; get; } = "0"; public string PageRangeSelectIndex { set; get; } = "0"; #endregion #region 委托声明 public DelegateCommand RadioButtonCommand { get; set; } public DelegateCommand CancelCommand { get; set; } public DelegateCommand ConverterCommnad { get; set; } #endregion public ConverterExcelDialogViewModel(IDialogService dialogService) { CancelCommand = new DelegateCommand(cancel); ConverterCommnad = new DelegateCommand(converter); RadioButtonCommand = new DelegateCommand(radiobutton); dialogs = dialogService; } #region 逻辑函数 private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void converter() { FolderBrowserDialog dlg = new FolderBrowserDialog(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { ConverterExcelModel.OutputPath = dlg.SelectedPath.Trim(); } HomePageEditHelper.GetPagerange(PageRangeSelectIndex, currentViewer, ref ConverterExcelModel.PageRange, PageRangeText, true, CurrentPageIndex); char[] enumerationSeparator = new char[] { ',' }; char[] rangeSeparator = new char[] { '-' }; if (!CommonHelper.GetPagesInRange(ref ConverterExcelModel.PageIndexLists, ConverterExcelModel.PageRange, currentViewer.Document.PageCount, enumerationSeparator, rangeSeparator)) { //TODO Trace.WriteLine("输入不对"); MessageBoxEx.Show("输入不对"); return; } if (ConverterExcelModel.excelOption == null) { ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(); } DialogParameters value = new DialogParameters(); value.Add(ParameterNames.ConverterType, "Excel"); value.Add(ParameterNames.ConverterTypeModel, ConverterExcelModel); RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); dialogs.ShowDialog(DialogNames.ConverterProgressBarDialog, value, e => { }); } private void radiobutton(string e) { string radioButton=e; if (radioButton != null) { switch (radioButton) { case "ForEachPageRadioBtn": ConverterExcelModel.excelOption=ConverterExcelModel.ExcelOptions(); break; case "ForTheDocRadioBtn": ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForTheDocument); break; case "OnlyTableRadioBtn": ConverterExcelModel.excelOption = ConverterExcelModel.ExcelOptions(WorksheetOptions.ForEachTable, ContentOptions.OnlyTable); break; default: break; } } } #endregion #region 构架行为 public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { CPDFViewer pdfViewer = null; parameters.TryGetValue(ParameterNames.PDFViewer, out pdfViewer); if (pdfViewer != null) { currentViewer = pdfViewer; ConverterExcelModel.InputPath = pdfViewer.Document.FilePath; FileInfo fileinfo = new FileInfo(ConverterExcelModel.InputPath); ConverterExcelModel.OutputPath = fileinfo.DirectoryName; } } #endregion } }