CPDFScalingControl.xaml.cs 6.7 KB

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