using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKit.PDFPage.Edit; using Compdfkit_Tools.Data; using Compdfkit_Tools.Edit; 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.Linq; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using Path = System.IO.Path; using Point = System.Windows.Point; namespace PDFViewer { public partial class MainPage : UserControl, INotifyPropertyChanged { #region Property private bool isFirstLoad = true; private string currentMode = "Viewer"; private PDFViewControl passwordViewer; private PDFViewControl pdfViewControl = new PDFViewControl(); private CPDFAnnotationControl pdfAnnotationControl = null; private CPDFPageEditControl pageEditControl = null; private FromPropertyControl pdfFormControl = new FromPropertyControl(); private PDFContentEditControl pdfContentEditControl = new PDFContentEditControl(); private CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl(); private PDFEditEvent lastPDFEditEvent = null; public event Func CheckExistBeforeOpenFileEvent; public event PropertyChangedEventHandler PropertyChanged; public event EventHandler FileChangeEvent; private PDFEditEvent pdfTextCreateParam; private KeyEventHandler KeyDownHandler; 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; } } #endregion public MainPage() { InitializeComponent(); DataContext = this; if (pdfFormControl != null) { pdfFormControl = new FromPropertyControl(); } } #region Initial Load Unload public void InitialPDFViewControl(PDFViewControl newPDFViewer) { pdfAnnotationControl.SetPDFViewer(newPDFViewer.PDFView); FormBarControl.InitWithPDFViewer(newPDFViewer.PDFView, pdfFormControl); pdfFormControl.SetPDFViewer(newPDFViewer.PDFView); pdfContentEditControl.InitWithPDFViewer(newPDFViewer.PDFView); pdfAnnotationControl.AnnotationCancel(); AnnotationBarControl.ClearAllToolState(); ExpandRightPropertyPanel(null, Visibility.Collapsed); newPDFViewer.PDFView.WidgetClickHandler += PDFView_WidgetClickHandler; newPDFViewer.CustomSignHandle = true; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { pdfAnnotationControl = new CPDFAnnotationControl(); BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search | BOTATools.Annotation); LoadDocument(); } public void SetPDFViewer(PDFViewControl newPdfViewer) { if (newPdfViewer != null) { pdfViewControl = newPdfViewer; } } private void CPDFTitleBarControl_Loaded(object sender, RoutedEventArgs e) { CPDFTitleBarControl.OpenFileEvent -= CPDFTitleBarControl_OpenFileEvent; CPDFTitleBarControl.OpenFileEvent += CPDFTitleBarControl_OpenFileEvent; CPDFTitleBarControl.SaveAsFileEvent -= CPDFTitleBarControl_SaveAsFileEvent; CPDFTitleBarControl.SaveAsFileEvent += CPDFTitleBarControl_SaveAsFileEvent; CPDFTitleBarControl.SaveFileEvent -= CPDFTitleBarControl_SaveFileEvent; CPDFTitleBarControl.SaveFileEvent += CPDFTitleBarControl_SaveFileEvent; ; } private void AnnotationBarControl_Unloaded(object sender, RoutedEventArgs e) { AnnotationBarControl.AnnotationPropertyChanged -= AnnotationBarControl_AnnotationPropertyChanged; AnnotationBarControl.AnnotationCancel -= AnnotationBarControl_AnnotationCancel; } 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; } #endregion #region UI private string GetTime() { DateTime dateTime = DateTime.Now; return " " + dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff"); } /// /// InfoChanged /// When the PDF zoom ratio changes, the value of the zoom control also changes. /// private void PdfViewer_InfoChanged(object sender, KeyValuePair e) { if (e.Key == "Zoom") { CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100))); } } private void EditLink_Click(object sender, RoutedEventArgs e) { RightPanelButton.IsChecked = true; PropertyContainer.Visibility = Visibility.Visible; } 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 PDFView_AnnotEditHandler(object sender, List e) { BotaSideTool.LoadAnnotationList(); } private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged(e.PropertyName); } private void PasswordUI_Closed(object sender, EventArgs e) { PopupBorder.Visibility = Visibility.Collapsed; PasswordUI.Visibility = Visibility.Collapsed; } private void PasswordUI_Canceled(object sender, EventArgs e) { PopupBorder.Visibility = Visibility.Collapsed; PasswordUI.Visibility = Visibility.Collapsed; } 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(); FileChangeEvent?.Invoke(null, EventArgs.Empty); } else { PasswordUI.SetShowError("error", Visibility.Visible); } } } private void SaveFileBtn_Click(object sender, RoutedEventArgs e) { SaveFile(); } private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; } if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { pdfContentEditControl.SetPDFImageEditData(lastPDFEditEvent); } } 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 PDFTextEditButton_Click(object sender, RoutedEventArgs e) { ToggleButton senderBtn = sender as ToggleButton; if (senderBtn != null && pdfViewControl != null) { ClearPDFEditState(senderBtn); if (senderBtn.IsChecked == true) { PDFEditEvent createParam = new PDFEditEvent(); createParam.EditType = CPDFEditType.EditText; createParam.IsBold = false; createParam.IsItalic = false; createParam.FontSize = 14; createParam.FontName = "Helvetica"; createParam.FontColor = Colors.Black; createParam.TextAlign = TextAlignType.AlignLeft; createParam.Transparency = 255; if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { CPDFDocument pdfDoc = pdfViewControl.PDFView.Document; if (pdfDoc.PageCount > 0) { CPDFPage pdfPage = pdfDoc.PageAtIndex(0); CPDFEditPage editPage = pdfPage.GetEditPage(); editPage.BeginEdit(CPDFEditType.EditText); createParam.SystemFontNameList.AddRange(editPage.GetFontList()); editPage.EndEdit(); } } pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText); pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditText); pdfViewControl.PDFView?.SetPDFEditParam(createParam); pdfContentEditControl.SetPDFTextEditData(createParam); ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible); if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; } RightPanelButton.IsChecked = true; pdfTextCreateParam = createParam; } else { pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None); pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText); RightPanelButton.IsChecked = false; if ((bool)ViewSettingBtn.IsChecked) { return; } pdfContentEditControl.ClearContentControl(); ExpandRightPropertyPanel(null, Visibility.Collapsed); } } } private void PDFImageEditButton_Click(object sender, RoutedEventArgs e) { ToggleButton senderBtn = sender as ToggleButton; if (senderBtn != null && pdfViewControl != null) { senderBtn.IsChecked = false; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { ClearPDFEditState(senderBtn); pdfViewControl.PDFView?.ClearSelectPDFEdit(); pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText); RightPanelButton.IsChecked = false; if (PropertyContainer.Child != null && !(bool)ViewSettingBtn.IsChecked) { ExpandRightPropertyPanel(null, Visibility.Collapsed); } pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage); pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName); RightPanelButton.IsChecked = false; } } } private void UndoBtn_Click(object sender, RoutedEventArgs e) { if (pdfViewControl != null && pdfViewControl.PDFView != null) { pdfViewControl.PDFView.UndoManager?.Undo(); } } private void RedoBtn_Click(object sender, RoutedEventArgs e) { if (pdfViewControl != null && pdfViewControl.PDFView != null) { pdfViewControl.PDFView.UndoManager?.Redo(); } } private void ClearPDFEditState(ToggleButton ignoreBtn = null) { List clearBtnList = new List() { PDFTextEditButton, PDFImageEditButton }; foreach (ToggleButton item in clearBtnList) { if (ignoreBtn == item) { continue; } item.IsChecked = false; } } private void PdfAnnotationControl_ClearAnnotationBar(object sender, EventArgs e) { AnnotationBarControl.ClearAllToolState(); } private void AnnotationBarControl_AnnotationCancel(object sender, EventArgs e) { pdfAnnotationControl.AnnotationCancel(); ExpandRightPropertyPanel(null, Visibility.Collapsed); RightPanelButton.IsChecked = false; ViewSettingBtn.IsChecked = false; } #endregion #region ContextMenu /// /// Annotation Context menu /// private void PDFView_AnnotCommandHandler(object sender, ComPDFKitViewer.AnnotEvent.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.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 }); } else { e.Handle = true; e.PopupMenu = new ContextMenu(); if (currentMode == "Annotations" || currentMode == "Forms") { 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.WidgetView) { 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 = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); } 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.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.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; } } /// /// TextEdit context menu. /// private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand) { editCommand.PopupMenu = new ContextMenu(); if (lastPDFEditEvent != null) { editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); } else { editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); if (editCommand.TextAreaCopied) { editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender }); } } } /// /// ImageEdit right-click menu. /// private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand) { editCommand.PopupMenu = new ContextMenu(); if (lastPDFEditEvent != null) { MenuItem rotateLeftMenu = new MenuItem(); rotateLeftMenu.Header = "Left Rotate"; rotateLeftMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.Rotate = -90; lastPDFEditEvent.UpdatePDFEditByEventArgs(); pdfContentEditControl.RefreshThumb(); } }; editCommand.PopupMenu.Items.Add(rotateLeftMenu); MenuItem rotateRightMenu = new MenuItem(); rotateRightMenu.Header = "Right Rotate"; rotateRightMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.Rotate = 90; lastPDFEditEvent.UpdatePDFEditByEventArgs(); pdfContentEditControl.RefreshThumb(); } }; editCommand.PopupMenu.Items.Add(rotateRightMenu); MenuItem replaceMenu = new MenuItem(); replaceMenu.Header = "Replace"; replaceMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { lastPDFEditEvent.ReplaceImagePath = openFileDialog.FileName; lastPDFEditEvent.UpdatePDFEditByEventArgs(); pdfViewControl.PDFView?.ClearSelectPDFEdit(); pdfContentEditControl.RefreshThumb(); } } }; editCommand.PopupMenu.Items.Add(replaceMenu); MenuItem exportMenu = new MenuItem(); exportMenu.Header = "Export"; exportMenu.Click += (o, p) => { if (pdfViewControl != null && pdfViewControl.PDFView != null) { Dictionary> imageDict = pdfViewControl.PDFView.GetSelectedImages(); if (imageDict != null && imageDict.Count > 0) { System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog(); if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string choosePath = folderBrowser.SelectedPath; string openPath = choosePath; foreach (int pageIndex in imageDict.Keys) { List imageList = imageDict[pageIndex]; foreach (Bitmap image in imageList) { string savePath = System.IO.Path.Combine(choosePath, Guid.NewGuid() + ".jpg"); image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); openPath = savePath; } } Process.Start("explorer", "/select,\"" + openPath + "\""); } } } }; editCommand.PopupMenu.Items.Add(exportMenu); MenuItem opacityMenu = new MenuItem(); opacityMenu.Header = "Opacity"; editCommand.PopupMenu.Items.Add(opacityMenu); AppendOpacityMenu(opacityMenu); MenuItem horizonMirror = new MenuItem(); horizonMirror.Header = "HMirror"; horizonMirror.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.HorizontalMirror = true; lastPDFEditEvent.UpdatePDFEditByEventArgs(); } }; editCommand.PopupMenu.Items.Add(horizonMirror); MenuItem verticalMirror = new MenuItem(); verticalMirror.Header = "VMirror"; verticalMirror.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.VerticalMirror = true; lastPDFEditEvent.UpdatePDFEditByEventArgs(); } }; editCommand.PopupMenu.Items.Add(verticalMirror); MenuItem cropMenu = new MenuItem(); cropMenu.Header = "Crop"; cropMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.ClipImage = true; lastPDFEditEvent.UpdatePDFEditByEventArgs(); } }; editCommand.PopupMenu.Items.Add(cropMenu); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); if (editCommand.TextAreaCopied) { editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste And Match Style", Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender }); } } else { editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); } } /// /// ImageEdit transparency submenu. /// private void AppendOpacityMenu(MenuItem parentMenu) { List opacityList = new List() { 25,50,75,100 }; foreach (int opacity in opacityList) { MenuItem opacityMenu = new MenuItem(); opacityMenu.Header = string.Format("{0}%", opacity); opacityMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.Transparency = (int)Math.Ceiling(opacity * 255 / 100D); lastPDFEditEvent.UpdatePDFEditByEventArgs(); } }; parentMenu.Items.Add(opacityMenu); } } 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; Dictionary> imageDict = pdfViewControl.PDFView?.GetSelectedImages(); if (imageDict != null && imageDict.Count > 0) { foreach (int pageIndex in imageDict.Keys) { List 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 + "\""); } } private void CopyImage_Click(object sender, RoutedEventArgs e) { Dictionary> imageDict = pdfViewControl.PDFView?.GetSelectedImages(); if (imageDict != null && imageDict.Count > 0) { foreach (int pageIndex in imageDict.Keys) { List 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; } } } } #endregion #region Property changed protected void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } #endregion #region Open and save file public void InitWithFilePath(string filePath) { pdfViewControl = new PDFViewControl(); pdfViewControl.PDFView.InitDocument(filePath); } private void LoadDocument() { if (pdfViewControl.PDFView.Document == null) { return; } //Load the Document pdfViewControl.PDFView?.Load(); //Highlight the link annotation pdfViewControl.PDFView?.SetShowLink(true); //Add PDFViewControl to PDFGrid PDFGrid.Child = pdfViewControl; // Register the InfoChanged event handler, // which is used to notify the CPDFScalingControl of real-time changes in the displayed number when the page size changes. pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged; pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged; // pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler; pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler; // pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler; pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler; //Can undo, can redo pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged; pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged; pdfViewControl.PDFView.SetFormFieldHighlight(true); PasswordUI.Closed -= PasswordUI_Closed; PasswordUI.Canceled -= PasswordUI_Canceled; PasswordUI.Confirmed -= PasswordUI_Confirmed; PasswordUI.Confirmed += PasswordUI_Confirmed; PasswordUI.Canceled += PasswordUI_Canceled; PasswordUI.Closed += PasswordUI_Closed; pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth); CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView); CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100))); InitialPDFViewControl(pdfViewControl); pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer); FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView); BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView); BotaSideTool.SelectBotaTool(BOTATools.Thumbnail); ViewSettingBtn.IsChecked = false; PropertyContainer.Child = null; PropertyContainer.Visibility = Visibility.Collapsed; ModeComboBox.SelectedIndex = 0; if (KeyDownHandler != null) { pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler); } KeyDownHandler = new KeyEventHandler(PDFView_KeyDown); pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true); } /// /// Save the file to another PDF file. /// 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); } } } } /// /// Save the file in the current path. /// public void SaveFile() { if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { CPDFDocument pdfDoc = pdfViewControl.PDFView.Document; if (pdfDoc.WriteToLoadedPath()) { pdfViewControl.PDFView.UndoManager.CanSave = false; return; } SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.Filter = "(*.pdf)|*.pdf"; saveDialog.DefaultExt = ".pdf"; saveDialog.OverwritePrompt = true; if (saveDialog.ShowDialog() == true) { pdfDoc.WriteToFilePath(saveDialog.FileName); } } } public void OpenFile() { string filePath = CommonHelper.GetFilePathOrEmpty(); string oldFilePath = pdfViewControl.PDFView.Document.FilePath; if (!string.IsNullOrEmpty(filePath) && pdfViewControl != null) { if (pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { if (oldFilePath.ToLower() == filePath.ToLower()) { return; } } if ((bool)CheckExistBeforeOpenFileEvent?.Invoke(new string[] { filePath, oldFilePath })) { 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(); FileChangeEvent?.Invoke(null, EventArgs.Empty); } } } private void OpenFile_Click(object sender, RoutedEventArgs e) { OpenFile(); } #endregion #region ToolPanel expand and collapse 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); } } private void ControlLeftPanel() { if (LeftToolPanelButton != null) { bool isExpand = LeftToolPanelButton.IsChecked == true; ExpandLeftPanel(isExpand); } } private void ControlRightPanel() { if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; } if (RightPanelButton != null) { if (RightPanelButton.IsChecked == true) { if (currentMode == "Annotation") { ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible); } else if (currentMode == "Edit") { ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible); } else if (currentMode == "Forms") { ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible); } else if (currentMode == "Content Editor") { ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible); } else { ExpandRightPropertyPanel(pdfAnnotationControl, Visibility.Visible); } } else { ExpandRightPropertyPanel(null, Visibility.Collapsed); } } } private void LeftToolPanelButton_Click(object sender, RoutedEventArgs e) { ControlLeftPanel(); } private void ExpandSearchBtn_Click(object sender, RoutedEventArgs e) { ExpandLeftPanel(true); LeftToolPanelButton.IsChecked = true; BotaSideTool.SelectBotaTool(BOTATools.Search); } private void ShowViewSettings() { if (ViewSettingBtn != null) { if (ViewSettingBtn.IsChecked == true) { 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(); } public void ExpandRightPropertyPanel(UIElement properytPanel, Visibility visible) { PropertyContainer.Width = 260; PropertyContainer.Child = properytPanel; PropertyContainer.Visibility = visible; if (visible == Visibility.Hidden || visible == Visibility.Collapsed) { RightPanelButton.IsChecked = false; } } private void RightPanelButton_Click(object sender, RoutedEventArgs e) { ControlRightPanel(); } #endregion #region Event handle 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) { if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; RightPanelButton.IsChecked = true; } PropertyContainer.Child = pdfAnnotationControl; pdfAnnotationControl.SetAnnotEventData(e); } private void PDFView_WidgetClickHandler(object sender, WidgetArgs e) { if (e is WidgetSignArgs) { RightPanelButton.IsChecked = true; ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible); pdfFormControl.SetPropertyForType(e, null); } else { RightPanelButton.IsChecked = false; ExpandRightPropertyPanel(null, Visibility.Collapsed); } } private void PDFView_PDFEditCommandHandler(object sender, PDFEditCommand e) { if (e == null) { return; } if (e.EditType == CPDFEditType.EditText) { e.Handle = true; PDFEditTextContextMenu(sender, e); } if (e.EditType == CPDFEditType.EditImage) { e.Handle = true; PDFEditImageContextMenu(sender, e); } } /// /// Text and Image Selected Event /// private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e) { PropertyContainer.Child = pdfContentEditControl; lastPDFEditEvent = e; if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; RightPanelButton.IsChecked = true; ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible); } if (e == null) { PropertyContainer.Child = pdfContentEditControl; if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null) { if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText) { pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam); } else if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.None) { pdfContentEditControl.ClearContentControl(); } } else { pdfContentEditControl.ClearContentControl(); } return; } if (e.EditType == CPDFEditType.EditText) { pdfContentEditControl.SetPDFTextEditData(e, true); return; } if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; } pdfContentEditControl.SetPDFImageEditData(e); return; } } private void PDFView_FormEditHandler(object sender, List e) { if (e != null && e.Count > 0) { AnnotEditEvent editEvent = e[e.Count - 1]; if (editEvent.EditAction == ActionType.Add) { if (pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs != null) { WidgetArgs widgetArgs = pdfViewControl.PDFView.ToolManager.CurrentAnnotArgs as WidgetArgs; if (widgetArgs != null) { switch (widgetArgs.WidgeType) { case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON: widgetArgs.FieldName = "Button" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_CHECKBOX: widgetArgs.FieldName = "Checkbox" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_RADIOBUTTON: widgetArgs.FieldName = "Radio button" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_TEXTFIELD: widgetArgs.FieldName = "Text" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX: widgetArgs.FieldName = "Combobox" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX: widgetArgs.FieldName = "List" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS: widgetArgs.FieldName = "Signature" + GetTime(); break; case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_UNKNOWN: break; default: break; } } } pdfViewControl.PDFView.SelectAnnotation(editEvent.PageIndex, editEvent.AnnotIndex); } else if (editEvent.EditAction == ActionType.Del) { pdfFormControl.CleanProperty(); } } } private void PDFView_FormActiveHandler(object sender, AnnotAttribEvent e) { if (e == null || e.IsAnnotCreateReset) { pdfFormControl.SetPropertyForType(null, null); } else { switch (e.GetAnnotTypes()) { case AnnotArgsType.WidgetViewForm: WidgetArgs formArgs = e.GetAnnotHandlerEventArgs(AnnotArgsType.WidgetViewForm).First() as WidgetArgs; switch (formArgs.WidgeType) { case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_PUSHBUTTON: case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_COMBOBOX: case ComPDFKit.PDFAnnotation.Form.C_WIDGET_TYPE.WIDGET_LISTBOX: ExpandRightPropertyPanel(pdfFormControl, Visibility.Visible); if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; } RightPanelButton.IsChecked = true; break; default: break; } pdfFormControl.SetPropertyForType(formArgs, e); break; } } } private void PageEditControl_ExitPageEdit(object sender, EventArgs e) { ModeComboBox.SelectedIndex = 0; } private void PageEditControl_PageMoved(object sender, RoutedEventArgs e) { if (pdfViewControl == null || pdfViewControl.PDFView == null) { return; } BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView); } private void CPDFPageEditBarControl_PageEditEvent(object sender, string e) { pageEditControl.PageEdit(e); } private void CPDFTitleBarControl_SaveFileEvent(object sender, EventArgs e) { SaveFile(); } private void CPDFTitleBarControl_SaveAsFileEvent(object sender, EventArgs e) { SaveAsFile(); } private void CPDFTitleBarControl_OpenFileEvent(object sender, EventArgs e) { OpenFile(); } public void PDFView_KeyDown(object sender, KeyEventArgs e) { if (pdfViewControl.PDFView.MouseMode != MouseModes.PDFEdit) { return; } if (Keyboard.Modifiers == ModifierKeys.Control) { if (e.Key == Key.Left) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, false); e.Handled = true; } if (e.Key == Key.Right) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, false); e.Handled = true; } if (e.Key == Key.Up) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, false); e.Handled = true; } if (e.Key == Key.Down) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, false); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.Shift) { if (e.Key == Key.Left) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreCharPlace, true); e.Handled = true; } if (e.Key == Key.Right) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextCharPlace, true); e.Handled = true; } if (e.Key == Key.Up) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeUpCharPlace, true); e.Handled = true; } if (e.Key == Key.Down) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeDownCharPlace, true); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.Alt) { if (e.SystemKey == Key.Up) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLocationLineBegin, false); e.Handled = true; } if (e.SystemKey == Key.Down) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeLineEnd, false); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.None) { if (e.Key == Key.Left) { pdfViewControl.PDFView.MoveEditArea(new Point(-5, 0)); e.Handled = true; } if (e.Key == Key.Right) { pdfViewControl.PDFView.MoveEditArea(new Point(5, 0)); e.Handled = true; } if (e.Key == Key.Up) { pdfViewControl.PDFView.MoveEditArea(new Point(0, -5)); e.Handled = true; } if (e.Key == Key.Down) { pdfViewControl.PDFView.MoveEditArea(new Point(0, 5)); e.Handled = true; } } if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift)) { if (e.Key == Key.Left) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypePreWord, true); e.Handled = true; } if (e.Key == Key.Right) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeNextWord, true); e.Handled = true; } if (e.Key == Key.Up) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionBegin, true); e.Handled = true; } if (e.Key == Key.Down) { pdfViewControl.PDFView.JumpEditingLoction(CEditingLocation.CEditingLoadTypeSectionEnd, true); e.Handled = true; } } } #endregion #region Change mode /// /// Actions performed when switching modes: /// Separated into actions performed upon entering and exiting the mode. /// private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { pdfTextCreateParam = null; if (isFirstLoad) { isFirstLoad = false; return; } var item = (sender as ComboBox).SelectedItem as ComboBoxItem; //Behavior when exit mode switch (currentMode) { case "Viewer": break; /* * Behavior when exit Edit mode * Clear and collapse the ToolBar * Clear And collapse the property panel. * Change the mousemode to PanTool * Unsubscribe the event handles * Clear the undo history */ case "Annotations": AnnotationBarControl.Visibility = Visibility.Collapsed; AnnotationBarControl.ClearAllToolState(); pdfAnnotationControl.ClearPanel(); if (!(bool)ViewSettingBtn.IsChecked) { ExpandRightPropertyPanel(null, Visibility.Collapsed); } pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool); pdfViewControl.PDFView.AnnotEditHandler -= PDFView_AnnotEditHandler; pdfAnnotationControl.ClearAnnotationBar -= PdfAnnotationControl_ClearAnnotationBar; pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_AnnotActiveHandler; pdfViewControl.PDFView?.UndoManager.ClearHistory(); break; /* * Behavior when exit edit mode: * Collapsed the ToolBar * Highlight the link annotation * Set the mode to not allow edit * Set the mouse mode to PanTool * Reload Document * Unsubscribe click event. * Clear And collapse the property panel. * Clear and collapse the ToolBar * Clear undo history * CPDFDisplaySettingsControl * Enable the configuration of the split view. */ case "Content Editor": PDFEditTool.Visibility = Visibility.Collapsed; pdfViewControl.PDFView?.SetShowLink(true); pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None); pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool); pdfViewControl.PDFView?.ReloadDocument(); pdfContentEditControl.ClearPanel(); pdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler; if (!(bool)ViewSettingBtn.IsChecked) { ExpandRightPropertyPanel(null, Visibility.Collapsed); } ClearPDFEditState(); pdfViewControl.PDFView?.UndoManager.ClearHistory(); if (displayPanel != null) { displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible); } break; /* * Behavior when exit form mode: * Clear and collapsed the ToolBar * Clear And Collapse the property panel. * Unsubscribe event. * Clear undo history */ case "Forms": if (!(bool)ViewSettingBtn.IsChecked) { ExpandRightPropertyPanel(null, Visibility.Collapsed); } FormBarControl.ClearAllToolState(); FormBarControl.Visibility = Visibility.Collapsed; pdfViewControl.PDFView?.SetMouseMode(MouseModes.PanTool); pdfViewControl.PDFView.AnnotActiveHandler -= PDFView_FormActiveHandler; pdfViewControl.PDFView.AnnotEditHandler -= PDFView_FormEditHandler; pdfViewControl.PDFView?.UndoManager.ClearHistory(); break; /* * Unsubscribe event. * Show Undo Redo Button * Collapse PageEditBarControl * Replace PDFGrid.Child to pdfViewControl. * Reload Document * Enable Button */ case "Docs Editor": pageEditControl.ExitPageEdit -= PageEditControl_ExitPageEdit; pageEditControl.PageMoved -= PageEditControl_PageMoved; PageEditBarControl.PageEditEvent -= CPDFPageEditBarControl_PageEditEvent; UndoRedoStackPanel.Visibility = Visibility.Visible; PageEditBarControl.Visibility = Visibility.Collapsed; PDFGrid.Child = pdfViewControl; FloatPageTool.Visibility = Visibility.Visible; pdfViewControl.PDFView.ReloadDocument(); LeftToolPanelButton.IsEnabled = true; SearchButton.IsEnabled = true; RightPanelButton.IsEnabled = true; ViewSettingBtn.IsEnabled = true; break; default: break; } /* * Behavior when switching to Viewer mode: * Hide the ToolBar * Set mousemode to Viewer */ if ((string)item.Content == "Viewer") { ToolBarContainer.Visibility = Visibility.Collapsed; pdfViewControl.PDFView.SetMouseMode(MouseModes.Viewer); } /* * Behavior when switching to Annotation mode: * Show the ToolBar * Show the AnnotationBar */ else if ((string)item.Content == "Annotations") { ToolBarContainer.Visibility = Visibility.Visible; AnnotationBarControl.Visibility = Visibility.Visible; pdfViewControl.PDFView.SetMouseMode(MouseModes.PanTool); pdfAnnotationControl.ClearAnnotationBar += PdfAnnotationControl_ClearAnnotationBar; pdfViewControl.PDFView.AnnotEditHandler += PDFView_AnnotEditHandler; pdfViewControl.PDFView.AnnotActiveHandler += PDFView_AnnotActiveHandler; } /* * Behavior when switching to Edit mode: * Show the ToolBar * Show the EditBar * Set the mode to allow editing of both text and images * Set the mouse mode to PDFEdit * Reload Document * Set the splite mode to None and unable the split setting */ else if ((string)item.Content == "Content Editor") { ToolBarContainer.Visibility = Visibility.Visible; PDFEditTool.Visibility = Visibility.Visible; PageEditBarControl.Visibility = Visibility.Collapsed; pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage); pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None); pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit); pdfViewControl.PDFView?.ReloadDocument(); pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler; pdfViewControl.PDFView.SetSplitMode(SplitMode.None); if (displayPanel != null) { displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed); } } /* * Behavior when switching to Form mode: * */ else if ((string)item.Content == "Forms") { ToolBarContainer.Visibility = Visibility.Visible; FormBarControl.Visibility = Visibility.Visible; pdfViewControl.PDFView?.SetShowLink(true); pdfViewControl.PDFView?.SetMouseMode(MouseModes.FormEditTool); pdfViewControl.PDFView?.ReloadDocument(); pdfViewControl.PDFView.AnnotActiveHandler += PDFView_FormActiveHandler; pdfViewControl.PDFView.AnnotEditHandler += PDFView_FormEditHandler; } /* * */ else if ((string)item.Content == "Docs Editor") { ToolBarContainer.Visibility = Visibility.Visible; if (pageEditControl == null) { pageEditControl = new CPDFPageEditControl(); } pageEditControl.ExitPageEdit += PageEditControl_ExitPageEdit; ; pageEditControl.PageMoved += PageEditControl_PageMoved; ; PageEditBarControl.PageEditEvent += CPDFPageEditBarControl_PageEditEvent; pageEditControl.LoadThumbnails(pdfViewControl.PDFView); PDFGrid.Child = pageEditControl; UndoRedoStackPanel.Visibility = Visibility.Collapsed; PageEditBarControl.Visibility = Visibility.Visible; LeftToolPanelButton.IsChecked = false; LeftToolPanelButton.IsEnabled = false; SearchButton.IsEnabled = false; RightPanelButton.IsChecked = false; RightPanelButton.IsEnabled = false; ViewSettingBtn.IsChecked = false; ViewSettingBtn.IsEnabled = false; ExpandLeftPanel(false); ExpandRightPropertyPanel(null, Visibility.Collapsed); FloatPageTool.Visibility = Visibility.Collapsed; } currentMode = (string)item.Content; } #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) { if (currentMode != "Docs Editor") { LeftToolPanelButton.IsChecked = !LeftToolPanelButton.IsChecked; ControlLeftPanel(); } } private void CommandBinding_Executed_ControlRightPanel(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { RightPanelButton.IsChecked = !RightPanelButton.IsChecked; ControlRightPanel(); } } private void CommandBinding_Executed_Bookmark(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { ExpandLeftPanel(true); LeftToolPanelButton.IsChecked = true; BotaSideTool.SelectBotaTool(BOTATools.Bookmark); } } private void CommandBinding_Executed_Outline(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { ExpandLeftPanel(true); LeftToolPanelButton.IsChecked = true; BotaSideTool.SelectBotaTool(BOTATools.Outline); } } private void CommandBinding_Executed_Thumbnail(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { ExpandLeftPanel(true); LeftToolPanelButton.IsChecked = true; BotaSideTool.SelectBotaTool(BOTATools.Thumbnail); } } private void CommandBinding_Executed_Annotation(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { ExpandLeftPanel(true); LeftToolPanelButton.IsChecked = true; BotaSideTool.SelectBotaTool(BOTATools.Annotation); } } private void CommandBinding_Executed_Search(object sender, ExecutedRoutedEventArgs e) { if (currentMode != "Docs Editor") { 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) { if (currentMode != "Docs Editor") { 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; this.Focus(); } } private void CommandBinding_Executed_Highlight(object sender, ExecutedRoutedEventArgs e) { if (currentMode == "Annotations") { AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Highlight); } } private void CommandBinding_Executed_Underline(object sender, ExecutedRoutedEventArgs e) { if (currentMode == "Annotations") { AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Underline); } } private void CommandBinding_Executed_Strikeout(object sender, ExecutedRoutedEventArgs e) { if (currentMode == "Annotations") { AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Strikeout); } } private void CommandBinding_Executed_Squiggly(object sender, ExecutedRoutedEventArgs e) { if (currentMode == "Annotations") { AnnotationBarControl.SetAnnotationType(CPDFAnnotationType.Squiggly); } } #endregion } }