CPDFScalingControl.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using ComPDFKitViewer;
  2. using System;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Input;
  6. namespace Compdfkit_Tools.PDFControl
  7. {
  8. public partial class CPDFScalingControl : UserControl
  9. {
  10. public PDFViewControl ViewControl;
  11. public CPDFScalingControl()
  12. {
  13. InitializeComponent();
  14. }
  15. public void InitWithPDFViewer(PDFViewControl viewControl)
  16. {
  17. ViewControl = viewControl;
  18. }
  19. private void PDFControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e)
  20. {
  21. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  22. {
  23. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  24. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  25. }
  26. }
  27. private void PDFScalingControl_Loaded(object sender, RoutedEventArgs e)
  28. {
  29. CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent;
  30. CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent;
  31. CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent;
  32. CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent;
  33. ViewControl.PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFControl_MouseWheelZoomHandler;
  34. }
  35. private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e)
  36. {
  37. CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
  38. CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
  39. CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
  40. CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
  41. ViewControl.PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler;
  42. }
  43. private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e)
  44. {
  45. if (ViewControl == null || ViewControl.PDFViewTool == null)
  46. {
  47. return;
  48. }
  49. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  50. if (pdfViewer == null)
  51. {
  52. return;
  53. }
  54. if (pdfViewer.GetZoom() < 3)
  55. {
  56. double newZoom = Math.Max(0.01, pdfViewer.GetZoom() - 0.1);
  57. pdfViewer.SetZoom(newZoom);
  58. pdfViewer.UpDateRenderFrame();
  59. }
  60. else if (pdfViewer.GetZoom() < 6)
  61. {
  62. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2);
  63. pdfViewer.UpDateRenderFrame();
  64. }
  65. else if (pdfViewer.GetZoom() >6)
  66. {
  67. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3);
  68. pdfViewer.UpDateRenderFrame();
  69. }
  70. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  71. }
  72. private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
  73. {
  74. if (ViewControl == null || ViewControl.PDFViewTool == null)
  75. {
  76. return;
  77. }
  78. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  79. if (pdfViewer == null)
  80. {
  81. return;
  82. }
  83. if (pdfViewer.GetZoom() < 3)
  84. {
  85. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1);
  86. pdfViewer.UpDateRenderFrame();
  87. }
  88. else if (pdfViewer.GetZoom() < 6)
  89. {
  90. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2);
  91. pdfViewer.UpDateRenderFrame();
  92. }
  93. else if (pdfViewer.GetZoom() <= 10)
  94. {
  95. double newZoom = Math.Max(10, pdfViewer.GetZoom() + 0.3);
  96. pdfViewer.SetZoom(newZoom);
  97. pdfViewer.UpDateRenderFrame();
  98. }
  99. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom()* 100)));
  100. }
  101. private void PDFScalingControl_SetScaleEvent(object sender, string e)
  102. {
  103. if (ViewControl == null || ViewControl.PDFViewTool == null)
  104. {
  105. return;
  106. }
  107. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  108. if (pdfViewer == null)
  109. {
  110. return;
  111. }
  112. if (!string.IsNullOrEmpty(e))
  113. {
  114. pdfViewer.SetZoom(double.Parse(e) / 100);
  115. pdfViewer.UpDateRenderFrame();
  116. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  117. }
  118. }
  119. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  120. {
  121. if (ViewControl == null || ViewControl.PDFViewTool == null)
  122. {
  123. return;
  124. }
  125. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  126. if (pdfViewer == null)
  127. {
  128. return;
  129. }
  130. if (e == "Actual size")
  131. {
  132. pdfViewer.SetFitMode(FitModes.FitOriginal);
  133. }
  134. else if (e == "Suitable width")
  135. {
  136. pdfViewer.SetFitMode(FitModes.FitWidth);
  137. }
  138. else if (e == "Single page size")
  139. {
  140. pdfViewer.SetFitMode(FitModes.FitHeight);
  141. }
  142. else
  143. {
  144. pdfViewer.SetZoom(double.Parse(e) / 100);
  145. }
  146. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  147. pdfViewer.UpDateRenderFrame();
  148. }
  149. public void SetZoomTextBoxText(string value)
  150. {
  151. CPDFScalingUI.SetZoomTextBoxText(value);
  152. }
  153. private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
  154. {
  155. if (ViewControl == null || ViewControl.PDFViewTool == null)
  156. {
  157. return;
  158. }
  159. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  160. if (pdfViewer == null)
  161. {
  162. return;
  163. }
  164. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  165. }
  166. }
  167. }