PageNumberControl.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using ComPDFKitViewer.PdfViewer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. namespace Compdfkit_Tools.Common
  8. {
  9. public partial class PageNumberControl : UserControl
  10. {
  11. private CPDFViewer pdfView;
  12. public PageNumberControl()
  13. {
  14. InitializeComponent();
  15. }
  16. public void InitWithPDFViewer(CPDFViewer newPDFView)
  17. {
  18. if (pdfView != newPDFView)
  19. {
  20. if(pdfView != null)
  21. {
  22. pdfView.InfoChanged -= PdfView_InfoChanged;
  23. }
  24. pdfView = newPDFView;
  25. if(pdfView != null)
  26. {
  27. pdfView.InfoChanged += PdfView_InfoChanged;
  28. }
  29. }
  30. }
  31. private void PdfView_InfoChanged(object sender, KeyValuePair<string, object> e)
  32. {
  33. if (e.Key == "PageNum")
  34. {
  35. PageRangeText.Text = string.Format("{0}/{1}", e.Value, pdfView.Document.PageCount);
  36. }
  37. }
  38. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  39. {
  40. SetPageRangeVisible();
  41. if (pdfView.ModeView == ComPDFKitViewer.ViewMode.Single || pdfView.ModeView == ComPDFKitViewer.ViewMode.SingleContinuous)
  42. {
  43. pdfView?.GoToPage(pdfView.CurrentIndex + 1);
  44. }
  45. else
  46. {
  47. pdfView?.GoToPage(pdfView.CurrentIndex + 2);
  48. }
  49. }
  50. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  51. {
  52. SetPageRangeVisible();
  53. if (pdfView.ModeView == ComPDFKitViewer.ViewMode.Single || pdfView.ModeView == ComPDFKitViewer.ViewMode.SingleContinuous)
  54. {
  55. pdfView?.GoToPage(pdfView.CurrentIndex - 1);
  56. }
  57. else
  58. {
  59. pdfView?.GoToPage(pdfView.CurrentIndex - 2);
  60. }
  61. }
  62. private void PageRangeText_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  63. {
  64. PageRangeText.Visibility = Visibility.Collapsed;
  65. if(pdfView!=null && pdfView.Document!=null)
  66. {
  67. PageInputText.Text =string.Format("{0}",pdfView.CurrentIndex+1);
  68. }
  69. PageInputText.Visibility = Visibility.Visible;
  70. PageInputText.Focus();
  71. }
  72. private void PageInputText_LostFocus(object sender, RoutedEventArgs e)
  73. {
  74. SetInputPage();
  75. SetPageRangeVisible();
  76. }
  77. private void PageInputText_KeyDown(object sender, KeyEventArgs e)
  78. {
  79. if(e.Key == Key.Enter || e.Key== Key.Escape)
  80. {
  81. SetInputPage();
  82. SetPageRangeVisible();
  83. }
  84. }
  85. private void SetPageRangeVisible()
  86. {
  87. PageRangeText.Visibility = Visibility.Visible;
  88. PageInputText.Visibility = Visibility.Collapsed;
  89. }
  90. private void SetInputPage()
  91. {
  92. if (pdfView != null && pdfView.Document != null)
  93. {
  94. if (int.TryParse(PageInputText.Text, out int newPageNum))
  95. {
  96. newPageNum = Math.Min(pdfView.Document.PageCount, newPageNum);
  97. newPageNum = Math.Max(1, newPageNum);
  98. pdfView?.GoToPage(newPageNum-1);
  99. }
  100. }
  101. }
  102. private void PageInputText_PreviewKeyDown(object sender, KeyEventArgs e)
  103. {
  104. List<Key> allowKeys = new List<Key>()
  105. {
  106. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  107. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  108. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right
  109. };
  110. if(allowKeys.Contains(e.Key)==false)
  111. {
  112. e.Handled = true;
  113. }
  114. }
  115. private void PageInputText_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  116. {
  117. try
  118. {
  119. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  120. {
  121. string checkText=Clipboard.GetText();
  122. if (int.TryParse(checkText, out int value))
  123. {
  124. e.CanExecute = true;
  125. }
  126. e.Handled=true;
  127. }
  128. }
  129. catch(Exception ex)
  130. {
  131. }
  132. }
  133. }
  134. }