using ComPDFKit.PDFDocument; using PDF_Master.CustomControl; using PDF_Master.Model; using PDF_Master.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 CheckBox = System.Windows.Controls.CheckBox; using DialogResult = Prism.Services.Dialogs.DialogResult; using static PDF_Master.Model.Dialog.HomePageToolsDialogs.HomePageBatchProcessing.HomePageSetPasswordDialogModel; using PDF_Master.Model.Dialog.ToolsDialogs.SaftyDialogs; using PDF_Master.Helper; namespace PDF_Master.ViewModels.Dialog.HomePageToolsDialogs.HomePageBatchProcessing { public class HomePageSetPasswordDialogViewModel : BindableBase, INavigationAware { #region 参数和属性 private List fileNames; public IDialogService dialogs; 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 EnumPermissionsMod enumPermissionsMod = EnumPermissionsMod.StatusUnknown | EnumPermissionsMod.StatusRestrictPrinting | EnumPermissionsMod.StatusRestrictCopying; 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 _isEnableConfirm = "False"; public string IsEnabledConfirm { get { return _isEnableConfirm; } set { SetProperty(ref _isEnableConfirm, value); } } private bool _enableConfirm = false; public bool EnableConfirm { get { return _enableConfirm; } set { SetProperty(ref _enableConfirm, value); } } private string _openPassword = ""; public string OpenPassword { get { return _openPassword; } set { SetProperty(ref _openPassword, value); CheckEnableConfirmEncrypt(); } } private string _permissionsPassword = ""; public string PermissionsPassword { get { return _permissionsPassword; } set { SetProperty(ref _permissionsPassword, value); CheckEnableConfirmEncrypt(); } } private string _openPasswordMsg = "Open Password"; public string OpenPasswordMsg { get { return _openPasswordMsg; } set { SetProperty(ref _openPasswordMsg, value); } } private string _permissionsPasswordMsg = "Permission Password"; public string PermissionsPasswordMsg { get { return _permissionsPasswordMsg; } set { SetProperty(ref _permissionsPasswordMsg, 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 Visibility _openTextVisibility = Visibility.Collapsed; public Visibility OpenTextVisibility { get { return _openTextVisibility; } set { SetProperty(ref _openTextVisibility, value); } } private Visibility _openPasswordVisibility = Visibility.Visible; public Visibility OpenPasswordVisibility { get { return _openPasswordVisibility; } set { if (value == Visibility.Visible) { OpenTextVisibility = Visibility.Collapsed; } else { OpenTextVisibility = Visibility.Visible; } SetProperty(ref _openPasswordVisibility, value); } } private Visibility _permissionsPasswordVisibility = Visibility.Visible; public Visibility PermissionsPasswordVisibility { get { return _permissionsPasswordVisibility; } set { if (value == Visibility.Visible) { PermissionsTextVisibility = Visibility.Collapsed; } else { PermissionsTextVisibility = Visibility.Visible; } SetProperty(ref _permissionsPasswordVisibility, value); } } private Visibility _permissionsTextVisibility = Visibility.Collapsed; public Visibility PermissionsTextVisibility { get { return _permissionsTextVisibility; } set { SetProperty(ref _permissionsTextVisibility, value); } } private bool? _isRestrictPrinting; public bool? IsRestrictPrinting { get { return _isRestrictPrinting; } set { SetProperty(ref _isRestrictPrinting, value); } } private bool? _isRestrictCopying; public bool? IsRestrictCopying { get { return _isRestrictCopying; } set { SetProperty(ref _isRestrictCopying, value); } } private bool _needOpenPassword; public bool NeedOpenPassword { get { return _needOpenPassword; } set { SetProperty(ref _needOpenPassword, value); } } private bool _needPermissionsPassword; public bool NeedPermissionsPassword { get { return _needPermissionsPassword; } set { SetProperty(ref _needPermissionsPassword, value); } } private bool _isOpenPasswordDisplayed; public bool IsOpenPasswordDisplayed { get { return _isOpenPasswordDisplayed; } set { SetProperty(ref _isOpenPasswordDisplayed, value); } } private bool _isPermissionsPasswordDisplayed; public bool IsPermissionsPasswordDisplayed { get { return _isPermissionsPasswordDisplayed; } set { SetProperty(ref _isPermissionsPasswordDisplayed, 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 DisplayPasswordCommand { get; set; } public DelegateCommand SetOpenPasswordCommand { get; set; } public DelegateCommand SetPermissionsPasswordCommand { get; set; } public DelegateCommand SetRestrictCommand { get; set; } public DelegateCommand EncryptCommand { get; set; } 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 CancelEncryptCommand { get; set; } public DelegateCommand DelegateCancelEncryptCommand { get; set; } public DelegateCommand ADDPDFCommand { get; set; } public DelegateCommand ADDOpenedPDFCommand { get; set; } public DelegateCommand RemovePDFFileCommand { get; set; } public DelegateCommand ADDPDFFilesCommand { get; set; } #endregion public HomePageSetPasswordDialogViewModel(IDialogService dialogs) { PDFCurrentDataTable.Columns.Add("FileState"); PDFDataTable.Columns.Add("FileName"); PDFDataTable.Columns.Add("FileSize"); PDFDataTable.Columns.Add("FileState"); HomePageSetPasswordDialogModel.PasswordForOpen = ""; HomePageSetPasswordDialogModel.PasswordForEdit = ""; DisplayPasswordCommand = new DelegateCommand(DisplayPassword); SetOpenPasswordCommand = new DelegateCommand(SetOpenPassword); SetPermissionsPasswordCommand = new DelegateCommand(SetPermissionsPassword); SetRestrictCommand = new DelegateCommand(SetRestrict); EncryptCommand = new DelegateCommand(ConfirmEncrypt); DelegateConfirmEncryptCommand = new DelegateCommand(ConfirmEncrypt); ADDPDFCommand = new DelegateCommand(addpdf); ADDOpenedPDFCommand = new DelegateCommand(addOpenedPDFFiles); ADDPDFFilesCommand = new DelegateCommand(addpdffiles); RemovePDFFileCommand = new DelegateCommand(removepdffile); this.dialogs = dialogs; } #region 检查和初始化 private void CheckEnableConfirmEncrypt() { EnableConfirm = true; if (!(NeedOpenPassword || NeedPermissionsPassword)) { EnableConfirm = false; } if (NeedOpenPassword && string.IsNullOrEmpty(OpenPassword)) { EnableConfirm = false; } if (NeedPermissionsPassword && string.IsNullOrEmpty(PermissionsPassword)) { EnableConfirm = false; } if (NeedPermissionsPassword && (!((bool)IsRestrictCopying || (bool)IsRestrictPrinting))) { EnableConfirm = false; } } #endregion #region 逻辑函数 public void DisplayPassword(object e) { var chk = e as CheckBox; if (chk != null) { if (chk.Name == "DisplayOpenPasswordChk") { if (chk.IsChecked == true) { OpenPasswordVisibility = Visibility.Collapsed; } else { OpenPasswordVisibility = Visibility.Visible; } } else { if (chk.IsChecked == true) { PermissionsPasswordVisibility = Visibility.Collapsed; } else { PermissionsPasswordVisibility = Visibility.Visible; } } } } public void SetOpenPassword(object e) { var chk = e as CheckBox; if (chk != null) { IsOpenPasswordDisplayed = false; OpenPasswordVisibility = Visibility.Visible; } CheckEnableConfirmEncrypt(); } public void SetPermissionsPassword(object e) { var chk = e as CheckBox; if (chk != null) { IsPermissionsPasswordDisplayed = false; PermissionsPasswordVisibility = Visibility.Visible; if (chk.IsChecked == true) { IsRestrictPrinting = true; IsRestrictCopying = true; } else { IsRestrictPrinting = false; IsRestrictCopying = false; } } CheckEnableConfirmEncrypt(); } public void SetRestrict(object e) { var chk = e as CheckBox; if (chk != null) { if ((bool)chk.IsChecked) { if (chk.Tag.ToString() == "Printing") { enumPermissionsMod |= EnumPermissionsMod.StatusRestrictPrinting; } else if (chk.Tag.ToString() == "Copying") { enumPermissionsMod |= EnumPermissionsMod.StatusRestrictCopying; } } else { if (chk.Tag.ToString() == "Printing") { enumPermissionsMod -= EnumPermissionsMod.StatusRestrictPrinting; } else if (chk.Tag.ToString() == "Copying") { enumPermissionsMod -= EnumPermissionsMod.StatusRestrictCopying; } } } CheckEnableConfirmEncrypt(); } public void ConfirmEncrypt() { FolderBrowserDialog dlg = new FolderBrowserDialog(); string saveSelectedPath = ""; if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { saveSelectedPath = dlg.SelectedPath.Trim(); saveSelectedPath = saveSelectedPath + "\\"; } else { return; } HomePageBatchProcessingDialogModel.closeDialog = true; SafetyGridIsEnabled = "False"; string openPassword = ""; string permissionsPassword = ""; CPDFPermissionsInfo permissionsInfo = new CPDFPermissionsInfo(); if (NeedOpenPassword) { if (!string.IsNullOrEmpty(OpenPassword)) { openPassword = OpenPassword; } } if (NeedPermissionsPassword) { if (!string.IsNullOrEmpty(PermissionsPassword)) { permissionsPassword = PermissionsPassword; permissionsInfo = CreatePermissionsInfo(enumPermissionsMod); } } fileNamesIndex = 0; foreach (var filename in fileNames) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "wait"; CPDFDocument document = CPDFDocument.InitWithFilePath(filename); if(document == null) { PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error"; fileNamesIndex++; continue; } if (document.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(ParameterNames.PassWord) != null) { document.UnlockWithPassword(e.Parameters.GetValue(ParameterNames.PassWord).ToString()); } } }); if (document.IsLocked) { //未成功解密文档时,释放Document对象,返回 PDFDataTable.Rows[fileNamesIndex]["FileState"] = "error"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "error"; fileNamesIndex++; continue; } } //FileInfo fileinfo = new FileInfo(filename); //string file_size = (((float)fileinfo.Length) / 1024).ToString() + " K"; document.Encrypt(openPassword, permissionsPassword, permissionsInfo); document.WriteToFilePath(CommonHelper.CreateFilePath(saveSelectedPath + document.FileName + "SetPassword")); PDFDataTable.Rows[fileNamesIndex]["FileState"] = "complete"; PDFCurrentDataTable.Rows[fileNamesIndex]["FileState"] = "complete"; fileNamesIndex++; document.Release(); } SafetyGridIsEnabled = "True"; HomePageBatchProcessingDialogModel.closeDialog = false; } #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("wait"); } } public void addPDFFiles(string filename) { FileNameNumber = fileNames.Count; fileNames.Add(filename); RemoveExcess(ref fileNames); SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("wait"); } /// /// 添加已打开PDF文件 /// public void addOpenedPDFFiles() { FileNameNumber = fileNames.Count; foreach (var filename in App.OpenedFileList) { fileNames.Add(filename); } RemoveExcess(ref fileNames); SetSafetyGridIsEnabled = "True"; AddFileVisibility = Visibility.Collapsed; updateListview("wait"); } /// /// 删除重复的文件 /// 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("wait"); 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 = "wait") { 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("Explorer", "/select," + 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("wait"); } public void removepdffile(int index) { PDFCurrentDataTable.Rows.RemoveAt(index); fileNames.Remove(fileNames[index]); if (fileNames.Count < 1) { SetSafetyGridIsEnabled = "False"; AddFileVisibility = Visibility.Visible; } updateListview("wait"); } 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("wait"); } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } } }