123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Office.Core;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Model.Dialog.ConverterDialogs;
- using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing;
- using PDF_Office.Model.Dialog.ToolsDialogs;
- using PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs;
- using PDF_Office.Model.PDFTool;
- using PDF_Office.Views;
- 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;
- using static PDF_Office.Model.Dialog.ToolsDialogs.SaftyDialogs.DeleteSafetySettintgsModel;
- namespace PDF_Office.ViewModels.HomePanel.PDFTools
- {
- public class QuickToolsContentViewModel : BindableBase
- {
- #region 文案
- private string T_title;
- public string T_Title
- {
- get { return T_title; }
- set
- {
- SetProperty(ref T_title, value);
- }
- }
- private void InitString()
- {
- T_Title = App.HomePageLoader.GetString("HomeTool_Title");
- }
- #endregion
- #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();
- InitString();
- }
- #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||
- toolItem.FnType==PDFFnType.Background|| toolItem.FnType == PDFFnType.HeaderFooter||
- toolItem.FnType == PDFFnType.BatesNumbers|| toolItem.FnType == PDFFnType.WaterMark||
- toolItem.FnType == PDFFnType.PDFToExcel|| toolItem.FnType == PDFFnType.PDFToImage||
- toolItem.FnType == PDFFnType.PDFToPPT|| toolItem.FnType == PDFFnType.Batch ||
- toolItem.FnType == PDFFnType.Merge)
- {
- //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);
- if (!CheckPassword(viewer)) {return;}
- 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);
- if (!CheckPassword(viewer)) {return;}
- 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);
- if (!CheckPassword(viewer)) {return;}
- 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:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters compresspdf = new DialogParameters();
- compresspdf.Add(ParameterNames.PDFViewer, viewer);
- dialogs.ShowDialog(DialogNames.CompressDialog, compresspdf, e =>
- {
- CompressDialogModel compressDialogModel = new CompressDialogModel();
- compressDialogModel.OnOpened((Prism.Services.Dialogs.DialogResult)e);
- // OnOpened((Prism.Services.Dialogs.DialogResult)e);
- });
- //批量
- //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);
- if (!CheckPassword(viewer)) {return;}
- DialogParameters printvalue = new DialogParameters();
- printvalue.Add(ParameterNames.PDFViewer, viewer);
- printvalue.Add(ParameterNames.FilePath, dlg.FileName);
- dialogs.ShowDialog(DialogNames.HomePagePrinterDialog, printvalue, e => { });
- try
- {
- dialogs.ShowDialog("MainWindow");
- }
- catch
- {
- }
- 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 => { });
- CPDFViewer cPDFViewer = new CPDFViewer();
- cPDFViewer.InitDocument(dlg.FileName);
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(cPDFViewer.Document, EnumPasswordKind.StatusPermissionsPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- cPDFViewer.Document.UnlockWithPassword(result.Password);
- }
- ///TODO:
- ///此处填入需要执行的代码
- ///
- DialogParameters param = new DialogParameters();
- param.Add(ParameterNames.PDFViewer, cPDFViewer);
- dialogs.ShowDialog(DialogNames.SetPasswordDialog, param, e => {
- string FilePath;
- if (e.Parameters.TryGetValue("FilePath",out FilePath))
- {
- CommonHelper.ShowFileBrowser(FilePath);
- }
- });
- }
- 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:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters convertpdftoimage = new DialogParameters();
- convertpdftoimage.Add("PageRangeComboBoxCurrentPage", true);
- convertpdftoimage.Add(ParameterNames.PDFViewer, viewer);
- dialogs.ShowDialog(DialogNames.ConverterImgDialog, convertpdftoimage, e =>
- {
- ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
- converterDialogsModel.OnOpened((DialogResult)e);
- });
- //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:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters convertpdftoexcel = new DialogParameters();
- convertpdftoexcel.Add(ParameterNames.PDFViewer, viewer);
- convertpdftoexcel.Add("PageRangeComboBoxCurrentPage", true);
- dialogs.ShowDialog(DialogNames.ConverterExcelDialog, convertpdftoexcel, e =>
- {
- ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
- converterDialogsModel.OnOpened((DialogResult)e);
- });
- //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:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters convertpdftoPPT = new DialogParameters();
- convertpdftoPPT.Add(ParameterNames.PDFViewer, viewer);
- convertpdftoPPT.Add("PageRangeComboBoxCurrentPage", true);
- dialogs.ShowDialog(DialogNames.ConverterPPTDialog, convertpdftoPPT, e =>
- {
- ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
- converterDialogsModel.OnOpened((DialogResult)e);
- });
- //批量处理
- //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:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters convertpdftoword = new DialogParameters();
- convertpdftoword.Add(ParameterNames.PDFViewer, viewer);
- convertpdftoword.Add("PageRangeComboBoxCurrentPage", true);
- dialogs.ShowDialog(DialogNames.ConverterWordDialog, convertpdftoword, e =>
- {
- ConverterDialogsModel converterDialogsModel = new ConverterDialogsModel();
- converterDialogsModel.OnOpened((DialogResult)e);
- });
- //批量处理
- //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;
- case PDFFnType.Merge:
- viewer.InitDocument(dlg.FileName);
- if (!CheckPassword(viewer)) { return; }
- DialogParameters merge = new DialogParameters();
- merge.Add(ParameterNames.PDFViewer, viewer);
- merge.Add(ParameterNames.FilePath, dlg.FileNames);
- dialogs.ShowDialog(DialogNames.MergeDialog, merge, e =>
- {
-
- });
- break;
- }
- }
- }
- //
- //
- private bool CheckPassword(CPDFViewer viewer) {
- if (viewer.Document == null)
- {
- //MessageBoxEx.Show(App.MainPageLoader.GetString("Main_OpenFileFailedWarning"));
- return false;
- }
- if (viewer.Document.IsLocked)
- {
- VerifyPasswordResult result = SecurityHelper.VerifyPasswordByPasswordKind(viewer.Document, EnumPasswordKind.StatusOpenPassword, dialogs);
- if (result.IsDiscryptied)
- {
- if (result.Password != null)
- {
- string filePath = viewer.Document.FilePath;
- viewer.Document.UnlockWithPassword(result.Password);
- viewer.Tag = result.Password;
- }
- ///TODO:
- ///此处填入需要执行的代码
- }
- if (result.IsDiscryptied == false)
- {
- //未成功解密文档时,释放Document对象,返回
- viewer.Document.Release();
- return false;
- }
- }
- return true;
- }
- 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 = "Collapse";
- }
- else
- {
- item.Header = "Expand";
- }
- item.Command = ExpendCommand;
- //btn.ContextMenu = menu;
- menu.PlacementTarget = btn;
- menu.IsOpen = true;
- }
- }
- }
- }
- }
|