using ComPDFKitViewer; using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Compdfkit_Tools.PDFControl { public partial class CPDFScalingControl : UserControl { public PDFViewControl ViewControl; public CPDFScalingControl() { InitializeComponent(); } public void InitWithPDFViewer(PDFViewControl viewControl) { ViewControl = viewControl; } private void PDFControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100))); } } private void PDFScalingControl_Loaded(object sender, RoutedEventArgs e) { CPDFScalingUI.SetScaleEvent += PDFScalingControl_SetScaleEvent; CPDFScalingUI.ScaleIncreaseEvent += PDFScalingControl_ScaleIncreaseEvent; CPDFScalingUI.ScaleDecreaseEvent += PDFScalingControl_ScaleDecreaseEvent; CPDFScalingUI.SetPresetScaleEvent += CPDFScalingUI_SetPresetScaleEvent; ViewControl.PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFControl_MouseWheelZoomHandler; } private void CPDFScalingUI_Unloaded(object sender, RoutedEventArgs e) { CPDFScalingUI.SetScaleEvent -= PDFScalingControl_SetScaleEvent; CPDFScalingUI.ScaleIncreaseEvent -= PDFScalingControl_ScaleIncreaseEvent; CPDFScalingUI.ScaleDecreaseEvent -= PDFScalingControl_ScaleDecreaseEvent; CPDFScalingUI.SetPresetScaleEvent -= CPDFScalingUI_SetPresetScaleEvent; ViewControl.PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler -= PDFControl_MouseWheelZoomHandler; } private void PDFScalingControl_ScaleDecreaseEvent(object sender, EventArgs e) { if (ViewControl == null || ViewControl.PDFViewTool == null) { return; } CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); if (pdfViewer == null) { return; } if (pdfViewer.GetZoom() < 3) { double newZoom = Math.Max(0.01, pdfViewer.GetZoom() - 0.1); pdfViewer.SetZoom(newZoom); pdfViewer.UpDateRenderFrame(); } else if (pdfViewer.GetZoom() < 6) { pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.2); pdfViewer.UpDateRenderFrame(); } else if (pdfViewer.GetZoom() >6) { pdfViewer.SetZoom(pdfViewer.GetZoom() - 0.3); pdfViewer.UpDateRenderFrame(); } SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100))); } private void PDFScalingControl_ScaleIncreaseEvent(object sender, EventArgs e) { if (ViewControl == null || ViewControl.PDFViewTool == null) { return; } CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); if (pdfViewer == null) { return; } if (pdfViewer.GetZoom() < 3) { pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.1); pdfViewer.UpDateRenderFrame(); } else if (pdfViewer.GetZoom() < 6) { pdfViewer.SetZoom(pdfViewer.GetZoom() + 0.2); pdfViewer.UpDateRenderFrame(); } else if (pdfViewer.GetZoom() <= 10) { double newZoom = Math.Max(10, pdfViewer.GetZoom() + 0.3); pdfViewer.SetZoom(newZoom); pdfViewer.UpDateRenderFrame(); } SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom()* 100))); } private void PDFScalingControl_SetScaleEvent(object sender, string e) { if (ViewControl == null || ViewControl.PDFViewTool == null) { return; } CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); if (pdfViewer == null) { return; } if (!string.IsNullOrEmpty(e)) { pdfViewer.SetZoom(double.Parse(e) / 100); pdfViewer.UpDateRenderFrame(); SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100))); } } private void CPDFScalingUI_SetPresetScaleEvent(object sender, string e) { if (ViewControl == null || ViewControl.PDFViewTool == null) { return; } CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); if (pdfViewer == null) { return; } if (e == "Actual size") { pdfViewer.SetFitMode(FitModes.FitOriginal); } else if (e == "Suitable width") { pdfViewer.SetFitMode(FitModes.FitWidth); } else if (e == "Single page size") { pdfViewer.SetFitMode(FitModes.FitHeight); } else { pdfViewer.SetZoom(double.Parse(e) / 100); } SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100))); pdfViewer.UpDateRenderFrame(); } public void SetZoomTextBoxText(string value) { CPDFScalingUI.SetZoomTextBoxText(value); } private void CPDFPageScalingControl_LostFocus(object sender, RoutedEventArgs e) { if (ViewControl == null || ViewControl.PDFViewTool == null) { return; } CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer(); if (pdfViewer == null) { return; } SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewer.GetZoom() * 100))); } } }