123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- using ComPDFKitViewer.PdfViewer;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
- using PDF_Office.Model.PDFTool;
- using PDFSettings;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- namespace PDF_Office.ViewModels.HomePanel.PDFTools
- {
- public class QuickToolsContentViewModel : BindableBase
- {
- #region 属性
- /// <summary>
- /// 扩展/收缩
- /// </summary>
- private bool _isExpendTools = false;
- public bool IsExpendTools
- {
- get { return _isExpendTools; }
- set { SetProperty(ref _isExpendTools, value); }
- }
- private string regionName;
- public string ToolRegionName
- {
- get { return regionName; }
- set { SetProperty(ref regionName, value); }
- }
- #endregion
- #region Command and Event
- public IDialogService dialogs;
- public DelegateCommand<ToolItem> QuickToolsCommand { get; set; }
- public DelegateCommand<object> OpenMenuCommand { get; set; }
- public DelegateCommand ExpendCommand { get; set; }
- public DelegateCommand ShowToolCommand { get; set; }
- public IRegionManager toolregion;
- public event EventHandler<bool> ExpendToolsHanlder;
- #endregion
- public QuickToolsContentViewModel(IDialogService dialogService, IRegionManager regionManager)
- {
- toolregion = regionManager;
- ToolRegionName = RegionNames.ToolRegionName;
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- toolregion.RequestNavigate(ToolRegionName, "Guid");
- }));
- dialogs = dialogService;
- InitVariable();
- InitCommand();
-
- }
- #region 初始化和绑定
- private void InitVariable()
- {
- }
- private void InitCommand()
- {
- QuickToolsCommand = new DelegateCommand<ToolItem>(QuickTools_Click);
- OpenMenuCommand = new DelegateCommand<object>(OpenMenu);
- ExpendCommand = new DelegateCommand(Expend);
- ShowToolCommand = new DelegateCommand(ShowTool);
- }
- #endregion
- private void ShowTool()
- {
- toolregion.RequestNavigate(ToolRegionName, "Tools");
- }
- public void QuickTools_Click(ToolItem toolItem)
- {
- /*
- *设置这个对话框的起始保存路径
- */
- System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
- dlg.Multiselect = false;
- dlg.Filter = "PDF|*.pdf;*.PDF;";
- if (toolItem.FnType == PDFFnType.Compress || toolItem.FnType == PDFFnType.Security||toolItem.FnType ==PDFFnType.BatchRemove||toolItem.FnType==PDFFnType.ConvertPDF)
- {
- dlg.Multiselect = true;
- }
- if (toolItem.FnType == PDFFnType.ImageToPDF)
- {
- dlg.Multiselect = true;
- dlg.Filter = "Picture|*.png;*.PNG;*.jpg;*.JPG;*bmp;*jpeg;*gif;*tiff;";
- }
- if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- CPDFViewer viewer = new CPDFViewer();
- switch ((PDFFnType)toolItem.FnType)
- {
- case PDFFnType.Split:
- viewer.InitDocument(dlg.FileName);
- CheckPassword(viewer);
- DialogParameters splitvalue = new DialogParameters();
- splitvalue.Add(ParameterNames.PDFViewer, viewer);
- splitvalue.Add(ParameterNames.FilePath, dlg.FileName);
- dialogs.ShowDialog(DialogNames.HomePageSplitDialog, splitvalue, e => { });
- break;
- case PDFFnType.Extract:
- viewer.InitDocument(dlg.FileName);
- CheckPassword(viewer);
- DialogParameters extractvalue = new DialogParameters();
- extractvalue.Add(ParameterNames.PDFViewer, viewer);
- extractvalue.Add(ParameterNames.FilePath, dlg.FileName);
- dialogs.ShowDialog(DialogNames.HomePageExtractDialog, extractvalue, e => { });
- break;
- case PDFFnType.Insert:
- viewer.InitDocument(dlg.FileName);
- CheckPassword(viewer);
- DialogParameters insertvalue = new DialogParameters();
- insertvalue.Add(ParameterNames.PDFViewer, viewer);
- insertvalue.Add(ParameterNames.FilePath, dlg.FileName);
- dialogs.ShowDialog(DialogNames.HomePageInsertDialog, insertvalue, e => { });
- break;
- case PDFFnType.Compress:
- DialogParameters compresspdf = new DialogParameters();
- compresspdf.Add(ParameterNames.BatchProcessing_Name, "1");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 1;
- compresspdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, compresspdf, e => { });
- break;
- case PDFFnType.ImageToPDF:
- DialogParameters picturetopdf = new DialogParameters();
- picturetopdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePagePictureToPDFDialog, picturetopdf, e => { });
- break;
- case PDFFnType.Print:
- viewer.InitDocument(dlg.FileName);
- CheckPassword(viewer);
- DialogParameters printvalue = new DialogParameters();
- printvalue.Add(ParameterNames.PDFViewer, viewer);
- printvalue.Add(ParameterNames.FilePath, dlg.FileName);
- dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
- break;
- case PDFFnType.Security:
- DialogParameters securitypdf = new DialogParameters();
- securitypdf.Add(ParameterNames.BatchProcessing_Name, "2");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 2;
- securitypdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, securitypdf, e => { });
- break;
- case PDFFnType.ConvertPDF:
- DialogParameters convertpdf = new DialogParameters();
- convertpdf.Add(ParameterNames.BatchProcessing_Name, "0");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- convertpdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdf, e => { });
- break;
- case PDFFnType.BatchRemove:
- DialogParameters batchremovepdf = new DialogParameters();
- batchremovepdf.Add(ParameterNames.BatchProcessing_Name, "7");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 7;
- batchremovepdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batchremovepdf, e => { });
- break;
- case PDFFnType.HeaderFooter:
- DialogParameters headerFooterpdf = new DialogParameters();
- headerFooterpdf.Add(ParameterNames.BatchProcessing_Name, "5");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 5;
- headerFooterpdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, headerFooterpdf, e => { });
- break;
- case PDFFnType.BatesNumbers:
- DialogParameters batesNumberspdf = new DialogParameters();
- batesNumberspdf.Add(ParameterNames.BatchProcessing_Name, "6");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 6;
- batesNumberspdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batesNumberspdf, e => { });
- break;
- case PDFFnType.Background:
- DialogParameters backgroundpdf = new DialogParameters();
- backgroundpdf.Add(ParameterNames.BatchProcessing_Name, "4");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 4;
- backgroundpdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, backgroundpdf, e => { });
- break;
- case PDFFnType.WaterMark:
- DialogParameters watermarkpdf = new DialogParameters();
- watermarkpdf.Add(ParameterNames.BatchProcessing_Name, "3");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 3;
- watermarkpdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, watermarkpdf, e => { });
- break;
- case PDFFnType.PDFToImage:
- DialogParameters convertpdftoimage = new DialogParameters();
- convertpdftoimage.Add(ParameterNames.BatchProcessing_Name, "0");
- convertpdftoimage.Add("ConverterTypeIndex", 7);
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- convertpdftoimage.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoimage, e => { });
- break;
- case PDFFnType.PDFToExcel:
- DialogParameters convertpdftoexcel = new DialogParameters();
- convertpdftoexcel.Add(ParameterNames.BatchProcessing_Name, "0");
- convertpdftoexcel.Add("ConverterTypeIndex", 1);
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- convertpdftoexcel.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoexcel, e => { });
- break;
- case PDFFnType.PDFToPPT:
- DialogParameters convertpdftoppt = new DialogParameters();
- convertpdftoppt.Add(ParameterNames.BatchProcessing_Name, "0");
- convertpdftoppt.Add("ConverterTypeIndex", 2);
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- convertpdftoppt.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoppt, e => { });
- break;
- case PDFFnType.PDFToWord:
- DialogParameters convertpdftoword = new DialogParameters();
- convertpdftoword.Add(ParameterNames.BatchProcessing_Name, "0");
- convertpdftoword.Add("ConverterTypeIndex", 0);
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- convertpdftoword.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, convertpdftoword, e => { });
- break;
- case PDFFnType.Batch:
- DialogParameters batcpdf = new DialogParameters();
- batcpdf.Add(ParameterNames.BatchProcessing_Name, "0");
- HomePageBatchProcessingDialogModel.FilePaths = dlg.FileNames.ToList();
- HomePageBatchProcessingDialogModel.BatchProcessingIndex = 0;
- batcpdf.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.HomePageBatchProcessingDialog, batcpdf, e => { });
- break;
- }
- }
- }
- //
- //
- private void CheckPassword(CPDFViewer viewer) {
- if (viewer.Document.IsLocked)
- {
- DialogParameters value = new DialogParameters();
- value.Add(ParameterNames.PDFDocument, viewer.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)
- {
- viewer.Tag = e.Parameters.GetValue<string>(ParameterNames.PassWord).ToString();
- }
- }
- });
- if (viewer.Document.IsLocked)
- {
- //未成功解密文档时,释放Document对象,返回
- viewer.Document.Release();
- return;
- }
- }
- }
- private void Expend()
- {
- IsExpendTools = !IsExpendTools;
- ExpendToolsHanlder?.Invoke(null, IsExpendTools);
- }
- private void OpenMenu(object obj)
- {
- var menu = App.Current.FindResource("ExpendToolsContextMenu") as ContextMenu;
- var btn = obj as Button;
- if (menu != null && btn != null)
- {
- if(menu.Items.Count == 1)
- {
- var item = menu.Items[0] as MenuItem;
- if(_isExpendTools)
- {
- item.Header = "收缩";
- }
- else
- {
- item.Header = "展开";
- }
- item.Command = ExpendCommand;
- //btn.ContextMenu = menu;
- menu.PlacementTarget = btn;
- menu.IsOpen = true;
- }
- }
- }
- }
- }
|