PageRangeDialog.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using Compdfkit_Tools.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using RadioButton = System.Windows.Controls.RadioButton;
  9. namespace Compdfkit_Tools.PDFControl
  10. {
  11. /// <summary>
  12. /// Interaction logic for PageRangeDialog.xaml
  13. /// </summary>
  14. public partial class PageRangeDialog : Window, INotifyPropertyChanged
  15. {
  16. private bool canContinue = true;
  17. private int pageCount = 0;
  18. private List<int> defaultPageList = new List<int>();
  19. private List<int> _pageIndexList = new List<int>();
  20. public List<int> PageIndexList
  21. {
  22. get => _pageIndexList;
  23. set
  24. {
  25. _pageIndexList = value;
  26. PreviewControl.PageRangeList = _pageIndexList;
  27. }
  28. }
  29. private bool _isEvenEnable = false;
  30. public bool IsEvenEnable
  31. {
  32. get => _isEvenEnable;
  33. set
  34. {
  35. UpdateProper(ref _isEvenEnable, value);
  36. }
  37. }
  38. private string _pageRange;
  39. public string PageRange
  40. {
  41. get => _pageRange;
  42. set
  43. {
  44. if (fileInfo != null & UpdateProper(ref _pageRange, value))
  45. {
  46. List<int> list = new List<int>();
  47. canContinue = CommonHelper.GetPagesInRange(ref list, PageRange, fileInfo.Document.PageCount, new char[] { ',' }, new char[] { '-' });
  48. if (canContinue)
  49. {
  50. List<int> newList = list.Select(item => item + 1).ToList();
  51. PageIndexList = newList;
  52. }
  53. else
  54. {
  55. PageIndexList = fileInfo.PageRangeList;
  56. }
  57. }
  58. }
  59. }
  60. private FileInfoWithRange fileInfo;
  61. private WeakReference weakReference;
  62. public delegate void WindowClosedEventHandler(object sender, List<int> result);
  63. public event WindowClosedEventHandler WindowClosed;
  64. public PageRangeDialog()
  65. {
  66. this.DataContext = this;
  67. InitializeComponent();
  68. }
  69. public void InitWithFileInfo(FileInfoWithRange fileInfo)
  70. {
  71. this.fileInfo = fileInfo;
  72. }
  73. private void ConfirmBtn_Click(object sender, RoutedEventArgs e)
  74. {
  75. if (!canContinue)
  76. {
  77. MessageBox.Show("Please enter the right page range",
  78. LanguageHelper.CommonManager.GetString("Caption_Warning"), MessageBoxButton.OK, MessageBoxImage.Information);
  79. return;
  80. }
  81. WindowClosed?.Invoke(weakReference.Target, PageIndexList);
  82. this.Close();
  83. }
  84. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  85. {
  86. WindowClosed?.Invoke(weakReference.Target, null);
  87. this.Close();
  88. }
  89. protected override void OnClosed(EventArgs e)
  90. {
  91. fileInfo.Document.ReleasePages();
  92. base.OnClosed(e);
  93. }
  94. private void Window_Loaded(object sender, RoutedEventArgs e)
  95. {
  96. weakReference = new WeakReference(this);
  97. PreviewControl.InitPreview(fileInfo.Document);
  98. defaultPageList = CommonHelper.GetDefaultPageList(fileInfo.Document);
  99. PageIndexList = defaultPageList;
  100. pageCount = fileInfo.Document.PageCount;
  101. if (pageCount <= 1)
  102. {
  103. IsEvenEnable = false;
  104. }
  105. else
  106. {
  107. IsEvenEnable = true;
  108. }
  109. }
  110. private void RangeRdo_Checked(object sender, RoutedEventArgs e)
  111. {
  112. var radioButton = sender as RadioButton;
  113. if (radioButton != null && fileInfo != null)
  114. {
  115. canContinue = true;
  116. switch (radioButton.Tag)
  117. {
  118. case "All":
  119. PageIndexList = defaultPageList;
  120. break;
  121. case "Odd":
  122. PageIndexList = defaultPageList.Where(value => value % 2 != 0).ToList();
  123. break;
  124. case "Even":
  125. if (defaultPageList.Count > 1)
  126. {
  127. PageIndexList = defaultPageList.Where(value => value % 2 == 0).ToList();
  128. }
  129. break;
  130. case "Custom":
  131. List<int> list = new List<int>();
  132. canContinue = CommonHelper.GetPagesInRange(ref list, PageRange, fileInfo.Document.PageCount, new char[] { ',' }, new char[] { '-' }, false);
  133. if (canContinue)
  134. {
  135. // Increment each element in the list by 1
  136. for (int i = 0; i < list.Count; i++)
  137. {
  138. list[i]++;
  139. }
  140. PageIndexList = list;
  141. }
  142. else
  143. {
  144. PageIndexList = defaultPageList;
  145. }
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. }
  152. public event PropertyChangedEventHandler PropertyChanged;
  153. protected virtual void OnPropertyChanged(string propertyName = null)
  154. {
  155. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  156. }
  157. protected bool UpdateProper<T>(ref T properValue,
  158. T newValue,
  159. [CallerMemberName] string properName = "")
  160. {
  161. if (object.Equals(properValue, newValue))
  162. return false;
  163. properValue = newValue;
  164. OnPropertyChanged(properName);
  165. return true;
  166. }
  167. }
  168. }