12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- using ComPDFKit.PDFDocument;
- using Compdfkit_Tools.Data;
- using Compdfkit_Tools.Helper;
- using Compdfkit_Tools.PDFControl;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Runtime.CompilerServices;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- namespace Annotations
- {
- public partial class MainWindow : Window, INotifyPropertyChanged
- {
- #region Property
- private PDFViewControl passwordViewer;
- private PDFViewControl pdfViewControl;
- private CPDFAnnotationControl pdfAnnotationControl = null;
- private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- public bool CanSave
- {
- get
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- return pdfViewControl.PDFView.UndoManager.CanSave;
- }
- return false;
- }
- }
- public bool CanUndo
- {
- get
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- return pdfViewControl.PDFView.UndoManager.CanUndo;
- }
- return false;
- }
- }
- public bool CanRedo
- {
- get
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- return pdfViewControl.PDFView.UndoManager.CanRedo;
- }
- return false;
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public ICommand CloseTabCommand;
- #endregion
- public MainWindow()
- {
- InitializeComponent();
- DataContext = this;
- }
- #region Load document
- private void LoadDefaultDocument()
- {
- string defaultFilePath = "PDF32000_2008.pdf";
- pdfViewControl = new PDFViewControl();
- pdfViewControl.PDFView.InitDocument(defaultFilePath);
- LoadDocument();
- }
- private void LoadDocument()
- {
- if (pdfViewControl.PDFView.Document == null)
- {
- return;
- }
- pdfViewControl.PDFView?.Load();
- pdfViewControl.PDFView?.SetShowLink(true);
- PDFGrid.Child = pdfViewControl;
- pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged;
- pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged;
- pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler;
- pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler;
- pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler;
- pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler;
- pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged;
- pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
- pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler;
- pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler;
- pdfViewControl.PDFView.SetFormFieldHighlight(true);
- PasswordUI.Closed -= PasswordUI_Closed;
- PasswordUI.Canceled -= PasswordUI_Canceled;
- PasswordUI.Confirmed -= PasswordUI_Confirmed;
- PasswordUI.Closed += PasswordUI_Closed;
- PasswordUI.Canceled += PasswordUI_Canceled;
- PasswordUI.Confirmed += PasswordUI_Confirmed;
- pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth);
- CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView);
- CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100)));
- ViewSettingBtn.IsChecked = false;
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- InitialPDFViewControl(pdfViewControl);
- FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView);
- BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView);
- BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
- }
- #endregion
- #region Load Unload custom control
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- pdfAnnotationControl = new CPDFAnnotationControl();
- BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation);
- LoadDefaultDocument();
- }
- private void AnnotationBarControl_Loaded(object sender, RoutedEventArgs e)
- {
- CPDFAnnotationType[] annotationProperties = { CPDFAnnotationType.Highlight, CPDFAnnotationType.Underline, CPDFAnnotationType.Strikeout, CPDFAnnotationType.Squiggly, CPDFAnnotationType.Freehand, CPDFAnnotationType.FreeText, CPDFAnnotationType.Note, CPDFAnnotationType.Circle, CPDFAnnotationType.Square, CPDFAnnotationType.Arrow, CPDFAnnotationType.Line, CPDFAnnotationType.Image, CPDFAnnotationType.Stamp, CPDFAnnotationType.Signature, CPDFAnnotationType.Link, CPDFAnnotationType.Audio };
- AnnotationBarControl.InitAnnotationBar(annotationProperties);
- AnnotationBarControl.AnnotationPropertyChanged += AnnotationBarControl_AnnotationPropertyChanged;
- AnnotationBarControl.AnnotationCancel += AnnotationBarControl_AnnotationCancel;
- }
- private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e)
- {
- AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged;
- AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel;
- }
- #endregion
- #region Annotation
- public void InitialPDFViewControl(PDFViewControl newPDFViewer)
- {
- pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView);
- pdfAnnotationControl.AnnotationCancel();
- AnnotationBarControl.ClearAllToolState();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar;
- }
- private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e)
- {
- AnnotationBarControl.ClearAllToolState();
- }
- #endregion
- #region Load Document
- #endregion
- #region Password
- private void PasswordUI_Confirmed(object sender, string e)
- {
- if (passwordViewer != null && passwordViewer.PDFView != null && passwordViewer.PDFView.Document != null)
- {
- passwordViewer.PDFView.Document.UnlockWithPassword(e);
- if (passwordViewer.PDFView.Document.IsLocked == false)
- {
- PasswordUI.SetShowError("", Visibility.Collapsed);
- PasswordUI.ClearPassword();
- PasswordUI.Visibility = Visibility.Collapsed;
- PopupBorder.Visibility = Visibility.Collapsed;
- pdfViewControl.PDFView.Document.Release();
- pdfViewControl = passwordViewer;
- LoadDocument();
- }
- else
- {
- PasswordUI.SetShowError("Wrong Password", Visibility.Visible);
- }
- }
- }
- private void PasswordUI_Canceled(object sender, EventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- PasswordUI.Visibility = Visibility.Collapsed;
- }
- private void PasswordUI_Closed(object sender, EventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- PasswordUI.Visibility = Visibility.Collapsed;
- }
- #endregion
- #region Expand and collapse Panel
- public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible)
- {
- PropertyContainer.Width = 260;
- PropertyContainer.Child = propertytPanel;
- PropertyContainer.Visibility = visible;
- if (visible == Visibility.Collapsed || visible == Visibility.Hidden)
- {
- RightPanelButton.IsChecked = false;
- }
- }
- private void ExpandLeftPanel(bool isExpand)
- {
- BotaSideTool.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- Splitter.Visibility = isExpand ? Visibility.Visible : Visibility.Collapsed;
- if (isExpand)
- {
- BodyGrid.ColumnDefinitions[0].Width = new GridLength(320);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(15);
- }
- else
- {
- BodyGrid.ColumnDefinitions[0].Width = new GridLength(0);
- BodyGrid.ColumnDefinitions[1].Width = new GridLength(0);
- }
- }
- #endregion
- #region Context menu
- private void PDFView_AnnotCommandHandler(object sender, AnnotCommandArgs e)
- {
- switch (e.CommandType)
- {
- case CommandType.Context:
- e.Handle = true;
- if (e.CommandTarget == TargetType.Annot)
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- if (e.PressOnLink && AnnotationBarControl.CurrentMode == "Link")
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- MenuItem propertyMenu = new MenuItem();
- propertyMenu = new MenuItem();
- propertyMenu.Header = "Edit";
- WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(propertyMenu, "Click", EditLink_Click);
- propertyMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(propertyMenu);
- }
- else if (e.PressOnAnnot)
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- 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 });
- }
- else if (e.PressOnMedia || e.PressOnSound)
- {
- e.Handle = true;
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Play", Command = MediaCommands.Play, CommandTarget = (UIElement)sender, CommandParameter = e });
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
- }
- else if (e.PressOnSelectedText)
- {
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
- MenuItem highLightMenu = new MenuItem();
- highLightMenu.Header = "HighLight";
- highLightMenu.Click += (o, p) =>
- {
- TextHighlightAnnotArgs highLightArgs = new TextHighlightAnnotArgs();
- MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
- if (pdfAnnotationControl != null)
- {
- highLightArgs.Color = System.Windows.Media.Colors.Red;
- highLightArgs.Transparency = 1;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
- pdfViewControl.PDFView.SetToolParam(highLightArgs);
- pdfViewControl.PDFView.SetMouseMode(oldMode);
- }
- };
- e.PopupMenu.Items.Add(highLightMenu);
- MenuItem underlineMenu = new MenuItem();
- underlineMenu.Header = "UnderLine";
- underlineMenu.Click += (o, p) =>
- {
- TextUnderlineAnnotArgs underlineArgs = new TextUnderlineAnnotArgs();
- MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
- if (pdfAnnotationControl != null)
- {
- underlineArgs.Color = System.Windows.Media.Colors.Red;
- underlineArgs.Transparency = 1;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
- pdfViewControl.PDFView.SetToolParam(underlineArgs);
- pdfViewControl.PDFView.SetMouseMode(oldMode);
- }
- };
- e.PopupMenu.Items.Add(underlineMenu);
- MenuItem strikeOutMenu = new MenuItem();
- strikeOutMenu.Header = "StrikeOut";
- strikeOutMenu.Click += (o, p) =>
- {
- TextStrikeoutAnnotArgs strikeoutAnnotArgs = new TextStrikeoutAnnotArgs();
- MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
- if (pdfAnnotationControl != null)
- {
- strikeoutAnnotArgs.Color = System.Windows.Media.Colors.Red;
- strikeoutAnnotArgs.Transparency = 1;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
- pdfViewControl.PDFView.SetToolParam(strikeoutAnnotArgs);
- pdfViewControl.PDFView.SetMouseMode(oldMode);
- }
- };
- e.PopupMenu.Items.Add(strikeOutMenu);
- MenuItem SquiggleMenu = new MenuItem();
- SquiggleMenu.Header = "Squiggle";
- SquiggleMenu.Click += (o, p) =>
- {
- TextSquigglyAnnotArgs squigglyAnnotArgs = new TextSquigglyAnnotArgs();
- MouseModes oldMode = pdfViewControl.PDFView.MouseMode;
- if (pdfAnnotationControl != null)
- {
- squigglyAnnotArgs.Color = System.Windows.Media.Colors.Red;
- squigglyAnnotArgs.Transparency = 1;
- pdfViewControl.PDFView.SetMouseMode(MouseModes.AnnotCreate);
- pdfViewControl.PDFView.SetToolParam(squigglyAnnotArgs);
- pdfViewControl.PDFView.SetMouseMode(oldMode);
- }
- };
- e.PopupMenu.Items.Add(SquiggleMenu);
- }
- else
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- e.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- e.PopupMenu.Items.Add(new Separator());
- MenuItem fitWidthMenu = new MenuItem();
- fitWidthMenu.Header = "Automatically Resize";
- fitWidthMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitWidth);
- }
- };
- e.PopupMenu.Items.Add(fitWidthMenu);
- MenuItem fitSizeMenu = new MenuItem();
- fitSizeMenu.Header = "Actual Size";
- fitSizeMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeFitMode(FitMode.FitSize);
- }
- };
- e.PopupMenu.Items.Add(fitSizeMenu);
- MenuItem zoomInMenu = new MenuItem();
- zoomInMenu.Header = "Zoom In";
- zoomInMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- };
- e.PopupMenu.Items.Add(zoomInMenu);
- MenuItem zoomOutMenu = new MenuItem();
- zoomOutMenu.Header = "Zoom Out";
- zoomOutMenu.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- };
- e.PopupMenu.Items.Add(zoomOutMenu);
- e.PopupMenu.Items.Add(new Separator());
- MenuItem singleView = new MenuItem();
- singleView.Header = "Single Page";
- singleView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Single);
- }
- };
- e.PopupMenu.Items.Add(singleView);
- MenuItem singleContinuousView = new MenuItem();
- singleContinuousView.Header = "Single Page Continuous";
- singleContinuousView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.SingleContinuous);
- }
- };
- e.PopupMenu.Items.Add(singleContinuousView);
- MenuItem doubleView = new MenuItem();
- doubleView.Header = "Two Pages";
- doubleView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.Double);
- }
- };
- e.PopupMenu.Items.Add(doubleView);
- MenuItem doubleContinuousView = new MenuItem();
- doubleContinuousView.Header = "Two Pages Continuous";
- doubleContinuousView.Click += (o, p) =>
- {
- if (pdfViewControl != null)
- {
- pdfViewControl.PDFView?.ChangeViewMode(ViewMode.DoubleContinuous);
- }
- };
- e.PopupMenu.Items.Add(doubleContinuousView);
- }
- }
- else if (e.CommandTarget == TargetType.ImageSelection)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.GetSelectImageCount() > 0)
- {
- e.Handle = true;
- e.PopupMenu = new ContextMenu();
- MenuItem imageCopyMenu = new MenuItem();
- imageCopyMenu = new MenuItem();
- imageCopyMenu.Header = "Copy Images";
- WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageCopyMenu, "Click", CopyImage_Click);
- imageCopyMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(imageCopyMenu);
- MenuItem imageExtraMenu = new MenuItem();
- imageExtraMenu = new MenuItem();
- imageExtraMenu.Header = "Extract Images";
- WeakEventManager<MenuItem, RoutedEventArgs>.AddHandler(imageExtraMenu, "Click", ExtraImage_Click);
- imageExtraMenu.CommandParameter = e;
- e.PopupMenu.Items.Add(imageExtraMenu);
- }
- }
- break;
- case CommandType.Copy:
- e.DoCommand();
- break;
- case CommandType.Cut:
- case CommandType.Paste:
- case CommandType.Delete:
- e.DoCommand();
- break;
- default:
- break;
- }
- }
- private void CopyImage_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<Bitmap> imageList = imageDict[pageIndex];
- foreach (Bitmap image in imageList)
- {
- MemoryStream ms = new MemoryStream();
- image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
- 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 ExtraImage_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;
- string openPath = choosePath;
- try
- {
- Dictionary<int, List<Bitmap>> imageDict = pdfViewControl.PDFView?.GetSelectedImages();
- if (imageDict != null && imageDict.Count > 0)
- {
- foreach (int pageIndex in imageDict.Keys)
- {
- List<Bitmap> imageList = imageDict[pageIndex];
- foreach (Bitmap image in imageList)
- {
- string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
- image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
- openPath = savePath;
- }
- }
- }
- Process.Start("explorer", "/select,\"" + openPath + "\"");
- }
- catch (Exception ex)
- {
- }
- }
- }
- #endregion
- #region UI
- 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 < 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;
- }
- private void ControlLeftPanel()
- {
- if (LeftToolPanelButton != null)
- {
- bool isExpand = LeftToolPanelButton.IsChecked == true;
- ExpandLeftPanel(isExpand);
- }
- }
- private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e)
- {
- ControlLeftPanel();
- }
- private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- BotaSideTool.SelectBotaTool(BOTATools.Search);
- }
- private void ShowViewSettings()
- {
- if (ViewSettingBtn != null)
- {
- if (ViewSettingBtn.IsChecked == true)
- {
- CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl();
- displayPanel.InitWithPDFViewer(pdfViewControl.PDFView);
- PropertyContainer.Child = displayPanel;
- PropertyContainer.Visibility = Visibility.Visible;
- if ((bool)RightPanelButton.IsChecked)
- {
- RightPanelButton.IsChecked = false;
- }
- }
- else
- {
- PropertyContainer.Child = null;
- PropertyContainer.Visibility = Visibility.Collapsed;
- }
- }
- }
- private void ViewSettingBtn_Click(object sender, RoutedEventArgs e)
- {
- ShowViewSettings();
- }
- private void PageInfoBtn_Click(object sender, RoutedEventArgs e)
- {
- PasswordUI.Visibility = Visibility.Collapsed;
- CPDFInfoControl.Visibility = Visibility.Visible;
- CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
- CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
- CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
- PopupBorder.Visibility = Visibility.Visible;
- }
- private void CPDFInfoControl_CloseInfoEvent(object sender, EventArgs e)
- {
- PopupBorder.Visibility = Visibility.Collapsed;
- }
- private void OpenFile()
- {
- string filePath = CommonHelper.GetFilePathOrEmpty();
- if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null)
- {
- if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
- {
- string oldFilePath = pdfViewControl.PDFView.Document.FilePath;
- if (oldFilePath.ToLower() == filePath.ToLower())
- {
- return;
- }
- }
- passwordViewer = new PDFViewControl();
- passwordViewer.PDFView.InitDocument(filePath);
- if (passwordViewer.PDFView.Document == null)
- {
- MessageBox.Show("Open File Failed");
- return;
- }
- if (passwordViewer.PDFView.Document.IsLocked)
- {
- PasswordUI.SetShowText(System.IO.Path.GetFileName(filePath) + " password encrypted.");
- PasswordUI.ClearPassword();
- PopupBorder.Visibility = Visibility.Visible;
- PasswordUI.Visibility = Visibility.Visible;
- }
- else
- {
- pdfViewControl.PDFView.Document.Release();
- pdfViewControl = passwordViewer;
- LoadDocument();
- }
- }
- }
- private void OpenFile_Click(object sender, RoutedEventArgs e)
- {
- OpenFile();
- }
- private void SaveFileBtn_Click(object sender, RoutedEventArgs e)
- {
- SaveFile();
- pdfViewControl.PDFView.UndoManager.CanSave = false;
- }
- private void ControlRightPanel()
- {
- if (RightPanelButton != null)
- {
- if (RightPanelButton.IsChecked == true)
- {
- if (pdfAnnotationControl != null)
- {
- ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
- if ((bool)ViewSettingBtn.IsChecked)
- {
- ViewSettingBtn.IsChecked = false;
- }
- }
- }
- else
- {
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- }
- }
- }
- private void RightPanelButton_Click(object sender, RoutedEventArgs e)
- {
- ControlRightPanel();
- }
- private void EditLink_Click(object sender, RoutedEventArgs e)
- {
- PropertyContainer.Visibility = Visibility.Visible;
- }
- private void UndoButton_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.UndoManager?.Undo();
- }
- }
- private void RedoButton_Click(object sender, RoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.UndoManager?.Redo();
- }
- }
- #endregion
- #region Property changed
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- }
- #endregion
- #region Event handle
- private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e)
- {
- pdfAnnotationControl.AnnotationCancel();
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- RightPanelButton.IsChecked = false;
- ViewSettingBtn.IsChecked = false;
- }
- private void AnnotationBarControl_AnnotationPropertyChanged(object sender, CPDFAnnotationType e)
- {
- pdfAnnotationControl.LoadAnnotationPanel(e);
- if (e != CPDFAnnotationType.Audio && e != CPDFAnnotationType.Image)
- {
- ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible);
- RightPanelButton.IsChecked = true;
- }
- }
- private void PDFView_AnnotActiveHandler(object sender, AnnotAttribEvent e)
- {
- PropertyContainer.Child = pdfAnnotationControl;
- pdfAnnotationControl.SetAnnotEventData(e);
- }
- private void PDFView_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
- {
- BotaSideTool.LoadAnnotationList();
- }
- private void PdfViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
- {
- if (e.Key == "Zoom")
- {
- CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100)));
- }
- }
- #endregion
- #region Save file
- /// <summary>
- /// Save the file to another PDF file.
- /// </summary>
- public void SaveAsFile()
- {
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
- {
- CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
- SaveFileDialog saveDialog = new SaveFileDialog();
- saveDialog.Filter = "(*.pdf)|*.pdf";
- saveDialog.DefaultExt = ".pdf";
- saveDialog.OverwritePrompt = true;
- if (saveDialog.ShowDialog() == true)
- {
- pdfDoc.WriteToFilePath(saveDialog.FileName);
- }
- }
- }
- }
- /// <summary>
- /// Save the file in the current path.
- /// </summary>
- private void SaveFile()
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null)
- {
- try
- {
- CPDFDocument pdfDoc = pdfViewControl.PDFView.Document;
- if (pdfDoc.WriteToLoadedPath())
- {
- return;
- }
- SaveFileDialog saveDialog = new SaveFileDialog();
- saveDialog.Filter = "(*.pdf)|*.pdf";
- saveDialog.DefaultExt = ".pdf";
- saveDialog.OverwritePrompt = true;
- if (saveDialog.ShowDialog() == true)
- {
- pdfDoc.WriteToFilePath(saveDialog.FileName);
- }
- }
- catch (Exception ex)
- {
- }
- }
- }
- #endregion
- #region Selected changed
- private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- var item = (sender as ComboBox).SelectedItem as ComboBoxItem;
- if ((string)item.Content == "Viewer")
- {
- AnnotationBarControl.ClearAllToolState();
- ToolBarContainer.Visibility = Visibility.Collapsed;
- ExpandRightPropertyPanel(null, Visibility.Collapsed);
- pdfAnnotationControl.AnnotationCancel();
- RightPanelButton.IsChecked = false;
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer);
- }
- }
- else if ((string)item.Content == "Annotation")
- {
- ToolBarContainer.Visibility = Visibility.Visible;
- if (pdfViewControl != null && pdfViewControl.PDFView != null)
- {
- pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool);
- }
- }
- }
- #endregion
- #region Close window
- protected override void OnClosing(CancelEventArgs e)
- {
- if (pdfViewControl.PDFView.UndoManager.CanSave)
- {
- MessageBoxResult result = MessageBox.Show("Do you want to save your changes before closing the application?", "Message", MessageBoxButton.YesNoCancel);
- if (result == MessageBoxResult.Yes)
- {
- SaveFile();
- }
- else if (result == MessageBoxResult.No)
- {
- }
- else
- {
- e.Cancel = true;
- }
- }
- }
- #endregion
- #region Shortcut
- private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
- {
- OpenFile();
- }
- private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
- {
- if (CanSave)
- {
- SaveFile();
- }
- }
- private void CommandBinding_Executed_SaveAs(object sender, ExecutedRoutedEventArgs e)
- {
- SaveAsFile();
- }
- private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null && CanUndo)
- {
- pdfViewControl.PDFView.UndoManager?.Undo();
- }
- }
- private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e)
- {
- if (pdfViewControl != null && pdfViewControl.PDFView != null && CanRedo)
- {
- pdfViewControl.PDFView.UndoManager?.Redo();
- }
- }
- private void CommandBinding_Executed_ControlLeftPanel(object sender, ExecutedRoutedEventArgs e)
- {
- LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked;
- ControlLeftPanel();
- }
- private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e)
- {
- RightPanelButton.IsChecked = !RightPanelButton.IsChecked;
- ControlRightPanel();
- }
- private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e)
- {
- if (ModeComboBox.SelectedIndex == 1)
- {
- AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight);
- }
- }
- private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e)
- {
- if (ModeComboBox.SelectedIndex == 1)
- {
- AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline);
- }
- }
- private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e)
- {
- if (ModeComboBox.SelectedIndex == 1)
- {
- AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout);
- }
- }
- private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e)
- {
- if (ModeComboBox.SelectedIndex == 1)
- {
- AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly);
- }
- }
- private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Bookmark);
- }
- private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Outline);
- }
- private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Thumbnail);
- }
- private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Annotation);
- }
- private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e)
- {
- ExpandLeftPanel(true);
- LeftToolPanelButton.IsChecked = true;
- BotaSideTool.SelectBotaTool(BOTATools.Search);
- }
- private void CommandBinding_Executed_ScaleAdd(object sender, ExecutedRoutedEventArgs e)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor + 0.01, true);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- private void CommandBinding_Executed_ScaleSubtract(object sender, ExecutedRoutedEventArgs e)
- {
- double newZoom = CheckZoomLevel(pdfViewControl.PDFView.ZoomFactor - 0.01, false);
- pdfViewControl.PDFView?.Zoom(newZoom);
- }
- private void CommandBinding_Executed_DisplaySettings(object sender, ExecutedRoutedEventArgs e)
- {
- ViewSettingBtn.IsChecked = !ViewSettingBtn.IsChecked;
- ShowViewSettings();
- }
- private void CommandBinding_Executed_DocumentInfo(object sender, ExecutedRoutedEventArgs e)
- {
- if (PopupBorder.Visibility != Visibility.Visible)
- {
- PasswordUI.Visibility = Visibility.Collapsed;
- CPDFInfoControl.Visibility = Visibility.Visible;
- CPDFInfoControl.InitWithPDFViewer(pdfViewControl.PDFView);
- CPDFInfoControl.CloseInfoEvent -= CPDFInfoControl_CloseInfoEvent;
- CPDFInfoControl.CloseInfoEvent += CPDFInfoControl_CloseInfoEvent;
- PopupBorder.Visibility = Visibility.Visible;
- }
- else
- {
- CPDFInfoControl.Visibility = Visibility.Collapsed;
- PopupBorder.Visibility = Visibility.Collapsed;
- }
- }
- #endregion
- }
- }
|