CPDFScalingControl.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using compdfkit_tools.Common;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.PdfViewer;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. namespace compdfkit_tools.PDFControl
  19. {
  20. /// <summary>
  21. /// PDFScallingControl.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class CPDFScalingControl : UserControl
  24. {
  25. public CPDFViewer pdfViewer;
  26. public CPDFScalingControl()
  27. {
  28. InitializeComponent();
  29. }
  30. public void InitWithPDFViewer(CPDFViewer pdfViewer)
  31. {
  32. this.pdfViewer = pdfViewer;
  33. }
  34. private void PDFScalingControl_Loaded(object sender, RoutedEventArgs e)
  35. {
  36. CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent;
  37. CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent;
  38. CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent;
  39. CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent;
  40. }
  41. private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e)
  42. {
  43. CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent;
  44. CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent;
  45. CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent;
  46. CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent;
  47. }
  48. private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e)
  49. {
  50. if (pdfViewer == null || pdfViewer.Document == null)
  51. {
  52. return;
  53. }
  54. if (pdfViewer.ZoomFactor < 3)
  55. {
  56. pdfViewer.Zoom(pdfViewer.ZoomFactor - 0.1);
  57. }
  58. else if (pdfViewer.ZoomFactor < 6)
  59. {
  60. pdfViewer.Zoom(pdfViewer.ZoomFactor - 0.2);
  61. }
  62. else if (pdfViewer.ZoomFactor <= 10)
  63. {
  64. pdfViewer.Zoom(pdfViewer.ZoomFactor - 0.3);
  65. }
  66. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.ZoomFactor * 100)));
  67. }
  68. private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e)
  69. {
  70. if (pdfViewer == null || pdfViewer.Document == null)
  71. {
  72. return;
  73. }
  74. if (pdfViewer.ZoomFactor < 3)
  75. {
  76. pdfViewer.Zoom(pdfViewer.ZoomFactor + 0.1);
  77. }
  78. else if (pdfViewer.ZoomFactor < 6)
  79. {
  80. pdfViewer.Zoom(pdfViewer.ZoomFactor + 0.2);
  81. }
  82. else if (pdfViewer.ZoomFactor <= 10)
  83. {
  84. pdfViewer.Zoom(pdfViewer.ZoomFactor + 0.3);
  85. }
  86. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.ZoomFactor * 100)));
  87. }
  88. private void PDFScalingControl_SetScaleEvent(object sender, string e)
  89. {
  90. if (pdfViewer == null || pdfViewer.Document == null)
  91. {
  92. return;
  93. }
  94. if (!string.IsNullOrEmpty(e))
  95. {
  96. pdfViewer.Zoom(double.Parse(e) / 100);
  97. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.ZoomFactor * 100)));
  98. }
  99. }
  100. private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e)
  101. {
  102. if (pdfViewer == null || pdfViewer.Document == null)
  103. {
  104. return;
  105. }
  106. if (e == "Actual size")
  107. {
  108. pdfViewer.ChangeFitMode(FitMode.FitSize);
  109. }
  110. else if (e == "Suitable width")
  111. {
  112. pdfViewer.ChangeFitMode(FitMode.FitWidth);
  113. }
  114. else if (e == "Single page size")
  115. {
  116. pdfViewer.ChangeFitMode(FitMode.FitHeight);
  117. }
  118. else
  119. {
  120. pdfViewer.Zoom(double.Parse(e) / 100);
  121. }
  122. SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.ZoomFactor * 100)));
  123. }
  124. public void SetZoomTextBoxText(string value)
  125. {
  126. CPDFScalingUI.SetZoomTextBoxText(value);
  127. }
  128. }
  129. }