123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 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)));
- }
- }
- }
|