CPDFScalingControl.xaml.cs 4.2 KB

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