CPDFScalingControl.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.1);
  46. }
  47. else if (pdfViewer.GetZoom() < 6)
  48. {
  49. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2);
  50. }
  51. else if (pdfViewer.GetZoom() <= 10)
  52. {
  53. pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3);
  54. }
  55. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  56. }
  57. private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
  58. {
  59. if (ViewControl == null || ViewControl.PDFViewTool == null)
  60. {
  61. return;
  62. }
  63. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  64. if (pdfViewer == null)
  65. {
  66. return;
  67. }
  68. if (pdfViewer.GetZoom() < 3)
  69. {
  70. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1);
  71. }
  72. else if (pdfViewer.GetZoom() < 6)
  73. {
  74. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2);
  75. }
  76. else if (pdfViewer.GetZoom() <= 10)
  77. {
  78. pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.3);
  79. }
  80. SetZoomTextBoxText(string.Format("{0}", (int)pdfViewer.GetZoom()* 100));
  81. }
  82. private void PDFScalingControl_SetScaleEvent(object sender, string e)
  83. {
  84. if (ViewControl == null || ViewControl.PDFViewTool == null)
  85. {
  86. return;
  87. }
  88. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  89. if (pdfViewer == null)
  90. {
  91. return;
  92. }
  93. if (!string.IsNullOrEmpty(e))
  94. {
  95. pdfViewer.SetZoom(double.Parse(e) / 100);
  96. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  97. }
  98. }
  99. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  100. {
  101. if (ViewControl == null || ViewControl.PDFViewTool == null)
  102. {
  103. return;
  104. }
  105. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  106. if (pdfViewer == null)
  107. {
  108. return;
  109. }
  110. if (e == "Actual size")
  111. {
  112. pdfViewer.SetFitMode(FitModes.FitOriginal);
  113. }
  114. else if (e == "Suitable width")
  115. {
  116. pdfViewer.SetFitMode(FitModes.FitWidth);
  117. }
  118. else if (e == "Single page size")
  119. {
  120. pdfViewer.SetFitMode(FitModes.FitHeight);
  121. }
  122. else
  123. {
  124. pdfViewer.SetZoom(double.Parse(e) / 100);
  125. }
  126. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  127. pdfViewer.UpDataRenderFrame();
  128. }
  129. public void SetZoomTextBoxText(string value)
  130. {
  131. CPDFScalingUI.SetZoomTextBoxText(value);
  132. }
  133. private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e)
  134. {
  135. if (ViewControl == null || ViewControl.PDFViewTool == null)
  136. {
  137. return;
  138. }
  139. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  140. if (pdfViewer == null)
  141. {
  142. return;
  143. }
  144. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100)));
  145. }
  146. }
  147. }