123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- 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<string>(ParameterNames.PassWord) != null)
- {
- document.UnlockWithPassword(e.Parameters.GetValue<string>(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<IDialogResult> RequestClose;
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- CPDFViewer viewer = null;
- string filepath = "";
- parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out viewer);
- parameters.TryGetValue<string>(ParameterNames.FilePath, out filepath);
- if (viewer != null && viewer.Document != null)
- {
- currentViewer = viewer;
- document = currentViewer.Document;
- PageNumber = currentViewer.Document.PageCount.ToString();
- }
- }
- #endregion
- }
- }
|