PageRangeDialog.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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", "", MessageBoxButton.OK, MessageBoxImage.Information);
  78. return;
  79. }
  80. WindowClosed?.Invoke(weakReference.Target, PageIndexList);
  81. this.Close();
  82. }
  83. private void CancelBtn_Click(object sender, RoutedEventArgs e)
  84. {
  85. WindowClosed?.Invoke(weakReference.Target, null);
  86. this.Close();
  87. }
  88. private void Window_Loaded(object sender, RoutedEventArgs e)
  89. {
  90. weakReference = new WeakReference(this);
  91. PreviewControl.InitPreview(fileInfo.Document);
  92. defaultPageList = CommonHelper.GetDefaultPageList(fileInfo.Document);
  93. PageIndexList = defaultPageList;
  94. pageCount = fileInfo.Document.PageCount;
  95. if (pageCount <= 1)
  96. {
  97. IsEvenEnable = false;
  98. }
  99. else
  100. {
  101. IsEvenEnable = true;
  102. }
  103. }
  104. private void RangeRdo_Checked(object sender, RoutedEventArgs e)
  105. {
  106. var radioButton = sender as RadioButton;
  107. if (radioButton != null && fileInfo != null)
  108. {
  109. canContinue = true;
  110. switch (radioButton.Tag)
  111. {
  112. case "All":
  113. PageIndexList = defaultPageList;
  114. break;
  115. case "Odd":
  116. PageIndexList = defaultPageList.Where(value => value % 2 != 0).ToList();
  117. break;
  118. case "Even":
  119. if (fileInfo.PageRangeList.Count > 1)
  120. {
  121. PageIndexList = defaultPageList.Where(value => value % 2 == 0).ToList();
  122. }
  123. break;
  124. case "Custom":
  125. List<int> list = new List<int>();
  126. canContinue = CommonHelper.GetPagesInRange(ref list, PageRange, fileInfo.Document.PageCount, new char[] { ',' }, new char[] { '-' }, false);
  127. if (canContinue)
  128. {
  129. PageIndexList = list;
  130. }
  131. else
  132. {
  133. PageIndexList = defaultPageList;
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. }
  141. public event PropertyChangedEventHandler PropertyChanged;
  142. protected virtual void OnPropertyChanged(string propertyName = null)
  143. {
  144. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  145. }
  146. protected bool UpdateProper<T>(ref T properValue,
  147. T newValue,
  148. [CallerMemberName] string properName = "")
  149. {
  150. if (object.Equals(properValue, newValue))
  151. return false;
  152. properValue = newValue;
  153. OnPropertyChanged(properName);
  154. return true;
  155. }
  156. }
  157. }