CPDFScalingControl.xaml.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.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.MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler;
  34. ViewControl.FocusPDFViewToolChanged -= ViewControl_FocusPDFViewToolChanged;
  35. CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent;
  36. CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent;
  37. CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent;
  38. CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent;
  39. ViewControl.MouseWheelZoomHandler += PDFControl_MouseWheelZoomHandler;
  40. ViewControl.FocusPDFViewToolChanged += ViewControl_FocusPDFViewToolChanged;
  41. }
  42. private void ViewControl_FocusPDFViewToolChanged(object sender, EventArgs e)
  43. {
  44. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  45. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  46. }
  47. private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e)
  48. {
  49. CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
  50. CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
  51. CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
  52. CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
  53. ViewControl.MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler;
  54. }
  55. private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e)
  56. {
  57. if (ViewControl == null || ViewControl.PDFViewTool == null)
  58. {
  59. return;
  60. }
  61. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  62. if (pdfViewer == null)
  63. {
  64. return;
  65. }
  66. if (pdfViewer.GetZoom() < 3)
  67. {
  68. double newZoom = Math.Max(0.01, pdfViewer.GetZoom() - 0.1);
  69. pdfViewer.SetZoom(newZoom);
  70. pdfViewer.UpDateRenderFrame();
  71. }
  72. else if (pdfViewer.GetZoom() < 6)
  73. {
  74. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2);
  75. pdfViewer.UpDateRenderFrame();
  76. }
  77. else if (pdfViewer.GetZoom() >6)
  78. {
  79. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3);
  80. pdfViewer.UpDateRenderFrame();
  81. }
  82. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  83. }
  84. private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
  85. {
  86. if (ViewControl == null || ViewControl.PDFViewTool == null)
  87. {
  88. return;
  89. }
  90. CPDFViewer pdfViewer = ViewControl.GetCPDFViewer();
  91. if (pdfViewer == null)
  92. {
  93. return;
  94. }
  95. if (pdfViewer.GetZoom() < 3)
  96. {
  97. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1);
  98. pdfViewer.UpDateRenderFrame();
  99. }
  100. else if (pdfViewer.GetZoom() < 6)
  101. {
  102. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2);
  103. pdfViewer.UpDateRenderFrame();
  104. }
  105. else if (pdfViewer.GetZoom() <= 10)
  106. {
  107. double newZoom = Math.Max(10, pdfViewer.GetZoom() + 0.3);
  108. pdfViewer.SetZoom(newZoom);
  109. pdfViewer.UpDateRenderFrame();
  110. }
  111. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom()* 100)));
  112. }
  113. private void PDFScalingControl_SetScaleEvent(object sender, string e)
  114. {
  115. if (ViewControl == null || ViewControl.PDFViewTool == null)
  116. {
  117. return;
  118. }
  119. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  120. if (pdfViewer == null)
  121. {
  122. return;
  123. }
  124. if (!string.IsNullOrEmpty(e))
  125. {
  126. pdfViewer.SetZoom(double.Parse(e) / 100);
  127. pdfViewer.UpDateRenderFrame();
  128. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  129. }
  130. }
  131. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  132. {
  133. if (ViewControl == null || ViewControl.PDFViewTool == null)
  134. {
  135. return;
  136. }
  137. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  138. if (pdfViewer == null)
  139. {
  140. return;
  141. }
  142. if (e == "Actual size")
  143. {
  144. pdfViewer.SetFitMode(FitModes.FitOriginal);
  145. }
  146. else if (e == "Suitable width")
  147. {
  148. pdfViewer.SetFitMode(FitModes.FitWidth);
  149. }
  150. else if (e == "Single page size")
  151. {
  152. pdfViewer.SetFitMode(FitModes.FitHeight);
  153. }
  154. else
  155. {
  156. pdfViewer.SetZoom(double.Parse(e) / 100);
  157. }
  158. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  159. pdfViewer.UpDateRenderFrame();
  160. }
  161. public void SetZoomTextBoxText(string value)
  162. {
  163. CPDFScalingUI.SetZoomTextBoxText(value);
  164. }
  165. private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
  166. {
  167. if (ViewControl == null || ViewControl.PDFViewTool == null)
  168. {
  169. return;
  170. }
  171. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  172. if (pdfViewer == null)
  173. {
  174. return;
  175. }
  176. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  177. }
  178. }
  179. }