CPDFPageInsertUI.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 = 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.ShowDialog();
  98. if (password != string.Empty)
  99. {
  100. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  101. FilePathTextBox.Text = filePath;
  102. SelectedFileChanged?.Invoke(sender, filePath);
  103. }
  104. }
  105. else
  106. {
  107. WritableComboBoxControl.MaxPageIndex = pdfDoc.PageCount;
  108. FilePathTextBox.Text = filePath;
  109. PasswordChanged?.Invoke(sender, string.Empty);
  110. SelectedFileChanged?.Invoke(sender, filePath);
  111. }
  112. }
  113. }
  114. private void PasswordWindow_DialogClosed(object sender, PasswordEventArgs e)
  115. {
  116. if (e.DialogResult != string.Empty)
  117. {
  118. password = e.DialogResult;
  119. PasswordChanged?.Invoke(sender, e.DialogResult);
  120. }
  121. }
  122. private void PageInsertLocation_Click(object sender, RoutedEventArgs e)
  123. {
  124. var radioButton = sender as RadioButton;
  125. if (radioButton.Tag.ToString() == "HomePage")
  126. {
  127. InsertIndexChanged?.Invoke(sender, 0);
  128. }
  129. else if (radioButton.Tag.ToString() == "GastricPage")
  130. {
  131. InsertIndexChanged?.Invoke(sender, MaxIndex);
  132. }
  133. else
  134. {
  135. CustomPageLocationChange();
  136. }
  137. }
  138. public void CustomPageLocationChange(int index = -2)
  139. {
  140. if (index == -2 && !string.IsNullOrEmpty(PageTextBox.Text))
  141. {
  142. index = int.Parse(PageTextBox.Text);
  143. }
  144. if (index != -2)
  145. {
  146. if (PageLocationComboBox.SelectedIndex == 0)
  147. {
  148. InsertIndexChanged?.Invoke(null, index);
  149. }
  150. else
  151. {
  152. InsertIndexChanged?.Invoke(null, index - 1);
  153. }
  154. }
  155. else
  156. {
  157. InsertIndexChanged?.Invoke(null, -1);
  158. }
  159. }
  160. private void InsertTypeCheckBox_Click(object sender, RoutedEventArgs e)
  161. {
  162. var radioButton = sender as RadioButton;
  163. InsertTypeChanged?.Invoke(sender, radioButton.Tag.ToString());
  164. }
  165. private void PageLocationComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  166. {
  167. CustomPageLocationChange();
  168. }
  169. private void WritableComboBoxControl_Loaded(object sender, RoutedEventArgs e)
  170. {
  171. WritableComboBoxControl.TextChanged += WritableComboBoxControl_TextChanged;
  172. }
  173. private void WritableComboBoxControl_Unloaded(object sender, RoutedEventArgs e)
  174. {
  175. WritableComboBoxControl.TextChanged -= WritableComboBoxControl_TextChanged;
  176. }
  177. private void WritableComboBoxControl_TextChanged(object sender, string e)
  178. {
  179. TextChanged?.Invoke(sender, e);
  180. }
  181. private void PageSizeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  182. {
  183. ComboBoxItem selectedItem = (ComboBoxItem)PageSizeComboBox.SelectedItem;
  184. string selectedContent = selectedItem.Content.ToString();
  185. double[] doubles = new double[2];
  186. if (selectedContent == "A3")
  187. {
  188. doubles[0] = 297 * 2.54;
  189. doubles[1] = 420 * 2.54;
  190. }
  191. else if (selectedContent == "A4")
  192. {
  193. doubles[0] = 210 * 2.54;
  194. doubles[1] = 297 * 2.54;
  195. }
  196. else if (selectedContent == "A5")
  197. {
  198. doubles[0] = 148 * 2.54;
  199. doubles[1] = 210 * 2.54;
  200. }
  201. PageSizeChanged?.Invoke(sender, doubles);
  202. }
  203. }
  204. public enum PageInsertLocation
  205. {
  206. HomePage,
  207. GastricPage,
  208. CustomPage
  209. }
  210. }