using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using Compdfkit_Tools.Common; using ComPDFKit.PDFDocument; namespace Compdfkit_Tools.PDFControl { public partial class EncryptionDialog : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public bool CanEncrypt => FileListControl.FileNum > 0 && SetEncryptionControl.ValidSetting; private CPDFDocument document; public EncryptionDialog() { InitializeComponent(); DataContext = this; FileListControl.FileNumChanged += (sender, args) => { OnPropertyChanged(nameof(CanEncrypt)); }; SetEncryptionControl.EncryptionSettingChanged += (sender, args) => { OnPropertyChanged(nameof(CanEncrypt)); }; } private void ButtonCancel_Click(object sender, RoutedEventArgs e) { this.Close(); } private void ButtonEncrypt_Click(object sender, RoutedEventArgs e) { var dialog = new FolderBrowserDialog(); dialog.ShowDialog(); var savePath = dialog.SelectedPath; if (string.IsNullOrEmpty(savePath)) return; var permissionsInfo = new CPDFPermissionsInfo(); string userPassword = string.Empty; string ownerPassword = string.Empty; if (SetEncryptionControl.IsOwnerPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.OwnerPassword)) { permissionsInfo.AllowsPrinting = SetEncryptionControl.IsAllowPrint; permissionsInfo.AllowsCopying = SetEncryptionControl.IsAllowCopy; ownerPassword = SetEncryptionControl.OwnerPassword; } if(SetEncryptionControl.IsUserPasswordEnabled && !string.IsNullOrEmpty(SetEncryptionControl.UserPassword)) { userPassword = SetEncryptionControl.UserPassword; } foreach (var fileInfoData in FileListControl.FileInfoDataList) { document = CPDFDocument.InitWithFilePath(fileInfoData.Location); if (document.IsLocked) { PasswordWindow window = new PasswordWindow(); window.InitWithDocument(document); window.Owner = this; window.ShowDialog(); if (document.IsLocked) { continue; } } document.Encrypt(userPassword, ownerPassword, permissionsInfo); document.WriteToFilePath(savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf"); document.Release(); } } protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } }