using ComPDFKitDemo.AnntControl; using ComPDFKitDemo.BOTA; using System; using System.Collections.Generic; using System.Diagnostics; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using ComPDFKitDemo.WidgetForm; using ComPDFKit.PDFAnnotation.Form; using ComPDFKitViewer.AnnotView.Form; using Microsoft.Win32; using System.IO; using System.Windows.Media.Imaging; using System.Linq; using ComPDFKit.Import; namespace ComPDFKitDemo { public partial class PDFViewerControl : UserControl { private SideWindowControl sideControl; private MainWindow currentMain; public CPDFViewer currentViewer { get; private set; } public AnnotPropertiesControl AnnotSetControl; private string filePath=""; private double[] zoomLevel = { 1.00f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800,1000 }; public double NaviWidth { get { return DocViewerGrid.ColumnDefinitions[0].Width.Value; } set { DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(value); } } public PDFViewerControl(MainWindow mainWindow) { InitializeComponent(); currentMain = mainWindow; currentViewer = new CPDFViewer(); sideControl = new SideWindowControl(); AnnotSetControl = new AnnotPropertiesControl(); AnnotSetControl.pdfViewerControl = this; NaviBarGrid.Children.Add(sideControl); PDFGrid.Children.Add(currentViewer); AnnotCtrlGrid.Children.Add(AnnotSetControl); DocViewerGrid.ColumnDefinitions[4].Width = new GridLength(200); AnnotSplitter.Visibility = Visibility.Visible; currentMain.btn_collapse.IsEnabled = false; currentMain.btn_expand.IsEnabled = true; currentViewer.AnnotActiveHandler += CurrentView_AnnotActiveHandler; currentViewer.AnnotCommandHandler += CurrentViewer_AnnotCommandHandler; currentViewer.TextEditCommandHandler += CurrentViewer_TextEditCommandHandler; currentViewer.TextEditActiveHandler += CurrentViewer_TextEditActiveHandler; currentViewer.SnapshotCommandHandler += CurrentViewer_SnapshotCommandHandler; currentViewer.MouseWheelZoomHandler += CurrentViewer_MouseWheelZoomHandler; currentViewer.AnnotEditHandler += CurrentViewer_AnnotEditHandler; currentViewer.WidgetClickHander += CurrentViewer_WidgetClickHander; } private void CurrentViewer_WidgetClickHander(object sender, WidgetArgs e) { if (e is WidgetSignArgs) { WidgetSignArgs signArgs = (WidgetSignArgs)e; //set text ap //signArgs.UpdateApWithText("hhh", "Courier-Bold", Colors.Red, 0); //set image ap OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { string ImagePath = openFileDialog.FileName; using (FileStream fileData = File.OpenRead(ImagePath)) { string fileExt = Path.GetExtension(ImagePath).ToLower(); BitmapFrame frame = null; BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default); if (decoder != null && decoder.Frames.Count > 0) { frame = decoder.Frames[0]; } if (frame != null) { byte[] ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4]; int ImageWidth = frame.PixelWidth; int ImageHeight = frame.PixelHeight; frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0); signArgs.UpdateApWithImage(ImageArray, ImageWidth, ImageHeight, C_Scale_Type.fitCenter, 0); } } } } } public void CurrentViewer_AnnotEditHandler(object sender, List e) { sideControl.LoadAnnot(); } private double CheckZoomLevel(double zoom, bool IsGrowth) { double standardZoom = 100; if(zoom<=0.01) { return 0.01; } if (zoom >= 10) { return 10; } zoom *= 100; for (int i = 0; i < zoomLevel.Length - 1; i++) { if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth) { standardZoom = zoomLevel[i + 1]; break; } if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth) { standardZoom = zoomLevel[i]; break; } } return standardZoom / 100; } private void CurrentViewer_MouseWheelZoomHandler(object sender, bool e) { double newZoom = CheckZoomLevel(currentViewer.ZoomFactor+(e?0.01:-0.01), e); currentViewer.Zoom(newZoom); } private void CurrentViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e) { switch (e.CommandType) { case CommandType.Context: e.Handle = true; e.PopupMenu = new ContextMenu(); if(e.HitSnapshotTool) { e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); } e.PopupMenu.Items.Add(new MenuItem() { Header = "Select All", Command = ApplicationCommands.SelectAll, CommandTarget = (UIElement)sender }); break; case CommandType.Copy: { e.DoCommand(); } break; default: break; } } private void CurrentViewer_TextEditActiveHandler(object sender, TextEditArgs e) { AnnotSetControl.ProcessTextEditEvent(e); } private void CurrentViewer_TextEditCommandHandler(object sender, TextEditCommand e) { switch (e.CommandType) { case CommandType.Context: e.Handle = true; e.PopupMenu = new ContextMenu(); e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Select All", Command = ApplicationCommands.SelectAll, CommandTarget = (UIElement)sender }); break; case CommandType.Copy: case CommandType.Cut: case CommandType.Delete: case CommandType.SelectAll: e.DoCommand(); break; case CommandType.Paste: e.DoCommand(); break; } } private void CurrentViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e) { switch(e.CommandType) { case CommandType.Context: e.Handle = true; e.PopupMenu = new ContextMenu(); e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); if(e.CommandTarget==TargetType.WidgetView) { MenuItem propertyMenu = new MenuItem(); propertyMenu.Header = "Property"; propertyMenu.Click += PropertyMenu_Click; propertyMenu.CommandParameter = (e as WidgetViewCommandArgs); e.PopupMenu.Items.Add(propertyMenu); } if(e.CommandTarget== TargetType.Annot && e.AnnotEventArgsList.AsEnumerable().Where(x=>x.EventType==AnnotArgsType.AnnotRedaction).Count()>0) { MenuItem propertyMenu = new MenuItem(); propertyMenu.Header = "Apply Redaction"; propertyMenu.Click += ApplyRedaction_Click; propertyMenu.CommandParameter = e; e.PopupMenu.Items.Add(propertyMenu); } if(e.CommandTarget==TargetType.ImageSelection) { e.PopupMenu = new ContextMenu(); int count = 0; if (currentViewer!=null) { count= currentViewer.GetSelectImageCount(); } MenuItem copyMenu=new MenuItem(); copyMenu.Header = "Copy Images"; copyMenu.Click += CopyMenu_Click; copyMenu.CommandParameter = e; if(count>1) { copyMenu.IsEnabled = false; } e.PopupMenu.Items.Add(copyMenu); MenuItem extractMenu = new MenuItem(); extractMenu.Header = "Extract Images"; extractMenu.Click += ExtractMenu_Click; extractMenu.CommandParameter = e; e.PopupMenu.Items.Add(extractMenu); } if(e.PressOnMedia || e.PressOnSound) { e.PopupMenu = new ContextMenu(); //if(e.PressOnSound) //{ // e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender,CommandParameter=e}); //} e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender,CommandParameter=e }); } break; default: e.DoCommand(); break; } } private void ExtractMenu_Click(object sender, RoutedEventArgs e) { System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog(); if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string choosePath = folderDialog.SelectedPath; try { Dictionary> imageDict = currentViewer?.GetSelectedImages(); if (imageDict != null && imageDict.Count > 0) { foreach (int pageIndex in imageDict.Keys) { List imageList = imageDict[pageIndex]; foreach (System.Drawing.Bitmap image in imageList) { string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg"); image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); } } } Process.Start("explorer", "/select,\"" + choosePath + "\""); } catch (Exception ex) { } } } private void CopyMenu_Click(object sender, RoutedEventArgs e) { try { Dictionary> imageDict = currentViewer?.GetSelectedImages(); if (imageDict != null && imageDict.Count > 0) { foreach (int pageIndex in imageDict.Keys) { List imageList = imageDict[pageIndex]; foreach (System.Drawing.Bitmap image in imageList) { MemoryStream ms = new MemoryStream(); image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage imageData = new BitmapImage(); imageData.BeginInit(); imageData.StreamSource = ms; imageData.CacheOption = BitmapCacheOption.OnLoad; imageData.EndInit(); imageData.Freeze(); Clipboard.SetImage(imageData); break; } } } } catch (Exception ex) { } } private void ApplyRedaction_Click(object sender, RoutedEventArgs e) { if(currentViewer!=null) { currentViewer.Document.ApplyRedaction(); currentViewer.Document.ReleasePages(); currentViewer.ReloadDocument(); } } private void MenuItem_Click(object sender, RoutedEventArgs e) { } private void PopFormDialog(WidgetAttribEvent attribEvent) { switch (attribEvent.WidgeType) { #region 不同表单控件属性弹窗 case C_WIDGET_TYPE.WIDGET_RADIOBUTTON: { RadioFormProperty formProperty = new RadioFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_CHECKBOX: { CheckBoxFormProperty formProperty = new CheckBoxFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_TEXTFIELD: { TextFieldFormProperty formProperty = new TextFieldFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_PUSHBUTTON: { PushButtonFormProperty formProperty = new PushButtonFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_LISTBOX: { ListBoxFormProperty formProperty = new ListBoxFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_COMBOBOX: { ComboBoxFormProperty formProperty = new ComboBoxFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; case C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS: { SignFormProperty formProperty = new SignFormProperty(); formProperty.WindowStartupLocation = WindowStartupLocation.CenterOwner; formProperty.ShowInTaskbar = false; formProperty.Owner = Window.GetWindow(this); formProperty.CurrentAttribEvent = null; formProperty.SetFormData(attribEvent); formProperty.CurrentAttribEvent = attribEvent; formProperty.Show(); } break; #endregion default: break; } } private void PropertyMenu_Click(object sender, RoutedEventArgs e) { MenuItem menuItem = sender as MenuItem; if(menuItem != null && menuItem.CommandParameter is WidgetViewCommandArgs) { WidgetViewCommandArgs widgetArgs=menuItem.CommandParameter as WidgetViewCommandArgs; PopFormDialog(widgetArgs.AnnotEvent); } } #region Events public void CurrentView_AnnotActiveHandler(object sender, AnnotAttribEvent e) { if (sideControl!=null) { Dictionary> pageAnnots = new Dictionary>(); if (e != null) { pageAnnots= e.GetPageAnnotsIndex(); } sideControl.SetSelectAnnotList(pageAnnots); } if((e is WidgetAttribEvent) || (e!=null &&e.GetAnnotTypes()==AnnotArgsType.WidgetViewForm)) { } else { AnnotSetControl.ProcessAnnotEvents(e); } } #endregion #region Method public bool OpenPDF(string fileName) { try { currentViewer.InitDocument(fileName); if (currentViewer.Document == null) { MessageBox.Show("Open File Failed"); return false; } /* //old while(currentViewer.Document.IsLocked) { PasswordDlg dlg = new PasswordDlg(); System.Windows.Forms.DialogResult dlg_result = dlg.ShowDialog(); if (dlg_result == System.Windows.Forms.DialogResult.Cancel && dlg.press_key != 13) { currentViewer.CloseDocument(); return false; } currentViewer.Document.UnlockWithPassword(dlg.password); if(currentViewer.Document.IsLocked) MessageBox.Show("Password error."); } */ if(currentViewer.Document.IsLocked) { EnterPasswordDlg dlg = new EnterPasswordDlg(currentViewer); bool? res = dlg.ShowDialog(); } currentViewer.Load(); } catch (Exception ex) { return false; } if (currentViewer.Document == null || currentViewer.Document.IsLocked) return false; filePath = fileName; currentMain.filePathList.Add(fileName); return true; } public void ClosePDF() { currentViewer.CloseDocument(); currentMain.filePathList.Remove(filePath); sideControl = null; currentMain = null; AnnotSetControl = null; } public void OpenNavibar() { DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(400); currentMain.btn_expand.IsEnabled = false; currentMain.btn_collapse.IsEnabled = true; SideWindowSplitter.Visibility = Visibility.Visible; if(sideControl.ThumbnailGrid!=null && sideControl.ThumbnailGrid.Children.Count>0) { ThumbnailsControl thumbnailCtrl = sideControl.ThumbnailGrid.Children[0] as ThumbnailsControl; if(thumbnailCtrl!=null) { thumbnailCtrl.Visibility = Visibility.Visible; } } sideControl.Visibility = Visibility.Visible; sideControl.CreateSideWindow(currentViewer); } public void CloseNavibar() { DocViewerGrid.ColumnDefinitions[0].Width = new GridLength(0); currentMain.btn_collapse.IsEnabled = false; currentMain.btn_expand.IsEnabled = true; SideWindowSplitter.Visibility = Visibility.Collapsed; if (sideControl.ThumbnailGrid != null && sideControl.ThumbnailGrid.Children.Count > 0) { ThumbnailsControl thumbnailCtrl = sideControl.ThumbnailGrid.Children[0] as ThumbnailsControl; if (thumbnailCtrl != null) { thumbnailCtrl.Visibility = Visibility.Collapsed; } } sideControl.Visibility = Visibility.Collapsed; } public void HorizontalSplit() { currentViewer.SetSplitMode(SplitMode.Horizontal); } public void VerticalSplit() { currentViewer.SetSplitMode(SplitMode.Vertical); } public void NoneSplit() { currentViewer.SetSplitMode(SplitMode.None); } public void SetMouseMode(MouseModes mouseMode) { currentViewer.SetMouseMode(mouseMode); } public void SetToolParam(AnnotHandlerEventArgs args) { currentViewer.SetToolParam(args); } public ToolManager CurrentToolManager { get { if(currentViewer!=null) { return currentViewer.ToolManager; } return null; } } public UndoManager CurrentUndoManager { get { if (currentViewer != null) { return currentViewer.UndoManager; } return null; } } #endregion } }