CPDFPageInsertUI.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using ComPDFKit.PDFDocument;
  2. using Compdfkit_Tools.Common;
  3. using Compdfkit_Tools.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_Tools.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.Maxium = _maxIndex;
  22. MaxPageTextBox.Text = _maxIndex.ToString();
  23. }
  24. }
  25. private int _customPageIndex;
  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. InsertEvent?.Invoke(null, EventArgs.Empty);
  79. }
  80. private void SelectFileButton_Click(object sender, RoutedEventArgs e)
  81. {
  82. string filePath = CommonHelper.GetExistedPathOrEmpty();
  83. if (filePath != string.Empty)
  84. {
  85. CPDFDocument pdfDoc=CPDFDocument.InitWithFilePath(filePath);
  86. if (pdfDoc.IsLocked)
  87. {
  88. PasswordWindow passwordWindow = new PasswordWindow();
  89. Window parentWindow = Window.GetWindow(this);
  90. passwordWindow.Owner = parentWindow;
  91. passwordWindow.InitDocument(pdfDoc);
  92. passwordWindow.DialogClosed += PasswordWindow_DialogClosed;
  93. passwordWindow.ShowDialog();
  94. if (password != string.Empty)
  95. {
  96. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  97. FilePathTextBox.Text = filePath;
  98. SelectedFileChanged?.Invoke(sender, filePath);
  99. }
  100. }
  101. else
  102. {
  103. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  104. FilePathTextBox.Text = filePath;
  105. PasswordChanged?.Invoke(sender, string.Empty);
  106. SelectedFileChanged?.Invoke(sender, filePath);
  107. }
  108. }
  109. }
  110. private void PasswordWindow_DialogClosed(object sender, PasswordEventArgs e)
  111. {
  112. if(e.DialogResult != string.Empty)
  113. {
  114. password = e.DialogResult;
  115. PasswordChanged?.Invoke(sender, e.DialogResult);
  116. }
  117. }
  118. private void PageInsertLocation_Click(object sender, RoutedEventArgs e)
  119. {
  120. var radioButton = sender as RadioButton;
  121. if (radioButton.Tag.ToString() == "HomePage")
  122. {
  123. InsertIndexChanged?.Invoke(sender, 0);
  124. }
  125. else if (radioButton.Tag.ToString() == "GastricPage")
  126. {
  127. InsertIndexChanged?.Invoke(sender, MaxIndex);
  128. }
  129. else
  130. {
  131. CustomPageLocationChange();
  132. }
  133. }
  134. public void CustomPageLocationChange(int index = -2)
  135. {
  136. if (index == -2 && PageTextBox.Text != null)
  137. {
  138. index = int.Parse(PageTextBox.Text);
  139. }
  140. if (PageTextBox.Text != string.Empty)
  141. {
  142. if (PageLocationComboBox.SelectedIndex == 0)
  143. {
  144. InsertIndexChanged?.Invoke(null, index);
  145. }
  146. else
  147. {
  148. InsertIndexChanged?.Invoke(null, index - 1);
  149. }
  150. }
  151. else
  152. {
  153. InsertIndexChanged?.Invoke(null, -1);
  154. }
  155. }
  156. private void InsertTypeCheckBox_Click(object sender, RoutedEventArgs e)
  157. {
  158. var radioButton = sender as RadioButton;
  159. InsertTypeChanged?.Invoke(sender, radioButton.Tag.ToString());
  160. }
  161. private void PageLocationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  162. {
  163. CustomPageLocationChange();
  164. }
  165. private void WritableComboBoxControl_Loaded(object sender, RoutedEventArgs e)
  166. {
  167. WritableComboBoxControl.TextChanged += WritableComboBoxControl_TextChanged;
  168. }
  169. private void WritableComboBoxControl_Unloaded(object sender, RoutedEventArgs e)
  170. {
  171. WritableComboBoxControl.TextChanged -= WritableComboBoxControl_TextChanged;
  172. }
  173. private void WritableComboBoxControl_TextChanged(object sender, string e)
  174. {
  175. TextChanged?.Invoke(sender, e);
  176. }
  177. private void PageSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  178. {
  179. ComboBoxItem selectedItem = (ComboBoxItem)PageSizeComboBox.SelectedItem;
  180. string selectedContent = selectedItem.Content.ToString();
  181. double[] doubles = new double[2];
  182. if (selectedContent == "A3")
  183. {
  184. doubles[0] = 297 * 2.54;
  185. doubles[1] = 420 * 2.54;
  186. }
  187. else if (selectedContent == "A4")
  188. {
  189. doubles[0] = 210 * 2.54;
  190. doubles[1] = 297 * 2.54;
  191. }
  192. else if (selectedContent == "A5")
  193. {
  194. doubles[0] = 148 * 2.54;
  195. doubles[1] = 210 * 2.54;
  196. }
  197. PageSizeChanged?.Invoke(sender, doubles);
  198. }
  199. }
  200. public enum PageInsertLocation
  201. {
  202. HomePage,
  203. GastricPage,
  204. CustomPage
  205. }
  206. }