using ComPDFKit.PDFDocument; using PDF_Office.CustomControl; using PDF_Office.Model; using PDF_Office.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Windows; using System.Windows.Forms; using DialogResult = Prism.Services.Dialogs.DialogResult; namespace PDF_Office.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing { public class HomePageSetPasswordDialogViewModel : BindableBase, INavigationAware { #region 参数和属性 private List fileNames; private int fileNamesIndex = 0; private int FileNameNumber = 0; private HomePageSetPasswordDialogModel setPasswordDialogModel = new HomePageSetPasswordDialogModel(); public List fileNamesView = new List(); private bool _canOpen; public bool CanOpen { get { return _canOpen; } set { _canOpen = value; RaisePropertyChanged("isChecked"); } } private bool _canEdit; public bool CanEdit { get { return _canEdit; } set { _canEdit = value; RaisePropertyChanged("isChecked"); } } private string _changeMod = "0"; /// ///"0"为ChangeMod.None; ///"1"为ChangeMod.ChangePage; ///"2"为ChangeMod.FormAndSignature; ///"3"为ChangeMod.AnnotAndFormAndSignature; ///"4"为ChangeMod.ExceptAbstrat; /// public string ChangeMod { get { return _changeMod; } set { _changeMod = value; } } private string _printMod = "0"; /// ///"0"为PrintMod.None; ///"1"为PrintMod.LowDpi; ///"2"为PrintMod.HighDpi; /// public string PrintMod { get { return _printMod; } set { _printMod = value; } } private string safetyGridIsEnabled = "True"; public string SafetyGridIsEnabled { get { return safetyGridIsEnabled; } set { SetProperty(ref safetyGridIsEnabled, value); } } private string setSafetyGridIsEnabled = "True"; public string SetSafetyGridIsEnabled { get { return setSafetyGridIsEnabled; } set { SetProperty(ref setSafetyGridIsEnabled, value); } } private string _isEnableConfirm = "False"; public string IsEnabledConfirm { get { return _isEnableConfirm; } set { SetProperty(ref _isEnableConfirm, value); } } private DataTable pdfDataTable = new DataTable(); public DataTable PDFDataTable { get { return pdfDataTable; } set { SetProperty(ref pdfDataTable, value); } } private DataTable pdfCurrentDataTable = new DataTable(); public DataTable PDFCurrentDataTable { get { return pdfCurrentDataTable; } set { SetProperty(ref pdfCurrentDataTable, value); } } private string removeIsEnable = "False"; public string RemoveIsEnable { get { return removeIsEnable; } set { SetProperty(ref removeIsEnable, value); } } private string selectFileName = "False"; public string SelectFileName { get { return selectFileName; } set { SetProperty(ref selectFileName, value); } } private Visibility addFileVisibility = Visibility.Hidden; public Visibility AddFileVisibility { get { return addFileVisibility; } set { SetProperty(ref addFileVisibility, value); RaisePropertyChanged(); } } #endregion #region 委托声明 public DelegateCommand DelegateSetOpenCommand { get; set; } public DelegateCommand DelegateSetEditCommand { get; set; } public DelegateCommand DelegateCanOpenTextChangedCommand { get; set; } public DelegateCommand DelegateCanEditTextChangedCommand { get; set; } public DelegateCommand DelegateConfirmEncryptCommand { get; set; } public DelegateCommand DelegateCancelEncryptCommand { get; set; } public DelegateCommand ADDPDFCommand { get; set; } public DelegateCommand RemovePDFFileCommand { get; set; } public DelegateCommand ADDPDFFilesCommand { get; set; } #endregion public HomePageSetPasswordDialogViewModel() { PDFCurrentDataTable.Columns.Add("FileState"); PDFDataTable.Columns.Add("FileName"); PDFDataTable.Columns.Add("FileSize"); PDFDataTable.Columns.Add("FileState"); HomePageSetPasswordDialogModel.PasswordForOpen = ""; HomePageSetPasswordDialogModel.PasswordForEdit = ""; DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt); DelegateSetOpenCommand = new DelegateCommand(SetCanOpen); DelegateSetEditCommand = new DelegateCommand(SetCanEdit); DelegateCanOpenTextChangedCommand = new DelegateCommand(CanOpenTextChanged); DelegateCanEditTextChangedCommand = new DelegateCommand(CanEditTextChanged); ADDPDFCommand = new DelegateCommand(addpdf); ADDPDFFilesCommand = new DelegateCommand(addpdffiles); RemovePDFFileCommand = new DelegateCommand(removepdffile); } #region 检查和初始化 private void CheckCanConfirmEncrypt() { if (setPasswordDialogModel.CanOpen) { if (HomePageSetPasswordDialogModel.PasswordForOpen.Length > 0) { IsEnabledConfirm = "True"; return; } } if (setPasswordDialogModel.CanEdit) { if (HomePageSetPasswordDialogModel.PasswordForEdit.Length > 0) { IsEnabledConfirm = "True"; return; } } IsEnabledConfirm = "False"; } private void InitPermissionsDictionary(ref Dictionary GetPrintMod, ref Dictionary GetChangeMod) { GetPrintMod.Add("0", HomePageSetPasswordDialogModel.PrintMod.None); GetPrintMod.Add("1", HomePageSetPasswordDialogModel.PrintMod.LowDpi); GetPrintMod.Add("2", HomePageSetPasswordDialogModel.PrintMod.HighDpi); GetChangeMod.Add("0", HomePageSetPasswordDialogModel.ChangeMod.None); GetChangeMod.Add("1", HomePageSetPasswordDialogModel.ChangeMod.ChangePage); GetChangeMod.Add("2", HomePageSetPasswordDialogModel.ChangeMod.FormAndSignature); GetChangeMod.Add("3", HomePageSetPasswordDialogModel.ChangeMod.AnnotAndFormAndSignature); GetChangeMod.Add("4", HomePageSetPasswordDialogModel.ChangeMod.ExceptAbstrat); } #endregion #region 逻辑函数 public void SetCanOpen() { setPasswordDialogModel.CanOpen = CanOpen; CheckCanConfirmEncrypt(); } public void SetCanEdit() { setPasswordDialogModel.CanEdit = CanEdit; CheckCanConfirmEncrypt(); } public void CanOpenTextChanged() { CheckCanConfirmEncrypt(); } public void CanEditTextChanged() { CheckCanConfirmEncrypt(); } public void ConfirmEncrypt() { SafetyGridIsEnabled = "False"; string openPassword = ""; string editPassword = ""; CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); if (setPasswordDialogModel.CanOpen) { if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForOpen)) { openPassword = HomePageSetPasswordDialogModel.PasswordForOpen; } } if (setPasswordDialogModel.CanEdit) { if (!string.IsNullOrEmpty(HomePageSetPasswordDialogModel.PasswordForEdit)) { editPassword = HomePageSetPasswordDialogModel.PasswordForEdit; Dictionary GetPrintMod = new Dictionary(); Dictionary GetChangeMod = new Dictionary(); ; InitPermissionsDictionary(ref GetPrintMod, ref GetChangeMod); permissionsInfo = setPasswordDialogModel.CreatePermissionsInfo(GetPrintMod[PrintMod], GetChangeMod[ChangeMod]); } } fileNamesIndex = 0; foreach (var filename in fileNames) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "待完成"; CPDFDocument document = CPDFDocument.InitWithFilePath(filename); if (document == null || document.IsEncrypted) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "文件出错"; fileNamesIndex++; continue; } FileInfo fileinfo = new FileInfo(filename); string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K"; if (document == null) { return; } document.Encrypt(openPassword, editPassword, permissionsInfo); document.WriteToFilePath(filename + "SetPassword"); PDFDataTable.Rows[fileNamesIndex]["FileState"] = "完成"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "完成"; fileNamesIndex++; document.Release(); } SafetyGridIsEnabled = "True"; MessageBoxEx.Show("已完成"); } #endregion #region 批量处理逻辑函数 /// /// 添加PDF文件 /// private void addpdf() { FileNameNumber = fileNames.Count; System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog(); dlg.Multiselect = true; dlg.Filter = "PDF|*.pdf;*.PDF;"; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { fileNames.AddRange(dlg.FileNames.ToList()); RemoveExcess(ref fileNames); SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("待确定"); } } public void addPDFFiles(string filename) { FileNameNumber = fileNames.Count; fileNames.Add(filename); RemoveExcess(ref fileNames); SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("待确定"); } /// /// 删除重复的文件 /// public void RemoveExcess(ref List Filenames) { List filenames = new List(); foreach (var fileName in Filenames) { if (!filenames.Contains(fileName)) { filenames.Add(fileName); } } Filenames.Clear(); Filenames = filenames; } /// /// 添加PDF文件夹 /// private void addpdffiles() { FileNameNumber = fileNames.Count; FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径"; if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string foldPath = dialog.SelectedPath; var apps = System.IO.Directory.GetFiles(foldPath); foreach (string app in apps) { var fi = new FileInfo(app); if (fi.Extension == ".pdf" || fi.Extension == ".PDF") { fileNames.Add(app); } } RemoveExcess(ref fileNames); updateListview("待确定"); SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; } } /// /// 更新listview显示 /// state 状态显示字符串 /// public void updateListview(string state) { updateCurrentListview(); DataTable pdfdatatable = new DataTable(); pdfdatatable.Columns.Add("FileName"); pdfdatatable.Columns.Add("FileSize"); pdfdatatable.Columns.Add("FileState"); int datatableindex = 0; foreach (var fileName in fileNames) { string file_all = fileName; FileInfo f = new FileInfo(file_all); string file_size = (((float)f.Length) / 1024).ToString() + " K"; pdfdatatable.Rows.Add(f.Name, file_size, PDFCurrentDataTable.Rows[datatableindex]["FileState"]); datatableindex++; } PDFDataTable = pdfdatatable; HomePageBatchProcessingDialogModel.FilePaths = fileNames; } /// /// 更新Currentlistview显示 /// state 状态显示字符串 /// public void updateCurrentListview( string state = "待完成") { if (fileNames.Count >= FileNameNumber) { for (int i = 0; fileNames.Count - FileNameNumber > i; i++) { PDFCurrentDataTable.Rows.Add(state); } } else { Reverseorder(ref fileNamesView); foreach (int filenamesview in fileNamesView) { PDFCurrentDataTable.Rows.RemoveAt(filenamesview); } } } /// /// 逆序int类型集合 /// public void Reverseorder(ref List Numbers) { Numbers = Numbers.OrderBy(a => a).ToList(); Numbers.Reverse(); } /// /// 打开文件PDF /// public void openfiledialog() { foreach (int filenamesview in fileNamesView) { System.Diagnostics.Process.Start(fileNames[filenamesview]); } } /// /// 删除文件PDF /// public void removepdffile() { FileNameNumber = fileNames.Count; Reverseorder(ref fileNamesView); foreach (int filenamesview in fileNamesView) { //Trace.WriteLine(filenamesview); fileNames.Remove(fileNames[filenamesview]); } if (fileNames.Count < 1) { SetSafetyGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } updateListview("待确定"); } public void removepdffile(int index) { PDFCurrentDataTable.Rows.RemoveAt(index); fileNames.Remove(fileNames[index]); if (fileNames.Count < 1) { SetSafetyGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } updateListview("待确定"); } public void PDFFileCount() { if (fileNames.Count == 0) { SetSafetyGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } else { SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; } } #endregion public void OnNavigatedTo(NavigationContext navigationContext) { List filepath = new List(); navigationContext.Parameters.TryGetValue>(ParameterNames.FilePath, out filepath); if (filepath != null) { fileNames = filepath; PDFFileCount(); updateListview("待确定"); } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } }