using ComPDFKit.PDFDocument; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using PDF_Office.Model.HomePageToolsDialogs; using System.Diagnostics; using DialogResult = Prism.Services.Dialogs.DialogResult; using PDF_Office.CustomControl; using PDF_Office.Helper; namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs { public class HomePageInsertDialogViewModel : BindableBase, IDialogAware { #region 参数和属性 public CPDFDocument document; public CPDFViewer currentViewer; private HomePageInsertDialogModel insertModel = new HomePageInsertDialogModel(); 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() { CancelCommand = new DelegateCommand(cancel); InsertCommand = new DelegateCommand(insert); SelectFileCommand = new DelegateCommand(selectFile); FirstPageCommand = new DelegateCommand(firstPage); LastPageCommand = new DelegateCommand(lastPage); CustomPageCommand = new DelegateCommand(customPage); } #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.IsEncrypted) { Trace.WriteLine("youmima"); MessageBoxEx.Show("文档加密"); //TODO 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 } }