CPDFScalingControl.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. pdfViewer.UpDataRenderFrame();
  105. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  106. }
  107. }
  108. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  109. {
  110. if (ViewControl == null || ViewControl.PDFViewTool == null)
  111. {
  112. return;
  113. }
  114. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  115. if (pdfViewer == null)
  116. {
  117. return;
  118. }
  119. if (e == "Actual size")
  120. {
  121. pdfViewer.SetFitMode(FitModes.FitOriginal);
  122. }
  123. else if (e == "Suitable width")
  124. {
  125. pdfViewer.SetFitMode(FitModes.FitWidth);
  126. }
  127. else if (e == "Single page size")
  128. {
  129. pdfViewer.SetFitMode(FitModes.FitHeight);
  130. }
  131. else
  132. {
  133. pdfViewer.SetZoom(double.Parse(e) / 100);
  134. }
  135. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  136. pdfViewer.UpDataRenderFrame();
  137. }
  138. public void SetZoomTextBoxText(string value)
  139. {
  140. CPDFScalingUI.SetZoomTextBoxText(value);
  141. }
  142. private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
  143. {
  144. if (ViewControl == null || ViewControl.PDFViewTool == null)
  145. {
  146. return;
  147. }
  148. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  149. if (pdfViewer == null)
  150. {
  151. return;
  152. }
  153. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  154. }
  155. }
  156. }