RemoveWatermarkListControl.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Common;
  3. using Compdfkit_Tools.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.ComponentModel;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Forms;
  17. using System.Windows.Input;
  18. using System.Windows.Media;
  19. using System.Windows.Media.Imaging;
  20. using System.Windows.Navigation;
  21. using System.Windows.Shapes;
  22. using DataGrid = System.Windows.Controls.DataGrid;
  23. using UserControl = System.Windows.Controls.UserControl;
  24. namespace Compdfkit_Tools.PDFControl
  25. {
  26. /// <summary>
  27. /// Interaction logic for RemoveWatermarkListControl.xaml
  28. /// </summary>
  29. public partial class RemoveWatermarkListControl : UserControl, INotifyPropertyChanged
  30. {
  31. private int _fileNumText;
  32. public int FileNumText
  33. {
  34. get => _fileNumText;
  35. set
  36. {
  37. if (UpdateProper(ref _fileNumText, value))
  38. IsEnsure = _fileNumText > 0;
  39. }
  40. }
  41. private ObservableCollection<FileInfoWithRange> _fileInfoDataList;
  42. public ObservableCollection<FileInfoWithRange> FileInfoDataList
  43. {
  44. get { return _fileInfoDataList; }
  45. set
  46. {
  47. _fileInfoDataList = value;
  48. _fileInfoDataList.CollectionChanged += (sender, args) =>
  49. {
  50. FileNumText = _fileInfoDataList.Count;
  51. };
  52. }
  53. }
  54. public static readonly DependencyProperty IsEnsureProperty =
  55. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(RemoveWatermarkListControl), new PropertyMetadata(false));
  56. public bool IsEnsure
  57. {
  58. get => (bool)GetValue(IsEnsureProperty);
  59. private set
  60. {
  61. SetValue(IsEnsureProperty, value);
  62. }
  63. }
  64. public RemoveWatermarkListControl()
  65. {
  66. InitializeComponent();
  67. DataContext = this;
  68. }
  69. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  70. {
  71. FileInfoDataList = new ObservableCollection<FileInfoWithRange>();
  72. FileDataGrid.ItemsSource = FileInfoDataList;
  73. }
  74. public event PropertyChangedEventHandler PropertyChanged;
  75. protected virtual void OnPropertyChanged(string propertyName = null)
  76. {
  77. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  78. }
  79. protected bool UpdateProper<T>(ref T properValue,
  80. T newValue,
  81. [CallerMemberName] string properName = "")
  82. {
  83. if (object.Equals(properValue, newValue))
  84. return false;
  85. properValue = newValue;
  86. OnPropertyChanged(properName);
  87. return true;
  88. }
  89. private void AddFilesBtn_Click(object sender, RoutedEventArgs e)
  90. {
  91. var openFileDialog = new OpenFileDialog();
  92. openFileDialog.Multiselect = true;
  93. openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
  94. if (openFileDialog.ShowDialog() == DialogResult.OK)
  95. {
  96. foreach (string filePath in openFileDialog.FileNames)
  97. {
  98. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  99. if(document == null)
  100. {
  101. continue;
  102. }
  103. if (document.IsLocked)
  104. {
  105. PasswordWindow passwordWindow = new PasswordWindow();
  106. passwordWindow.InitDocument(document);
  107. passwordWindow.Owner = Window.GetWindow(this);
  108. passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
  109. passwordWindow.ShowDialog();
  110. if (document.IsLocked)
  111. {
  112. document.Release();
  113. continue;
  114. }
  115. }
  116. List<int> pageRangeList = new List<int>();
  117. for (int i = 0; i < document.PageCount; i++)
  118. {
  119. pageRangeList.Add(i + 1);
  120. }
  121. FileInfoDataList.Add(new FileInfoWithRange
  122. {
  123. Document = document,
  124. Name = document.FileName,
  125. Size = CommonHelper.GetFileSize(filePath),
  126. Path = document.FilePath,
  127. PageRangeList = pageRangeList
  128. });
  129. }
  130. }
  131. }
  132. private void RemoveBtn_Click(object sender, RoutedEventArgs e)
  133. {
  134. if (FileDataGrid.SelectedItems.Count == 0)
  135. {
  136. foreach(var item in FileInfoDataList)
  137. {
  138. item.Document.Release();
  139. }
  140. FileInfoDataList.Clear();
  141. return;
  142. }
  143. var selectedItems = FileDataGrid.SelectedItems;
  144. var selectedItemsList = selectedItems.Cast<FileInfoWithRange>().ToList();
  145. foreach (var item in selectedItemsList)
  146. {
  147. item.Document.Release();
  148. FileInfoDataList.Remove(item);
  149. }
  150. }
  151. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  152. {
  153. var dataGrid = sender as DataGrid;
  154. if (dataGrid != null)
  155. {
  156. RemoveBtn.Content = dataGrid.SelectedItems.Count > 0 ? LanguageHelper.SecurityManager.GetString("Button_RemoveSelected") : LanguageHelper.SecurityManager.GetString("Button_RemoveAll");
  157. }
  158. }
  159. private void FileDataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  160. {
  161. FileDataGrid.UnselectAll();
  162. }
  163. }
  164. }