123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610 |
- 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<AnnotEditEvent> 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<int, List<System.Drawing.Bitmap>> imageDict = currentViewer?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<System.Drawing.Bitmap> 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<int, List<System.Drawing.Bitmap>> imageDict = currentViewer?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<System.Drawing.Bitmap> 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<int, List<int>> pageAnnots = new Dictionary<int, List<int>>();
- 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
- }
- }
|