PageNumberControl.xaml.cs 5.7 KB

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