CPDFPageInsertUI.xaml.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.Controls.Common;
  3. using ComPDFKit.Controls.Helper;
  4. using System;
  5. using System.ComponentModel;
  6. using System.Runtime.CompilerServices;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. namespace ComPDFKit.Controls.PDFControlUI
  10. {
  11. public partial class CPDFPageInsertUI : UserControl, INotifyPropertyChanged
  12. {
  13. private string password = string.Empty;
  14. private int _maxIndex;
  15. public int MaxIndex
  16. {
  17. get => _maxIndex;
  18. set
  19. {
  20. _maxIndex = value;
  21. PageTextBox.Maximum = _maxIndex;
  22. MaxPageTextBox.Text = _maxIndex.ToString();
  23. }
  24. }
  25. private int _customPageIndex = 1;
  26. public int CustomPageIndex
  27. {
  28. get => _customPageIndex;
  29. set
  30. {
  31. _customPageIndex = value;
  32. OnPropertyChanged();
  33. CustomPageLocationChange(value);
  34. }
  35. }
  36. public PageInsertLocation PageInsertLocation
  37. {
  38. set
  39. {
  40. if (value == PageInsertLocation.HomePage)
  41. {
  42. HomePageRadioButton.IsChecked = true;
  43. }
  44. else if (value == PageInsertLocation.GastricPage)
  45. {
  46. GastricPageRadioButton.IsChecked = true;
  47. }
  48. else if (value == PageInsertLocation.CustomPage)
  49. {
  50. CustomPageRadioButton.IsChecked = true;
  51. }
  52. }
  53. }
  54. public event PropertyChangedEventHandler PropertyChanged;
  55. public event EventHandler<string> InsertTypeChanged;
  56. public event EventHandler<string> SelectedFileChanged;
  57. public event EventHandler<string> PasswordChanged;
  58. public event EventHandler<string> TextChanged;
  59. public event EventHandler<int> InsertIndexChanged;
  60. public event EventHandler<double[]> PageSizeChanged;
  61. public event EventHandler CancelEvent;
  62. public event EventHandler InsertEvent;
  63. protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
  64. {
  65. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  66. }
  67. public CPDFPageInsertUI()
  68. {
  69. InitializeComponent();
  70. DataContext = this;
  71. }
  72. private void CancelButton_Click(object sender, RoutedEventArgs e)
  73. {
  74. CancelEvent?.Invoke(null, EventArgs.Empty);
  75. }
  76. private void InsertButton_Click(object sender, RoutedEventArgs e)
  77. {
  78. if (string.IsNullOrEmpty(PageTextBox.Text))
  79. {
  80. InsertIndexChanged?.Invoke(null, -1);
  81. }
  82. InsertEvent?.Invoke(null, EventArgs.Empty);
  83. }
  84. private void SelectFileButton_Click(object sender, RoutedEventArgs e)
  85. {
  86. string filePath = CommonHelper.GetExistedPathOrEmpty();
  87. if (filePath != string.Empty)
  88. {
  89. CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(filePath);
  90. if (pdfDoc.IsLocked)
  91. {
  92. PasswordWindow passwordWindow = new PasswordWindow();
  93. Window parentWindow = Window.GetWindow(this);
  94. passwordWindow.Owner = parentWindow;
  95. passwordWindow.InitDocument(pdfDoc);
  96. passwordWindow.DialogClosed -= PasswordWindow_DialogClosed;
  97. passwordWindow.DialogClosed += PasswordWindow_DialogClosed;
  98. passwordWindow.ShowDialog();
  99. if (password != string.Empty)
  100. {
  101. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  102. FilePathTextBox.Text = filePath;
  103. SelectedFileChanged?.Invoke(sender, filePath);
  104. }
  105. }
  106. else
  107. {
  108. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  109. FilePathTextBox.Text = filePath;
  110. PasswordChanged?.Invoke(sender, string.Empty);
  111. SelectedFileChanged?.Invoke(sender, filePath);
  112. }
  113. }
  114. }
  115. private void PasswordWindow_DialogClosed(object sender, PasswordEventArgs e)
  116. {
  117. if (e.DialogResult != string.Empty)
  118. {
  119. password = e.DialogResult;
  120. PasswordChanged?.Invoke(sender, e.DialogResult);
  121. }
  122. }
  123. private void PageInsertLocation_Click(object sender, RoutedEventArgs e)
  124. {
  125. var radioButton = sender as RadioButton;
  126. if (radioButton.Tag.ToString() == "HomePage")
  127. {
  128. InsertIndexChanged?.Invoke(sender, 0);
  129. }
  130. else if (radioButton.Tag.ToString() == "GastricPage")
  131. {
  132. InsertIndexChanged?.Invoke(sender, MaxIndex);
  133. }
  134. else
  135. {
  136. CustomPageLocationChange();
  137. }
  138. }
  139. public void CustomPageLocationChange(int index = -2)
  140. {
  141. if (index == -2 && !string.IsNullOrEmpty(PageTextBox.Text))
  142. {
  143. int.TryParse(PageTextBox.Text,out index);
  144. }
  145. if (index != -2)
  146. {
  147. if (PageLocationComboBox.SelectedIndex == 0)
  148. {
  149. InsertIndexChanged?.Invoke(null, index);
  150. }
  151. else
  152. {
  153. InsertIndexChanged?.Invoke(null, index - 1);
  154. }
  155. }
  156. else
  157. {
  158. InsertIndexChanged?.Invoke(null, -1);
  159. }
  160. }
  161. private void InsertTypeCheckBox_Click(object sender, RoutedEventArgs e)
  162. {
  163. var radioButton = sender as RadioButton;
  164. InsertTypeChanged?.Invoke(sender, radioButton.Tag.ToString());
  165. }
  166. private void PageLocationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  167. {
  168. CustomPageLocationChange();
  169. }
  170. private void WritableComboBoxControl_Loaded(object sender, RoutedEventArgs e)
  171. {
  172. WritableComboBoxControl.TextChanged -= WritableComboBoxControl_TextChanged;
  173. WritableComboBoxControl.TextChanged += WritableComboBoxControl_TextChanged;
  174. }
  175. private void WritableComboBoxControl_Unloaded(object sender, RoutedEventArgs e)
  176. {
  177. WritableComboBoxControl.TextChanged -= WritableComboBoxControl_TextChanged;
  178. }
  179. private void WritableComboBoxControl_TextChanged(object sender, string e)
  180. {
  181. TextChanged?.Invoke(sender, e);
  182. }
  183. private void PageSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  184. {
  185. ComboBoxItem selectedItem = (ComboBoxItem)PageSizeComboBox.SelectedItem;
  186. string selectedContent = selectedItem.Content.ToString();
  187. double[] doubles = new double[2];
  188. if (selectedContent == "A3")
  189. {
  190. doubles[0] = 297 * 2.54;
  191. doubles[1] = 420 * 2.54;
  192. }
  193. else if (selectedContent == "A4")
  194. {
  195. doubles[0] = 210 * 2.54;
  196. doubles[1] = 297 * 2.54;
  197. }
  198. else if (selectedContent == "A5")
  199. {
  200. doubles[0] = 148 * 2.54;
  201. doubles[1] = 210 * 2.54;
  202. }
  203. PageSizeChanged?.Invoke(sender, doubles);
  204. }
  205. }
  206. public enum PageInsertLocation
  207. {
  208. HomePage,
  209. GastricPage,
  210. CustomPage
  211. }
  212. }