using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using PDF_Master.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using PDF_Master.Model.HomePageToolsDialogs; using System.Diagnostics; using DialogResult = Prism.Services.Dialogs.DialogResult; using PDF_Master.CustomControl; using PDF_Master.Helper; namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs { public class HomePageInsertDialogViewModel : BindableBase, IDialogAware { #region 参数和属性 public CPDFDocument document; public CPDFViewer currentViewer; private HomePageInsertDialogModel insertModel = new HomePageInsertDialogModel(); public IDialogService dialogs; private string selectFilePath = "选择文件"; public string SelectFilePath { get { return selectFilePath; } set { SetProperty(ref selectFilePath, value); } } public string PageLocation { get; set; } = "0"; private string pageNumber = "1"; public string PageNumber { get { return pageNumber; } set { SetProperty(ref pageNumber, value); } } public string PageInsertIndex { get; set; } = "1"; private string firstIsCheck = "True"; public string FirstIsCheck { get { return firstIsCheck; } set { SetProperty(ref firstIsCheck, value); } } private string lastIsCheck = "False"; public string LastIsCheck { get { return lastIsCheck; } set { SetProperty(ref lastIsCheck, value); } } private string customIsCheck = "False"; public string CustomIsCheck { get { return customIsCheck; } set { SetProperty(ref customIsCheck, value); } } private string customIsEnabled = "False"; public string CustomIsEnabled { get { return customIsEnabled; } set { SetProperty(ref customIsEnabled, value); } } #endregion #region 委托声明 public DelegateCommand CancelCommand { get; set; } public DelegateCommand InsertCommand { get; set; } public DelegateCommand SelectFileCommand { get; set; } public DelegateCommand FirstPageCommand { get; set; } public DelegateCommand LastPageCommand { get; set; } public DelegateCommand CustomPageCommand { get; set; } #endregion public HomePageInsertDialogViewModel(IDialogService dialogs) { CancelCommand = new DelegateCommand(cancel); InsertCommand = new DelegateCommand(insert); SelectFileCommand = new DelegateCommand(selectFile); FirstPageCommand = new DelegateCommand(firstPage); LastPageCommand = new DelegateCommand(lastPage); CustomPageCommand = new DelegateCommand(customPage); this.dialogs = dialogs; } #region 逻辑函数 private void cancel() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } private void insert() { CPDFDocument insertdocument = CPDFDocument.InitWithFilePath(insertModel.FilePath); if (insertdocument == null) { Trace.WriteLine("Document==null"); //TODO MessageBoxEx.Show("文档为空"); return; } if (insertdocument.IsLocked) { DialogParameters value = new DialogParameters(); value.Add(ParameterNames.PDFDocument, document); dialogs.ShowDialog(DialogNames.VerifyPassWordDialog, value, e => { if (e.Result == ButtonResult.OK) { if (e.Parameters.ContainsKey(ParameterNames.PassWord) && e.Parameters.GetValue(ParameterNames.PassWord) != null) { document.UnlockWithPassword(e.Parameters.GetValue(ParameterNames.PassWord).ToString()); } } }); if (insertdocument.IsLocked) { return; } } insertModel.InsertIndex = int.Parse(PageInsertIndex); if (PageLocation == "1") { insertModel.InsertIndex = insertModel.InsertIndex - 1; } currentViewer.Document.ImportPagesAtIndex(insertdocument, insertModel.InserPageRange(insertdocument), insertModel.InsertIndex); currentViewer.Document.WriteToLoadedPath(); insertdocument.Release(); CommonHelper.ShowFileBrowser(currentViewer.Document.FilePath); //System.Diagnostics.Process.Start("Explorer", "/select," + currentViewer.Document.FilePath); RequestClose.Invoke(new DialogResult(ButtonResult.OK)); } private void selectFile() { System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Multiselect = false; dlg.Filter = "PDF|*.pdf;*.PDF;"; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { insertModel.FilePath = dlg.FileName; SelectFilePath = dlg.FileName; } } private void firstPage() { insertModel.InsertIndex = 1; CustomIsEnabled = "False"; FirstIsCheck = "True"; LastIsCheck = "False"; CustomIsCheck = "False"; } private void lastPage() { insertModel.InsertIndex = currentViewer.Document.PageCount; CustomIsEnabled = "False"; FirstIsCheck = "False"; LastIsCheck = "True"; CustomIsCheck = "False"; } private void customPage() { CustomIsEnabled = "True"; FirstIsCheck = "False"; LastIsCheck = "False"; CustomIsCheck = "True"; } #endregion #region 构架行为 public string Title => ""; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { CPDFViewer viewer = null; string filepath = ""; parameters.TryGetValue(ParameterNames.PDFViewer, out viewer); parameters.TryGetValue(ParameterNames.FilePath, out filepath); if (viewer != null && viewer.Document != null) { currentViewer = viewer; document = currentViewer.Document; PageNumber = currentViewer.Document.PageCount.ToString(); } } #endregion } }