using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFAnnotation.Form; using ComPDFKit.PDFDocument; using ComPDFKit.Tool; using ComPDFKit.Tool.SettingParam; using Compdfkit_Tools.PDFControlUI; using ComPDFKitViewer; using Nager.Country.Currencies; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using static ComPDFKit.Tool.CPDFToolManager; using SplitMode = Compdfkit_Tools.PDFControlUI.CPDFViewModeUI.SplitMode; namespace Compdfkit_Tools.PDFControl { public partial class PDFViewControl : UserControl { private CPDFViewerTool splitViewerTool; private CPDFToolManager splitToolManager; private CPDFViewerTool viewerTool; private CPDFToolManager toolManager; private AnnotParam annotParam; public CPDFViewerTool PDFViewTool { get;private set; } public CPDFToolManager PDFToolManager { get;private set; } public CPDFViewerTool SelectedPDFViewTool { get;private set; } public event EventHandler SplitPDFViewToolCreated; public event EventHandler SelectedPDFViewToolChanged; #region Properties public bool CustomSignHandle { get; set; } private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; #endregion public PDFViewControl() { InitializeComponent(); //if (ViewerTool==null) //{ // PDFViewTool = ViewerTool= new CPDFViewerTool(); // PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler; // PDFViewTool.SizeChanged += PDFViewTool_SizeChanged; // if (pDFToolManager == null) // { // PDFToolManager= pDFToolManager = new CPDFToolManager(PDFViewTool); // } // else // { // pDFToolManager.Remove(); // PDFToolManager= pDFToolManager = new CPDFToolManager(PDFViewTool); // } //} //else //{ // PDFViewTool = ViewerTool; // PDFToolManager = pDFToolManager; //} viewerTool = new CPDFViewerTool(); toolManager = new CPDFToolManager(viewerTool); PDFViewTool = viewerTool; PDFToolManager = toolManager; SelectedPDFViewTool = viewerTool; PDFView.Child = PDFViewTool; PDFViewTool.SizeChanged += PDFViewTool_SizeChanged; PDFViewTool.GetCPDFViewer().MouseWheelZoomHandler += PDFViewControl_MouseWheelZoomHandler; PDFToolManager.SetToolType(CPDFToolManager.ToolType.Viewer); } private void PDFViewTool_SizeChanged(object sender, SizeChangedEventArgs e) { PDFViewTool.GetCPDFViewer().UpDateRenderFrame(); } public void InitDocument(string Path) { CPDFDocument pdfDoc = CPDFDocument.InitWithFilePath(Path); if (pdfDoc != null) { PDFViewTool.GetCPDFViewer().InitDoc(pdfDoc); PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitHeight); PDFViewTool.GetCPDFViewer().SetViewMode(ViewModes.SingleContinuous); } } private void PDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { e.IsZoom = true; PDFViewTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom); double zoom = PDFViewTool.GetCPDFViewer().GetZoom(); PDFViewTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior))); PDFViewTool.GetCPDFViewer().UpDateRenderFrame(); } } private void SplitPDFViewControl_MouseWheelZoomHandler(object sender, ComPDFKitViewer.MouseWheelZoomArgs e) { if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { e.IsZoom = true; splitViewerTool.GetCPDFViewer().SetFitMode(FitModes.FitZoom); double zoom = splitViewerTool.GetCPDFViewer().GetZoom(); splitViewerTool.GetCPDFViewer().SetZoom(CheckZoomLevel(zoom, Convert.ToBoolean(e.WheelBehavior))); splitViewerTool.GetCPDFViewer().UpDateRenderFrame(); } } public void SetSplitViewMode(SplitMode splitMode) { switch (splitMode) { case SplitMode.None: RemoveSplitViewerTool(); break; case SplitMode.Vertical: RemoveSplitViewerTool(); CreateSplitViewerTool(); if(splitViewerTool != null) { VerticalSplitter.Visibility = Visibility.Visible; VerticalView.Visibility = Visibility.Visible; ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(15, GridUnitType.Auto); ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(ViewToolGrid.ActualWidth / 2); VerticalView.Child = splitViewerTool; } break; case SplitMode.Horizontal: RemoveSplitViewerTool(); CreateSplitViewerTool(); if(splitViewerTool != null) { HorizontalSplitter.Visibility = Visibility.Visible; HorizontalView.Visibility = Visibility.Visible; ViewToolGrid.RowDefinitions[1].Height = new GridLength(15,GridUnitType.Auto); ViewToolGrid.RowDefinitions[2].Height = new GridLength(ViewToolGrid.ActualHeight / 2); HorizontalView.Child = splitViewerTool; } break; } UpdateFrame(); } public void UpdateFrame() { viewerTool.GetCPDFViewer().UpDateRenderFrame(); splitViewerTool?.GetCPDFViewer()?.UpDateRenderFrame(); } public void SetToolType(ToolType type) { toolManager.SetToolType(type); splitToolManager?.SetToolType(type); } public void SetCreateAnnotType(C_ANNOTATION_TYPE type) { toolManager.SetCreateAnnotType(type); splitToolManager?.SetCreateAnnotType(type); } public void SetCreateWidgetType(C_WIDGET_TYPE type) { toolManager.SetCreateWidgetType(type); splitToolManager?.SetCreateWidgetType(type); } public void SetAnnotParam(AnnotParam param) { annotParam = param; DefaultSettingParam defaultSettingParam = viewerTool.GetDefaultSettingParam(); DefaultSettingParam splitDefaultSettingParam = splitViewerTool?.GetDefaultSettingParam(); defaultSettingParam.SetAnnotParam(annotParam); splitDefaultSettingParam?.SetAnnotParam(annotParam); } public void SetIsShowStampMouse(bool isShow) { viewerTool.GetCPDFViewer().SetIsShowStampMouse(isShow); splitViewerTool?.GetCPDFViewer()?.SetIsShowStampMouse(isShow); } public void SetDrawModes(DrawModeData drawMode) { viewerTool.GetCPDFViewer().SetDrawModes(drawMode.DrawMode); splitViewerTool?.GetCPDFViewer()?.SetDrawModes(drawMode.DrawMode); UpdateFrame(); } private void CreateSplitViewerTool() { if (viewerTool.GetCPDFViewer().GetDocument() != null) { splitViewerTool = new CPDFViewerTool(); splitViewerTool.GetCPDFViewer().InitDoc(viewerTool.GetCPDFViewer().GetDocument()); splitToolManager = new CPDFToolManager(splitViewerTool); splitViewerTool.SizeChanged += SplitViewerTool_SizeChanged; splitViewerTool.GetCPDFViewer().MouseWheelZoomHandler += SplitPDFViewControl_MouseWheelZoomHandler; ToolType toolType = toolManager.GetToolType(); splitToolManager.SetToolType(toolType); if (toolType == ToolType.CreateAnnot) { C_ANNOTATION_TYPE createAnnotType = toolManager.GetAnnotType(); splitToolManager.SetCreateAnnotType(createAnnotType); DefaultSettingParam defaultSettingParam = splitViewerTool.GetDefaultSettingParam(); defaultSettingParam.SetAnnotParam(annotParam); } if(toolType == ToolType.FormEdit) { splitToolManager.SetCreateWidgetType((annotParam as FormParm).WidgetType); DefaultSettingParam defaultSettingParam = splitViewerTool.GetDefaultSettingParam(); defaultSettingParam.SetAnnotParam(annotParam); } SplitPDFViewToolCreated?.Invoke(this, EventArgs.Empty); } } private void SplitViewerTool_SizeChanged(object sender, SizeChangedEventArgs e) { splitViewerTool.GetCPDFViewer().UpDateRenderFrame(); } private void RemoveSplitViewerTool() { PDFViewTool.GetCPDFViewer().UpDateRenderFrame(); VerticalSplitter.Visibility = Visibility.Collapsed; HorizontalSplitter.Visibility = Visibility.Collapsed; VerticalView.Visibility = Visibility.Collapsed; HorizontalView.Visibility = Visibility.Collapsed; ViewToolGrid.ColumnDefinitions[1].Width = new GridLength(0); ViewToolGrid.ColumnDefinitions[2].Width = new GridLength(0); ViewToolGrid.RowDefinitions[1].Height = new GridLength(0); ViewToolGrid.RowDefinitions[2].Height = new GridLength(0); splitViewerTool = null; splitToolManager = null; VerticalView.Child = null; HorizontalView.Child = null; } private void PDFView_GotFocus(object sender, RoutedEventArgs e) { switch((sender as Border).Tag) { case "ViewerTool": SelectedPDFViewTool = viewerTool; break; case "SplitViewerTool": SelectedPDFViewTool = splitViewerTool; break; } SelectedPDFViewToolChanged?.Invoke(this,EventArgs.Empty); } #region Private Command Methods private double CheckZoomLevel(double zoom, bool IsGrowth) { zoom += (IsGrowth ? 0.01 : -0.01); double standardZoom = 100; if (zoom <= 0.01) { return 0.01; } if (zoom >= 10) { return 10; } zoom *= 100; for (int i = 0; i < zoomLevelList.Length - 1; i++) { if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth) { standardZoom = zoomLevelList[i + 1]; break; } if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth) { standardZoom = zoomLevelList[i]; break; } } return standardZoom / 100; } #endregion } }