PageNumberControl.xaml.cs 5.8 KB

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