CPDFScalingControl.xaml.cs 7.3 KB

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