using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKit.PDFPage.Edit; 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.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 Point = System.Windows.Point; namespace ContentEditor { public partial class MainWindow : Window, INotifyPropertyChanged { #region Property 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 bool CanSave { get { if (pdfViewControl != null && pdfViewControl.PDFView != null) { return pdfViewControl.PDFView.UndoManager.CanSave; } return false; } } /// /// The last PDF edit object /// private PDFEditEvent lastPDFEditEvent = null; private PDFViewControl passwordViewer; private PDFViewControl pdfViewControl; private PDFImageEditControl imageEditControl = new PDFImageEditControl(); private PDFTextEditControl textEditControl = new PDFTextEditControl(); private Border empytPanel = new Border(); private UIElement prevPanel = null; private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; public event PropertyChangedEventHandler PropertyChanged; private PDFEditEvent pdfTextCreateParam; private KeyEventHandler KeyDownHandler; #endregion public MainWindow() { InitializeComponent(); Loaded += MainWindow_Loaded; DataContext = this; } private void MainWindow_Loaded(object sender, RoutedEventArgs e) { TextBlock emptyBlock = new TextBlock(); emptyBlock.HorizontalAlignment = HorizontalAlignment.Center; emptyBlock.VerticalAlignment = VerticalAlignment.Center; empytPanel.Child = emptyBlock; empytPanel.Width = 260; BotaSideTool.AddBOTAContent(BOTATools.Thumbnail | BOTATools.Outline | BOTATools.Bookmark | BOTATools.Search); LoadDefaultDocument(); } #region Load document private void LoadDocument() { if (pdfViewControl.PDFView.Document == null) { return; } pdfViewControl.PDFView?.Load(); PDFGrid.Child = pdfViewControl; pdfViewControl.PDFView.InfoChanged -= PdfViewer_InfoChanged; pdfViewControl.PDFView.InfoChanged += PdfViewer_InfoChanged; 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.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler; pdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler; pdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged; pdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged; pdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler; pdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler; pdfViewControl.PDFView.AnnotCommandHandler -= PDFView_AnnotCommandHandler; pdfViewControl.PDFView.AnnotCommandHandler += PDFView_AnnotCommandHandler; pdfViewControl.PDFView.SetFormFieldHighlight(true); pdfViewControl.PDFView.ChangeFitMode(FitMode.FitWidth); CPDFSaclingControl.InitWithPDFViewer(pdfViewControl.PDFView); CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)(pdfViewControl.PDFView.ZoomFactor * 100))); FloatPageTool.InitWithPDFViewer(pdfViewControl.PDFView); BotaSideTool.InitWithPDFViewer(pdfViewControl.PDFView); BotaSideTool.SelectBotaTool(BOTATools.Thumbnail); ViewSettingBtn.IsChecked = false; PropertyContainer.Child = null; PropertyContainer.Visibility = Visibility.Collapsed; SetEditMode(); if (KeyDownHandler != null) { pdfViewControl.PDFView.RemoveHandler(KeyDownEvent, KeyDownHandler); } KeyDownHandler = new KeyEventHandler(PDFView_KeyDown); pdfViewControl.PDFView.AddHandler(KeyDownEvent, KeyDownHandler, false, true); } private void LoadDefaultDocument() { string defaultFilePath = "PDF32000_2008.pdf"; pdfViewControl = new PDFViewControl(); pdfViewControl.PDFView.InitDocument(defaultFilePath); LoadDocument(); } #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 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(260); 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) { if (e != null && e.CommandType == CommandType.Context) { if (e.PressOnSelectedText) { e.Handle = true; e.PopupMenu = new ContextMenu(); e.PopupMenu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, 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); } } else { e.Handle = true; e.PopupMenu = new ContextMenu(); 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); } } if (e != null && e.CommandType == CommandType.Copy) { e.DoCommand(); } } private void CopyImage_Click(object sender, RoutedEventArgs e) { try { 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; } } } } 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> 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 + "\""); } catch (Exception ex) { } } } private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand) { editCommand.PopupMenu = new ContextMenu(); if (ApplicationCommands.Copy.CanExecute(null, (UIElement)sender) && lastPDFEditEvent != null) { MenuItem rotateLeftMenu = new MenuItem(); rotateLeftMenu.Header = "Rotate Left"; rotateLeftMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.Rotate = -90; lastPDFEditEvent.UpdatePDFEditByEventArgs(); imageEditControl?.SetImageThumb(); } }; editCommand.PopupMenu.Items.Add(rotateLeftMenu); MenuItem rotateRightMenu = new MenuItem(); rotateRightMenu.Header = "Rotate Right"; rotateRightMenu.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.Rotate = 90; lastPDFEditEvent.UpdatePDFEditByEventArgs(); imageEditControl?.SetImageThumb(); } }; 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(); } } }; 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; try { 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 + "\""); } catch (Exception ex) { } } } } }; 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 = "Flip horizontal"; horizonMirror.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.HorizontalMirror = true; lastPDFEditEvent.UpdatePDFEditByEventArgs(); imageEditControl?.SetImageThumb(); } }; editCommand.PopupMenu.Items.Add(horizonMirror); MenuItem verticalMirror = new MenuItem(); verticalMirror.Header = "Flip vertical"; verticalMirror.Click += (o, p) => { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { lastPDFEditEvent.VerticalMirror = true; lastPDFEditEvent.UpdatePDFEditByEventArgs(); imageEditControl?.SetImageThumb(); } }; 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 }); } } 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)(opacity * 255 / 100D); lastPDFEditEvent.UpdatePDFEditByEventArgs(); imageEditControl?.SetImageTransparency(lastPDFEditEvent.Transparency); imageEditControl?.SetImageThumb(); } }; parentMenu.Items.Add(opacityMenu); } } 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 }); } } } #endregion #region UI /// /// Check the zoom factor /// 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 PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; } SetImageEditProperty(); } private void ControlRightPanel() { if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; } if (RightPanelButton != null) { if (RightPanelButton.IsChecked == true) { if (PropertyContainer.Child is CPDFDisplaySettingsControl) { PropertyContainer.Child = prevPanel; } if (PropertyContainer.Child == null) { PropertyContainer.Child = empytPanel; } PropertyContainer.Visibility = Visibility.Visible; } else { PropertyContainer.Visibility = Visibility.Collapsed; } } } private void RightPanelButton_Click(object sender, RoutedEventArgs e) { ControlRightPanel(); } 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) { LeftToolPanelButton.IsChecked = true; ExpandLeftPanel(true); BotaSideTool.SelectBotaTool(BOTATools.Search); } private void ShowViewSettings() { if (RightPanelButton != null) { RightPanelButton.IsChecked = false; } if (ViewSettingBtn != null) { if (ViewSettingBtn.IsChecked == true) { CPDFDisplaySettingsControl displayPanel = new CPDFDisplaySettingsControl(); displayPanel.InitWithPDFViewer(pdfViewControl.PDFView); if (ViewComboBox.SelectedIndex == 1) { displayPanel.SetVisibilityWhenContentEdit(Visibility.Collapsed); } else { displayPanel.SetVisibilityWhenContentEdit(Visibility.Visible); } prevPanel = PropertyContainer.Child; PropertyContainer.Child = displayPanel; PropertyContainer.Visibility = Visibility.Visible; } else { PropertyContainer.Child = null; PropertyContainer.Visibility = Visibility.Collapsed; } } } private void ViewSettingBtn_Click(object sender, RoutedEventArgs e) { ShowViewSettings(); } private void NextPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex + 1); } private void PrevPageBorder_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { pdfViewControl.PDFView?.GoToPage(pdfViewControl.PDFView.CurrentIndex - 1); } 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(); } 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); if (textEditControl == null) { textEditControl = new PDFTextEditControl(); } textEditControl.SetPDFTextEditData(createParam); PropertyContainer.Child = textEditControl; PropertyContainer.Visibility = 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; } else { if (PropertyContainer.Child != null) { PropertyContainer.Child = empytPanel; } } PropertyContainer.Visibility = 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) { PropertyContainer.Child = empytPanel; PropertyContainer.Visibility = Visibility.Collapsed; } pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage); pdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName); } } } 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 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(); } } #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 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); } } private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e) { PDFEditEvent tempEvent = lastPDFEditEvent; lastPDFEditEvent = e; if ((bool)ViewSettingBtn.IsChecked) { ViewSettingBtn.IsChecked = false; RightPanelButton.IsChecked = true; } if (e == null) { PropertyContainer.Child = empytPanel; if (pdfTextCreateParam != null && pdfViewControl != null && pdfViewControl.PDFView != null) { if (pdfViewControl.PDFView.GetPDFEditCreateType() == CPDFEditType.EditText) { if (textEditControl == null) { textEditControl = new PDFTextEditControl(); } textEditControl.SetPDFTextEditData(pdfTextCreateParam); PropertyContainer.Child = textEditControl; } } return; } if (Mouse.RightButton == MouseButtonState.Pressed && PropertyContainer.Visibility != Visibility.Visible) { return; } if (e.EditType == CPDFEditType.EditText) { if (textEditControl == null) { textEditControl = new PDFTextEditControl(); } textEditControl.SetPDFTextEditData(e); PropertyContainer.Child = textEditControl; return; } if (e.EditType == CPDFEditType.EditImage && pdfViewControl != null) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; } if (Mouse.RightButton == MouseButtonState.Pressed) { SetImageEditProperty(); } return; } } #endregion #region Save file /// /// 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); } } } } private void SaveFile() { if (pdfViewControl != null && pdfViewControl.PDFView != null && pdfViewControl.PDFView.Document != null) { 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); } } } #endregion #region Selected changed private void PdfViewer_InfoChanged(object sender, KeyValuePair e) { if (e.Key == "Zoom") { CPDFSaclingControl.SetZoomTextBoxText(string.Format("{0}", (int)((double)e.Value * 100))); } } private void SetImageEditProperty() { if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) { if (imageEditControl == null) { imageEditControl = new PDFImageEditControl(); } imageEditControl.InitWithPDFViewer(pdfViewControl.PDFView); imageEditControl.SetPDFImageEditData(lastPDFEditEvent); PropertyContainer.Child = imageEditControl; } } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { SetEditMode(); } private void SetEditMode() { ClearPDFEditState(null); if (pdfViewControl != null && ViewComboBox != null) { if (ViewComboBox.SelectedIndex == 0) { PDFEditTool.Visibility = Visibility.Collapsed; pdfViewControl.PDFView?.SetShowLink(true); pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.None); pdfViewControl.PDFView?.SetMouseMode(MouseModes.Viewer); pdfViewControl.PDFView?.ReloadDocument(); } if (ViewComboBox.SelectedIndex == 1) { PDFEditTool.Visibility = Visibility.Visible; pdfViewControl.PDFView?.SetShowLink(false); pdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditText | CPDFEditType.EditImage); pdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None); pdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit); pdfViewControl.PDFView?.ReloadDocument(); } RightPanelButton.IsChecked = false; ViewSettingBtn.IsChecked = false; PropertyContainer.Visibility = Visibility.Collapsed; if (PropertyContainer != null) { PropertyContainer.Child = null; } } } 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 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_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_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 } }