CPDFPageInsertUI.xaml.cs 7.4 KB

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