CPDFScalingControl.xaml.cs 5.6 KB

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