123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- 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
- {
- private int _currentFileNum;
- public int CurrentFileNum
- {
- get => _currentFileNum;
- set => UpdateProper(ref _currentFileNum, value);
- }
- private int _fileNum;
- public int FileNum
- {
- get => _fileNum;
- set => UpdateProper(ref _fileNum,value);
- }
-
- private bool isEnsure;
- public event PropertyChangedEventHandler PropertyChanged;
- public bool CanEncrypt
- {
- get { return FileListControl.FileInfoDataList.Count > 0 && SetEncryptionControl.IsSettingValid; }
- }
- private CPDFDocument document;
- private List<string> failedList = new List<string>();
- public EncryptionDialog()
- {
- InitializeComponent();
- DataContext = this;
- CurrentFileNum = 0;
- }
- 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;
-
- CurrentFileNum = 0;
- 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)
- {
- CurrentFileNum++;
- document = CPDFDocument.InitWithFilePath(fileInfoData.Location);
- if (document.IsLocked)
- {
- PasswordWindow window = new PasswordWindow();
- window.InitWithDocument(document);
- window.Owner = this;
- window.PasswordDialog.SetShowText(fileInfoData.FileName + " is encrypted.");
- window.ShowDialog();
- if (document.IsLocked)
- {
- failedList.Add(fileInfoData.Location);
- continue;
- }
- }
- var fullSavePath = savePath + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(fileInfoData.FileName) + "_Encrypted.pdf";
- document.Encrypt(userPassword, ownerPassword, permissionsInfo, (CPDFDocumentEncryptionLevel)SetEncryptionControl.CryptographicLevel);
- if (!document.WriteToFilePath(fullSavePath))
- {
- failedList.Add(fileInfoData.Location);
- }
- document.Release();
- }
- if(failedList.Count > 0)
- {
- System.Windows.MessageBox.Show("Failed to encrypt the following files:\n" + string.Join("\n", failedList));
- }
- if(failedList.Count < FileListControl.FileInfoDataList.Count)
- {
- System.Diagnostics.Process.Start(savePath);
- }
- this.Close();
- }
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
-
- protected void UpdateProper<T>(ref T properValue,
- T newValue,
- [CallerMemberName] string properName = "")
- {
- if (object.Equals(properValue, newValue))
- return;
- properValue = newValue;
- OnPropertyChanged(properName);
- }
- }
- }
|