CPDFPageInsertUI.xaml.cs 7.5 KB

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