using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKit.PDFPage.Edit; using ComPDFKit.Tool; using ComPDFKit.Tool.SettingParam; using Compdfkit_Tools.Edit; using Compdfkit_Tools.Helper; using ComPDFKitViewer; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.CompilerServices; using System.Windows; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using static ComPDFKit.Tool.CPDFToolManager; using System.Windows.Media.Imaging; using ComPDFKit.Import; using ComPDFKit.Tool.Help; using ComPDFKit.Tool.UndoManger; using ComPDFKit.Viewer.Helper; using ContextMenu = System.Windows.Controls.ContextMenu; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using KeyEventHandler = System.Windows.Input.KeyEventHandler; using MenuItem = System.Windows.Controls.MenuItem; using OpenFileDialog = Microsoft.Win32.OpenFileDialog; using UserControl = System.Windows.Controls.UserControl; namespace Compdfkit_Tools.PDFControl { public partial class ContentEditControl : UserControl, INotifyPropertyChanged { #region Property public PDFViewControl PdfViewControl; public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl(); private CPDFDisplaySettingsControl displaySettingsControl = null; private TextEditParam pdfTextCreateParam; private PDFEditParam imageAreaParam = null; private List lastPDFEditMultiEvents = null; private PanelState panelState = PanelState.GetInstance(); private KeyEventHandler KeyDownHandler; public event PropertyChangedEventHandler PropertyChanged; public ICommand CloseTabCommand; public ICommand ExpandPropertyPanelCommand; private CPoint startPoint; private CPoint endPoint; private bool isUpdateStartPoint = true; public bool CanUndo { get { try { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null) { CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool; CPDFViewer pdfViewer = viewerTool.GetCPDFViewer(); return pdfViewer.UndoManager.CanUndo; } } catch (Exception ex) { } return false; } } public bool CanRedo { get { try { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null) { CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool; CPDFViewer pdfViewer = viewerTool.GetCPDFViewer(); return pdfViewer.UndoManager.CanRedo; } } catch (Exception ex) { } return false; } } private bool CanSave { get { try { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null) { CPDFViewerTool viewerTool = PdfViewControl.PDFViewTool; CPDFViewer pdfViewer = viewerTool.GetCPDFViewer(); return (pdfViewer.UndoManager.CanUndo | pdfViewer.UndoManager.CanRedo); } } catch (Exception ex) { } return false; } } public event EventHandler OnCanSaveChanged; public event EventHandler OnAnnotEditHandler; #endregion public ContentEditControl() { InitializeComponent(); panelState.PropertyChanged -= PanelState_PropertyChanged; panelState.PropertyChanged += PanelState_PropertyChanged; } private void PanelState_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(PanelState.IsLeftPanelExpand)) { ExpandLeftPanel(panelState.IsLeftPanelExpand); } else if (e.PropertyName == nameof(PanelState.RightPanel)) { if (panelState.RightPanel == PanelState.RightPanelState.PropertyPanel) { ExpandRightPropertyPanel(pdfContentEditControl, Visibility.Visible); } else if (panelState.RightPanel == PanelState.RightPanelState.ViewSettings) { ExpandRightPropertyPanel(displaySettingsControl, Visibility.Visible); } else { ExpandRightPropertyPanel(null, Visibility.Collapsed); } } } public void ClearPDFEditState(ToggleButton ignoreBtn = null) { List clearBtnList = new List() { PDFTextEditButton, PDFImageEditButton }; foreach (ToggleButton item in clearBtnList) { if (ignoreBtn == item) { continue; } item.IsChecked = false; } PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.None); PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(false); PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(false); } private void PDFTextEditButton_Click(object sender, RoutedEventArgs e) { ToggleButton senderBtn = sender as ToggleButton; if (senderBtn != null && PdfViewControl != null) { ClearPDFEditState(senderBtn); if (senderBtn.IsChecked == true) { TextEditParam textEditParam = new TextEditParam(); textEditParam.EditType = CPDFEditType.EditText; textEditParam.IsBold = false; textEditParam.IsItalic = false; textEditParam.FontSize = 14; textEditParam.FontName = "Helvetica"; textEditParam.FontColor = new byte[] { 0, 0, 0 }; textEditParam.EditIndex = -1; textEditParam.TextAlign = TextAlignType.AlignLeft; textEditParam.Transparency = 255; pdfContentEditControl.SetPDFTextEditData(textEditParam); DefaultSettingParam defaultSettingParam = PdfViewControl.PDFViewTool.GetDefaultSettingParam(); defaultSettingParam.SetPDFEditParamm(textEditParam); panelState.RightPanel = PanelState.RightPanelState.PropertyPanel; PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditText); } else { } // 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; // PdfViewControl.PDFView.ToolManager.EnableClickCreate = true; // PdfViewControl.PDFView.ToolManager.ClickCreateWidth = 100; // 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?.SetMouseMode(MouseModes.PDFEdit); // PdfViewControl.PDFView?.ReloadDocument(); // PdfViewControl.PDFView?.SetPDFEditParam(createParam); // pdfContentEditControl.SetPDFTextEditData(createParam); // panelState.RightPanel = PanelState.RightPanelState.PropertyPanel; // pdfTextCreateParam = createParam; //} //else //{ // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.None); // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText); // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit); // PdfViewControl.PDFView?.ReloadDocument(); // pdfContentEditControl.ClearContentControl(); // panelState.RightPanel = PanelState.RightPanelState.None; //} } } private void PDFImageEditButton_Click(object sender, RoutedEventArgs e) { ToggleButton senderBtn = sender as ToggleButton; if (senderBtn != null && PdfViewControl != null) { panelState.RightPanel = PanelState.RightPanelState.None; 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); try { bool SetImage = PdfViewControl.PDFViewTool.GetCPDFViewer().SetStampMouseImage(openFileDialog.FileName); PdfViewControl.PDFToolManager.SetCreateImagePath(openFileDialog.FileName); PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsVisibleCustomMouse(SetImage); PdfViewControl.PDFViewTool.GetCPDFViewer().SetIsShowStampMouse(SetImage); PdfViewControl.PDFToolManager.SetCreateContentEditType(CPDFEditType.EditImage); } catch { } //if (openFileDialog.ShowDialog() == true) //{ // ClearPDFEditState(senderBtn); // PdfViewControl.PDFView?.ClearSelectPDFEdit(); // PdfViewControl.PDFView?.SetPDFEditType(CPDFEditType.EditImage | CPDFEditType.EditText); // PdfViewControl.PDFView?.SetMouseMode(MouseModes.PDFEdit); // PdfViewControl.PDFView?.ReloadDocument(); // PdfViewControl.PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage); // PdfViewControl.PDFView?.AddPDFEditImage(openFileDialog.FileName); //} } } } public void ExpandLeftPanel(bool isExpand) { BotaContainer.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 UndoBtn_Click(object sender, RoutedEventArgs e) { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null) { PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo(); //PdfViewControl.PDFToolManager.ClearSelect(); PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateRenderFrame(); } } private void RedoBtn_Click(object sender, RoutedEventArgs e) { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null) { PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo(); //PdfViewControl.PDFToolManager.ClearSelect(); PdfViewControl.PDFViewTool.GetCPDFViewer().UpdateRenderFrame(); } } public void SetViewSettings(Visibility visibility, CPDFDisplaySettingsControl displaySettingsControl = null) { this.PropertyContainer.Child = displaySettingsControl; this.PropertyContainer.Visibility = visibility; } public void SetBOTAContainer(CPDFBOTABarControl botaControl) { this.BotaContainer.Child = botaControl; botaControl.ReplaceFunctionEnabled = true; } public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl) { this.displaySettingsControl = displaySettingsControl; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler; PdfViewControl.MouseRightButtonDownHandler += PdfViewControl_MouseRightButtonDownHandler; } private void UserControl_UnLoaded(object sender, RoutedEventArgs e) { PdfViewControl.MouseRightButtonDownHandler -= PdfViewControl_MouseRightButtonDownHandler; } public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible) { PropertyContainer.Width = 260; PropertyContainer.Child = propertytPanel; PropertyContainer.Visibility = visible; } public void InitWithPDFViewer(PDFViewControl view) { PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged; PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged; PdfViewControl.MouseLeftButtonDownHandler -= PDFToolManager_MouseLeftButtonDownHandler; PdfViewControl.MouseLeftButtonDownHandler += PDFToolManager_MouseLeftButtonDownHandler; PdfViewControl.MouseLeftButtonUpHandler -= PDFToolManager_MouseLeftButtonUpHandler; PdfViewControl.MouseLeftButtonUpHandler += PDFToolManager_MouseLeftButtonUpHandler; PdfViewControl = view; PDFGrid.Child = PdfViewControl; FloatPageTool.InitWithPDFViewer(view); pdfContentEditControl.InitWithPDFViewer(view); DataContext = this; if (PdfViewControl != null) { PdfViewControl.PDFViewTool.IsManipulationEnabled = true; //pdfViewer.PDFEditMultiActiveHandler -= PDFEditMultiActiveHandler; //pdfViewer.PDFEditMultiActiveHandler += PDFEditMultiActiveHandler; if (KeyDownHandler != null) { PdfViewControl.PDFViewTool.RemoveHandler(KeyDownEvent, KeyDownHandler); } KeyDownHandler = new KeyEventHandler(PDFView_KeyDown); PdfViewControl.PDFViewTool.AddHandler(KeyDownEvent, KeyDownHandler, false); } } private void PDFEditEmptyPanel() { PropertyContainer.Child = pdfContentEditControl; if (pdfTextCreateParam != null && PdfViewControl != null && PdfViewControl.PDFView != null) { if (PdfViewControl.PDFToolManager.GetCreateContentEditType() == CPDFEditType.EditText) { pdfContentEditControl.SetPDFTextEditData(pdfTextCreateParam); } else if (PdfViewControl.PDFToolManager.GetCreateContentEditType() == CPDFEditType.None) { pdfContentEditControl.ClearContentControl(); } } else { pdfContentEditControl.ClearContentControl(); } } //private void PDFEditMultiActiveHandler(object sender, List e) //{ // imageArea = null; // lastPDFEditMultiEvents = e; // if(e==null) // { // PDFEditEmptyPanel(); // return; // } // if(e.Count>1) // { // List editList= e.AsEnumerable().Select(x=>x.EditType).Distinct().ToList(); // if(editList.Count>1) // { // PDFEditEmptyPanel(); // return; // } // if (editList[0]==CPDFEditType.EditText) // { // pdfContentEditControl.SetPDFTextMultiEditData(e); // return; // } // if (editList[0]==CPDFEditType.EditImage) // { // UIElement pageView = sender as UIElement; // if (pageView != null) // { // pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; // } // pdfContentEditControl.SetPDFImageMultiEditData(e); // } // } //} private CPoint GetPoint(CPDFEditTextArea textArea) { CPoint caretPoint = new CPoint(); CPoint caretPointHigh = new CPoint(); textArea.GetTextCursorPoints(ref caretPoint, ref caretPointHigh); var lineHeight = caretPoint.y - caretPointHigh.y; CRect caretRect = new CRect(caretPoint.x, caretPoint.y, caretPointHigh.x, caretPointHigh.y); caretPoint = new CPoint(caretRect.left, caretRect.top); return new CPoint(caretRect.left, (caretRect.top + caretRect.bottom) / 2); } public void PDFView_KeyDown(object sender, KeyEventArgs e) { if (PdfViewControl.PDFViewTool.GetViewerModel() != MouseModes.ContentEdit) { return; } int pageIndex = -1; CPDFEditTextArea textArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref pageIndex) as CPDFEditTextArea; if (textArea == null) { return; } if (Keyboard.Modifiers == ModifierKeys.Control) { if (e.Key == Key.Left) { textArea.GetPreWordCharPlace(); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Right) { textArea.GetNextWordCharPlace(); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Up) { textArea.GetSectionBeginCharPlace(); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Down) { textArea.GetSectionEndCharPlace(); UpdateTextArea(textArea); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.Shift) { if (isUpdateStartPoint) { startPoint = GetPoint(textArea); isUpdateStartPoint = false; } if (e.Key == Key.Left) { textArea.GetPrevCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Right) { textArea.GetNextCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Up) { textArea.GetUpCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Down) { textArea.GetDownCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.Alt) { if (e.SystemKey == Key.Up) { textArea.GetLineBeginCharPlace(); UpdateTextArea(textArea); e.Handled = true; } if (e.SystemKey == Key.Down) { textArea.GetLineEndCharPlace(); UpdateTextArea(textArea); e.Handled = true; } } if (Keyboard.Modifiers == ModifierKeys.None) { var oldFrame = textArea.GetFrame(); if (e.Key == Key.Left) { oldFrame.left -= 5; e.Handled = true; } if (e.Key == Key.Right) { oldFrame.left += 5; e.Handled = true; } if (e.Key == Key.Up) { oldFrame.top -= 5; e.Handled = true; } if (e.Key == Key.Down) { oldFrame.top += 5; e.Handled = true; } textArea.SetFrame(oldFrame); } if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Shift)) { if (isUpdateStartPoint) { startPoint = GetPoint(textArea); isUpdateStartPoint = false; } if (e.Key == Key.Left) { textArea.GetPreWordCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Right) { textArea.GetNextWordCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Up) { textArea.GetSectionBeginCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } if (e.Key == Key.Down) { textArea.GetSectionEndCharPlace(); endPoint = GetPoint(textArea); textArea.GetSelectChars(startPoint, endPoint); UpdateTextArea(textArea); e.Handled = true; } } } private void UpdateTextArea(CPDFEditTextArea textArea) { Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame()); PdfViewControl.PDFViewTool.UpdateRender(oldRect, textArea); } private void UpdateImageArea(CPDFEditImageArea imageArea) { Rect oldRect = DataConversionForWPF.CRectConversionForRect(imageArea.GetFrame()); PdfViewControl.PDFViewTool.UpdateRender(oldRect, imageArea); } private void PdfViewControl_MouseRightButtonDownHandler(object sender, MouseEventObject e) { ContextMenu ContextMenu = PdfViewControl.GetRightMenu(); if (ContextMenu == null) { ContextMenu = new ContextMenu(); } switch (e.hitTestType) { case MouseHitTestType.TextEdit: CreateTextEditMenu(sender,ref ContextMenu); break; case MouseHitTestType.ImageEdit: CreateImageEditMenu(sender, ref ContextMenu); break; default: ContextMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); break; } PdfViewControl.SetRightMenu(ContextMenu); } private void CreateTextEditMenu(object sender, ref ContextMenu menu) { int index = -1; CPDFEditTextArea textArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref index) as CPDFEditTextArea; if (textArea != null) { if (PdfViewControl.PDFToolManager.GetDocument().GetPermissionsInfo().AllowsCopying) { menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); } menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); } else { menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); } } private void CreateImageEditMenu(object sender, ref ContextMenu menu) { int index = -1; CPDFEditImageArea imageArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref index) as CPDFEditImageArea; MenuItem rotateLeftMenu = new MenuItem(); rotateLeftMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateLeft"); rotateLeftMenu.Click += (o, p) => { if (imageArea != null) { imageArea.Rotate(-90); pdfContentEditControl.RefreshThumb(); UpdateImageArea(imageArea); } }; menu.Items.Add(rotateLeftMenu); MenuItem rotateRightMenu = new MenuItem(); rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight"); rotateRightMenu.Click += (o, p) => { if (imageArea != null) { imageArea.Rotate(90); pdfContentEditControl.RefreshThumb(); UpdateImageArea(imageArea); } }; menu.Items.Add(rotateRightMenu); MenuItem replaceMenu = new MenuItem(); replaceMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Replace"); replaceMenu.Click += (o, p) => { if (imageArea != null) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; if (openFileDialog.ShowDialog() == true) { int imageWidth = 0; int imageHeight = 0; byte[] imageData = null; BitmapFrame frame = null; BitmapDecoder decoder = BitmapDecoder.Create(new Uri(openFileDialog.FileName), BitmapCreateOptions.None, BitmapCacheOption.Default); if (decoder.Frames.Count > 0) { frame = decoder.Frames[0]; } if (frame != null) { imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4]; if (frame.Format != PixelFormats.Bgra32) { FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0); covert.CopyPixels(imageData, frame.PixelWidth * 4, 0); } else { frame.CopyPixels(imageData, frame.PixelWidth * 4, 0); } imageWidth = frame.PixelWidth; imageHeight = frame.PixelHeight; } imageArea.ReplaceImageArea(imageArea.GetFrame(), imageData, imageWidth, imageHeight); //imageArea.ReplaceImageArea(imageArea.GetFrame(), openFileDialog.FileName,string.Empty); pdfContentEditControl.RefreshThumb(); UpdateImageArea(imageArea); } } }; menu.Items.Add(replaceMenu); MenuItem exportMenu = new MenuItem(); exportMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Export"); exportMenu.Click += (o, p) => { if (PdfViewControl != null && PdfViewControl.PDFView != null) { try { FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() == DialogResult.OK && imageArea != null) { string path = Path.GetTempPath(); string uuid = Guid.NewGuid().ToString("N"); string imagePath = Path.Combine(path, uuid + ".tmp"); imageArea.ExtractImage(imagePath); Bitmap bitmapImage = new Bitmap(imagePath); string fileName = Path.Combine(folderBrowserDialog.SelectedPath, uuid + ".jpg"); bitmapImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); Process.Start("explorer", "/select,\"" + fileName + "\""); } } catch (Exception ex) { } } }; menu.Items.Add(exportMenu); MenuItem opacityMenu = new MenuItem(); opacityMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Opacity"); menu.Items.Add(opacityMenu); AppendOpacityMenu(opacityMenu, imageArea, CPDFEditType.EditImage); MenuItem horizonMirror = new MenuItem(); horizonMirror.Header = LanguageHelper.CommonManager.GetString("Menu_HFlip"); horizonMirror.Click += (o, p) => { imageArea?.HorizontalMirror(); pdfContentEditControl.RefreshThumb(); UpdateImageArea(imageArea); }; menu.Items.Add(horizonMirror); MenuItem verticalMirror = new MenuItem(); verticalMirror.Header = LanguageHelper.CommonManager.GetString("Menu_VFlip"); verticalMirror.Click += (o, p) => { if (imageArea != null) { imageArea?.VerticalMirror(); pdfContentEditControl.RefreshThumb(); UpdateImageArea(imageArea); } }; menu.Items.Add(verticalMirror); MenuItem cropMenu = new MenuItem(); cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop"); cropMenu.Click += (o, p) => { if (imageArea != null) { PdfViewControl.PDFViewTool.SetCropMode(!PdfViewControl.PDFViewTool.GetIsCropMode()); } }; menu.Items.Add(cropMenu); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); // if (editCommand.TextAreaCopied) // { // menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender }); // } } #region Property changed protected void OnPropertyChanged([CallerMemberName] string name = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } public void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e) { OnPropertyChanged(e.PropertyName); if (e.PropertyName == "CanSave") { OnCanSaveChanged?.Invoke(this, CanSave); } } #endregion #region Context menu //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); // } // if (e.EditType == (CPDFEditType.EditText | CPDFEditType.EditImage)) // { // e.Handle |= true; // PDFEditMultiContextMenu(sender, e); // } //} //private void PDFEditTextContextMenu(object sender, PDFEditCommand editCommand) //{ // editCommand.PopupMenu = new ContextMenu(); // if (lastPDFEditEvent != null || lastPDFEditMultiEvents != null) // { // if (PdfViewControl.PDFView.Document.GetPermissionsInfo().AllowsCopying) // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); // } // else // { // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); // if (editCommand.TextAreaCopied) // { // editCommand.PopupMenu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteMatchStyle, CommandTarget = (UIElement)sender }); // } // } //} //private void PDFEditImageContextMenu(object sender, PDFEditCommand editCommand) //{ // editCommand.PopupMenu = new ContextMenu(); // if (lastPDFEditEvent != null) // { // MenuItem rotateLeftMenu = new MenuItem(); // rotateLeftMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateLeft"); // rotateLeftMenu.Click += (o, p) => // { // if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) // { // lastPDFEditEvent.Rotate = -90; // lastPDFEditEvent.UpdatePDFEditByEventArgs(); // pdfContentEditControl.SetRotationText(lastPDFEditEvent.CurrentRotated); // pdfContentEditControl.RefreshThumb(); // } // }; // editCommand.PopupMenu.Items.Add(rotateLeftMenu); // MenuItem rotateRightMenu = new MenuItem(); // rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight"); // rotateRightMenu.Click += (o, p) => // { // if (imageArea != null) // { // imageArea.Rotate(90); // pdfContentEditControl.RefreshThumb(); // UpdateImageArea(imageArea); // } // }; // menu.Items.Add(rotateRightMenu); // MenuItem rotateRightMenu = new MenuItem(); // rotateRightMenu.Header = LanguageHelper.CommonManager.GetString("Menu_RotateRight"); // rotateRightMenu.Click += (o, p) => // { // if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage) // { // lastPDFEditEvent.Rotate = 90; // lastPDFEditEvent.UpdatePDFEditByEventArgs(); // pdfContentEditControl.SetRotationText(lastPDFEditEvent.CurrentRotated); // pdfContentEditControl.RefreshThumb(); // } // }; // editCommand.PopupMenu.Items.Add(rotateRightMenu); // MenuItem replaceMenu = new MenuItem(); // replaceMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Replace"); // replaceMenu.Click += (o, p) => // { // if (imageArea != null) // { // OpenFileDialog openFileDialog = new OpenFileDialog(); // openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;"; // if (openFileDialog.ShowDialog() == true) // { // //byte[] fileData = File.ReadAllBytes(openFileDialog.FileName); // //Bitmap bitmap = new Bitmap(openFileDialog.FileName); // //imageArea.ReplaceImageArea(imageArea.GetFrame(), fileData, bitmap.Width, bitmap.Height); // imageArea.ReplaceImageArea(imageArea.GetFrame(), openFileDialog.FileName, string.Empty); // pdfContentEditControl.RefreshThumb(); // UpdateImageArea(imageArea); // } // } // }; // menu.Items.Add(replaceMenu); // MenuItem exportMenu = new MenuItem(); // exportMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Export"); // exportMenu.Click += (o, p) => // { // if (PdfViewControl != null && PdfViewControl.PDFView != null) // { // try // { // FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); // if (folderBrowserDialog.ShowDialog() == DialogResult.OK && imageArea != null) // { // string path = Path.GetTempPath(); // string uuid = Guid.NewGuid().ToString("N"); // string imagePath = Path.Combine(path, uuid + ".tmp"); // imageArea.ExtractImage(imagePath); // Bitmap bitmapImage = new Bitmap(imagePath); // string fileName = Path.Combine(folderBrowserDialog.SelectedPath, uuid + ".jpg"); // bitmapImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); // Process.Start("explorer", "/select,\"" + fileName + "\""); // } // } // catch (Exception ex) // { // } // } // }; // menu.Items.Add(exportMenu); // MenuItem opacityMenu = new MenuItem(); // opacityMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Opacity"); // menu.Items.Add(opacityMenu); // AppendOpacityMenu(opacityMenu, imageArea, CPDFEditType.EditImage); // MenuItem horizonMirror = new MenuItem(); // horizonMirror.Header = LanguageHelper.CommonManager.GetString("Menu_HFlip"); // horizonMirror.Click += (o, p) => // { // imageArea?.HorizontalMirror(); // pdfContentEditControl.RefreshThumb(); // UpdateImageArea(imageArea); // }; // menu.Items.Add(horizonMirror); // MenuItem verticalMirror = new MenuItem(); // verticalMirror.Header = LanguageHelper.CommonManager.GetString("Menu_VFlip"); // verticalMirror.Click += (o, p) => // { // if (imageArea != null) // { // imageArea?.VerticalMirror(); // pdfContentEditControl.RefreshThumb(); // UpdateImageArea(imageArea); // } // }; // menu.Items.Add(verticalMirror); // MenuItem cropMenu = new MenuItem(); // cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop"); // cropMenu.Click += (o, p) => // { // if (imageArea != null) // { // } // }; // menu.Items.Add(cropMenu); // menu.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); // menu.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); // menu.Items.Add(new MenuItem() { Header = "Delete", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); // menu.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); // // if (editCommand.TextAreaCopied) // // { // // menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender }); // // } // } //} //MenuItem cropMenu = new MenuItem(); //cropMenu.Header = LanguageHelper.CommonManager.GetString("Menu_Crop"); //cropMenu.Click += (o, p) => //{ // if (imageArea != null) // { // } //}; //menu.Items.Add(cropMenu); //menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Copy"), Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender }); //menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Cut"), Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender }); //menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Delete"), Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender }); //menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_Paste"), Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender }); // if (editCommand.TextAreaCopied) // { // menu.Items.Add(new MenuItem() { Header = LanguageHelper.CommonManager.GetString("Menu_MatchPaste"), Command = CustomCommands.PasteWithoutStyle, CommandTarget = (UIElement)sender }); // } // } private void AppendOpacityMenu(MenuItem parentMenu, CPDFEditArea editArea, CPDFEditType editType) { 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 (editArea != null && editType == CPDFEditType.EditImage) { CPDFEditImageArea editImageArea = editArea as CPDFEditImageArea; editImageArea?.SetImageTransparency((byte)Math.Ceiling(opacity * 255 / 100D)); pdfContentEditControl.RefreshThumb(); UpdateImageArea(editImageArea); } else if (editArea != null && editType == CPDFEditType.EditText) { CPDFEditTextArea editTextArea = editArea as CPDFEditTextArea; editTextArea?.SetCharsFontTransparency((byte)Math.Ceiling(opacity * 255 / 100D)); pdfContentEditControl.RefreshThumb(); UpdateTextArea(editTextArea); } }; parentMenu.Items.Add(opacityMenu); } } private void PDFToolManager_MouseLeftButtonUpHandler(object sender, MouseEventObject e) { if (e.IsCreate) { PdfViewControl.SetIsShowStampMouse(false); } else { int PageIndex = -1; CPDFEditArea editAreaArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref PageIndex); if (editAreaArea == null) { return; } else { if (editAreaArea.Type == CPDFEditType.EditText) { PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex); pdfContentEditControl.SetPDFTextEditData((TextEditParam)pDFEditParam, true); PropertyContainer.Child = pdfContentEditControl; } //panelState.RightPanel = PanelState.RightPanelState.PropertyPanel; } } } private void PDFToolManager_MouseLeftButtonDownHandler(object sender, MouseEventObject e) { isUpdateStartPoint = true; if (PdfViewControl.PDFToolManager.GetToolType() != ToolType.ContentEdit) { 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; } int PageIndex = -1; CPDFEditArea editAreaArea = PdfViewControl.PDFToolManager.GetSelectedEditAreaObject(ref PageIndex); if (editAreaArea == null) { pdfContentEditControl.ClearContentControl(); return; } else { if (editAreaArea.Type == CPDFEditType.EditText) { PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex); pdfContentEditControl.SetPDFTextEditData((TextEditParam)pDFEditParam, true); PropertyContainer.Child = pdfContentEditControl; } else if (editAreaArea.Type == CPDFEditType.EditImage && PdfViewControl != null) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; } PDFEditParam pDFEditParam = ParamConverter.CPDFDataConverterToPDFEitParam(PdfViewControl.PDFToolManager.GetDocument(), editAreaArea, PageIndex); pdfContentEditControl.SetPDFImageEditData((ImageEditParam)pDFEditParam); PropertyContainer.Child = pdfContentEditControl; } //panelState.RightPanel = PanelState.RightPanelState.PropertyPanel; } } private void PdfViewControl_SplitPDFViewToolCreated(object sender, EventArgs e) { PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged -= UndoManager_PropertyChanged; PdfViewControl.PDFViewTool.GetCPDFViewer().UndoManager.PropertyChanged += UndoManager_PropertyChanged; } /// /// Text and Image Selected Event /// private void PDFView_PDFEditActiveHandler(object sender, PDFEditParam e) { imageAreaParam = e; 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((TextEditParam)e, true); return; } if (e.EditType == CPDFEditType.EditImage && PdfViewControl != null) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; pageView.MouseLeftButtonUp += PageView_MouseLeftButtonUp; } pdfContentEditControl.SetPDFImageEditData((ImageEditParam)e); return; } } private void PageView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { UIElement pageView = sender as UIElement; if (pageView != null) { pageView.MouseLeftButtonUp -= PageView_MouseLeftButtonUp; } if (imageAreaParam != null) { pdfContentEditControl.SetPDFImageEditData((ImageEditParam)imageAreaParam); } } public void ClearViewerControl() { PDFGrid.Child = null; BotaContainer.Child = null; PropertyContainer.Child = null; displaySettingsControl = null; } private void CommandBinding_Executed_Undo(object sender, ExecutedRoutedEventArgs e) { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanUndo) { PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Undo(); } } private void CommandBinding_Executed_Redo(object sender, ExecutedRoutedEventArgs e) { if (PdfViewControl != null && PdfViewControl.PDFViewTool != null && CanRedo) { PdfViewControl.PDFViewTool.GetCPDFViewer()?.UndoManager?.Redo(); } } } } #endregion