PageNumberControl.xaml.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using ComPDFKit.Tool;
  2. using Compdfkit_Tools.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_Tools.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.PDFViewTool.DrawChanged -= PdfView_InfoChanged;
  25. }
  26. pdfView = newPDFView;
  27. if (pdfView != null)
  28. {
  29. pdfView.PDFViewTool.DrawChanged += PdfView_InfoChanged;
  30. }
  31. }
  32. }
  33. private void PdfView_InfoChanged(object sender, EventArgs e)
  34. {
  35. if (sender is CPDFViewerTool)
  36. {
  37. PageRangeText.Text = string.Format("{0}/{1}", (sender as CPDFViewerTool).GetCPDFViewer().CurrentRenderFrame.PageIndex+1, (sender as CPDFViewerTool).GetCPDFViewer().GetDocument().PageCount);
  38. }
  39. }
  40. private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  41. {
  42. SetPageRangeVisible();
  43. if (pdfView.PDFViewTool.GetCPDFViewer().GetViewMode() == ComPDFKitViewer.ViewModes.Single || pdfView.PDFViewTool.GetCPDFViewer().GetViewMode() == ComPDFKitViewer.ViewModes.SingleContinuous)
  44. {
  45. pdfView.PDFViewTool.GetCPDFViewer().GoToPage(pdfView.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex + 1,new Point(0,0));
  46. }
  47. else
  48. {
  49. pdfView.PDFViewTool.GetCPDFViewer().GoToPage(pdfView.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex + 2, new Point(0, 0));
  50. }
  51. }
  52. private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  53. {
  54. SetPageRangeVisible();
  55. if (pdfView.PDFViewTool.GetCPDFViewer().GetViewMode() == ComPDFKitViewer.ViewModes.Single || pdfView.PDFViewTool.GetCPDFViewer().GetViewMode() == ComPDFKitViewer.ViewModes.SingleContinuous)
  56. {
  57. pdfView.PDFViewTool.GetCPDFViewer().GoToPage(pdfView.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex - 1, new Point(0, 0));
  58. }
  59. else
  60. {
  61. pdfView.PDFViewTool.GetCPDFViewer().GoToPage(pdfView.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex - 2, new Point(0, 0));
  62. }
  63. }
  64. private void PageRangeText_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  65. {
  66. PageRangeText.Visibility = Visibility.Collapsed;
  67. if(pdfView!=null && pdfView.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  68. {
  69. PageInputText.Text =string.Format("{0}", pdfView.PDFViewTool.GetCPDFViewer().CurrentRenderFrame.PageIndex + 1);
  70. }
  71. PageInputText.Visibility = Visibility.Visible;
  72. PageInputText.Focus();
  73. }
  74. private void PageInputText_LostFocus(object sender, RoutedEventArgs e)
  75. {
  76. SetInputPage();
  77. SetPageRangeVisible();
  78. }
  79. private void PageInputText_KeyDown(object sender, KeyEventArgs e)
  80. {
  81. if(e.Key == Key.Enter || e.Key== Key.Escape)
  82. {
  83. SetInputPage();
  84. SetPageRangeVisible();
  85. }
  86. }
  87. private void SetPageRangeVisible()
  88. {
  89. PageRangeText.Visibility = Visibility.Visible;
  90. PageInputText.Visibility = Visibility.Collapsed;
  91. }
  92. private void SetInputPage()
  93. {
  94. if (pdfView != null && pdfView.PDFViewTool.GetCPDFViewer().GetDocument() != null)
  95. {
  96. if (int.TryParse(PageInputText.Text, out int newPageNum))
  97. {
  98. newPageNum = Math.Min(pdfView.PDFViewTool.GetCPDFViewer().GetDocument().PageCount, newPageNum);
  99. newPageNum = Math.Max(1, newPageNum);
  100. pdfView.PDFViewTool.GetCPDFViewer().GoToPage(newPageNum-1,new Point(0,0));
  101. }
  102. }
  103. }
  104. private void PageInputText_PreviewKeyDown(object sender, KeyEventArgs e)
  105. {
  106. List<Key> allowKeys = new List<Key>()
  107. {
  108. Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
  109. Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
  110. Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right
  111. };
  112. if(allowKeys.Contains(e.Key)==false)
  113. {
  114. e.Handled = true;
  115. }
  116. }
  117. private void PageInputText_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  118. {
  119. try
  120. {
  121. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  122. {
  123. string checkText=Clipboard.GetText();
  124. if (int.TryParse(checkText, out int value))
  125. {
  126. e.CanExecute = true;
  127. }
  128. e.Handled=true;
  129. }
  130. }
  131. catch(Exception ex)
  132. {
  133. }
  134. }
  135. }
  136. }