FileGridListWithPageRangeControl.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 DataGridCell = System.Windows.Controls.DataGridCell;
  24. using UserControl = System.Windows.Controls.UserControl;
  25. namespace Compdfkit_Tools.PDFControl
  26. {
  27. public class FileInfoWithRange : INotifyPropertyChanged
  28. {
  29. public CPDFDocument Document;
  30. public string Name { get; set; }
  31. public string Path { get; set; }
  32. private string _pageRange;
  33. public string PageRange { get => _pageRange; set => UpdateProper(ref _pageRange, value); }
  34. public string Size { get; set; }
  35. private List<int> _pageRangeList = new List<int>();
  36. public List<int> PageRangeList
  37. {
  38. get => _pageRangeList;
  39. set
  40. {
  41. _pageRangeList = value;
  42. PageRange = CommonHelper.GetPageParmFromList(PageRangeList);
  43. }
  44. }
  45. public event PropertyChangedEventHandler PropertyChanged;
  46. protected virtual void OnPropertyChanged(string propertyName = null)
  47. {
  48. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  49. }
  50. protected bool UpdateProper<T>(ref T properValue,
  51. T newValue,
  52. [CallerMemberName] string properName = "")
  53. {
  54. if (object.Equals(properValue, newValue))
  55. return false;
  56. properValue = newValue;
  57. OnPropertyChanged(properName);
  58. return true;
  59. }
  60. }
  61. /// <summary>
  62. /// Interaction logic for FileGridListWithPageRangeControl.xaml
  63. /// </summary>
  64. public partial class FileGridListWithPageRangeControl : UserControl, INotifyPropertyChanged
  65. {
  66. public static readonly DependencyProperty IsEnsureProperty =
  67. DependencyProperty.Register(nameof(IsEnsure), typeof(bool), typeof(FileGridListWithPageRangeControl), new PropertyMetadata(false));
  68. public bool IsEnsure
  69. {
  70. get => (bool)GetValue(IsEnsureProperty);
  71. private set
  72. {
  73. SetValue(IsEnsureProperty, value);
  74. }
  75. }
  76. private int _fileNumText;
  77. public int FileNumText
  78. {
  79. get => _fileNumText;
  80. set
  81. {
  82. if (UpdateProper(ref _fileNumText, value))
  83. IsEnsure = _fileNumText > 0;
  84. }
  85. }
  86. private ObservableCollection<FileInfoWithRange> _fileInfoDataList;
  87. public ObservableCollection<FileInfoWithRange> FileInfoDataList
  88. {
  89. get { return _fileInfoDataList; }
  90. set
  91. {
  92. _fileInfoDataList = value;
  93. _fileInfoDataList.CollectionChanged += (sender, args) =>
  94. {
  95. FileNumText = _fileInfoDataList.Count;
  96. };
  97. }
  98. }
  99. public FileGridListWithPageRangeControl()
  100. {
  101. this.DataContext = this;
  102. InitializeComponent();
  103. }
  104. public event PropertyChangedEventHandler PropertyChanged;
  105. protected virtual void OnPropertyChanged(string propertyName = null)
  106. {
  107. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  108. }
  109. protected bool UpdateProper<T>(ref T properValue,
  110. T newValue,
  111. [CallerMemberName] string properName = "")
  112. {
  113. if (object.Equals(properValue, newValue))
  114. return false;
  115. properValue = newValue;
  116. OnPropertyChanged(properName);
  117. return true;
  118. }
  119. private void AddFilesBtn_Click(object sender, RoutedEventArgs e)
  120. {
  121. var openFileDialog = new OpenFileDialog();
  122. openFileDialog.Multiselect = true;
  123. openFileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
  124. if (openFileDialog.ShowDialog() == DialogResult.OK)
  125. {
  126. foreach (string filePath in openFileDialog.FileNames)
  127. {
  128. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  129. if (document.IsLocked)
  130. {
  131. PasswordWindow passwordWindow = new PasswordWindow();
  132. passwordWindow.InitWithDocument(document);
  133. passwordWindow.Owner = Window.GetWindow(this);
  134. passwordWindow.PasswordDialog.SetShowText(filePath + " is encrypted.");
  135. passwordWindow.ShowDialog();
  136. if (document.IsLocked)
  137. {
  138. document.Release();
  139. continue;
  140. }
  141. }
  142. List<int> pageRangeList = new List<int>();
  143. for (int i = 0; i < document.PageCount; i++)
  144. {
  145. pageRangeList.Add(i + 1);
  146. }
  147. FileInfoDataList.Add(new FileInfoWithRange
  148. {
  149. Document = document,
  150. Name = document.FileName,
  151. Size = CommonHelper.GetFileSize(filePath),
  152. Path = document.FilePath,
  153. PageRangeList = pageRangeList
  154. });
  155. }
  156. }
  157. }
  158. private void RemoveBtn_Click(object sender, RoutedEventArgs e)
  159. {
  160. }
  161. private void PageRangeBtn_Click(object sender, RoutedEventArgs e)
  162. {
  163. PageRangeDialog pageRangeDialog = new PageRangeDialog();
  164. if (FileDataGrid.SelectedItems[0] is FileInfoWithRange fileInfo)
  165. {
  166. pageRangeDialog.InitWithFileInfo(fileInfo);
  167. }
  168. pageRangeDialog.Owner = Window.GetWindow(this);
  169. pageRangeDialog.ShowDialog();
  170. }
  171. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  172. {
  173. FileInfoDataList = new ObservableCollection<FileInfoWithRange>();
  174. FileDataGrid.ItemsSource = FileInfoDataList;
  175. }
  176. private void FileDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
  177. {
  178. var dataGrid = sender as DataGrid;
  179. if (dataGrid != null)
  180. {
  181. RemoveBtn.Content = dataGrid.SelectedItems.Count > 0 ? "Remove" : "Remove All";
  182. PageRangeBtn.Visibility = dataGrid.SelectedItems.Count == 1 ? Visibility.Visible : Visibility.Collapsed;
  183. }
  184. }
  185. private void FileDataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  186. {
  187. FileDataGrid.UnselectAll();
  188. }
  189. }
  190. }