using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using ComPDFKit.PDFPage.Edit; using Compdfkit_Tools.Edit; using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Compdfkit_Tools.Helper; namespace Compdfkit_Tools.PDFControl { public partial class ContentEditControl : UserControl, INotifyPropertyChanged { #region Property public PDFViewControl PdfViewControl = new PDFViewControl(); public PDFContentEditControl pdfContentEditControl = new PDFContentEditControl(); private CPDFDisplaySettingsControl displaySettingsControl = null; private bool _isActive = false; public bool IsActive { get => _isActive; set { _isActive = value; OnPropertyChanged(); } } private double[] zoomLevelList = { 1f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 }; private PDFEditEvent pdfTextCreateParam; private PDFEditEvent lastPDFEditEvent = null; private PanelState panelState = PanelState.GetInstance(); private KeyEventHandler KeyDownHandler; public event PropertyChangedEventHandler PropertyChanged; public ICommand CloseTabCommand; public ICommand ExpandPropertyPanelCommand; 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; } } private bool CanSave { get { if (PdfViewControl != null && PdfViewControl.PDFView != null) { return PdfViewControl.PDFView.UndoManager.CanSave; } return false; } } public event EventHandler OnCanSaveChanged; public event EventHandler OnAnnotEditHandler; #endregion public ContentEditControl() { InitializeComponent(); 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((IsActive)?displaySettingsControl:null, Visibility.Visible); } else { ExpandRightPropertyPanel(null, Visibility.Collapsed); } } } 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 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; 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); 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.PDFView != null) { PdfViewControl.PDFView.UndoManager?.Undo(); } } private void RedoBtn_Click(object sender, RoutedEventArgs e) { if (PdfViewControl != null && PdfViewControl.PDFView != null) { PdfViewControl.PDFView.UndoManager?.Redo(); } } 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; } public void SetDisplaySettingsControl(CPDFDisplaySettingsControl displaySettingsControl) { this.displaySettingsControl = displaySettingsControl; } private void UserControl_Loaded(object sender, RoutedEventArgs e) { PdfViewControl.PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler; } private void UserControl_UnLoaded(object sender, RoutedEventArgs e) { PdfViewControl.PDFView.PDFEditCommandHandler -= PDFView_PDFEditCommandHandler; } public void ExpandRightPropertyPanel(UIElement propertytPanel, Visibility visible) { PropertyContainer.Width = 260; PropertyContainer.Child = propertytPanel; PropertyContainer.Visibility = visible; } public void InitWithPDFViewer(CPDFViewer pdfViewer) { PdfViewControl.PDFView = pdfViewer; PDFGrid.Child = PdfViewControl; FloatPageTool.InitWithPDFViewer(pdfViewer); pdfContentEditControl.InitWithPDFViewer(pdfViewer); PdfViewControl.PDFView.PDFEditActiveHandler -= PDFView_PDFEditActiveHandler; PdfViewControl.PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler; PdfViewControl.PDFView.UndoManager.PropertyChanged -= UndoManager_PropertyChanged; PdfViewControl.PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged; DataContext = this; } /// /// Text and Image Selected Event /// private void PDFView_PDFEditActiveHandler(object sender, ComPDFKitViewer.PDFEditEvent e) { lastPDFEditEvent = 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(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 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); } } public void ClearViewerControl() { PDFGrid.Child = null; BotaContainer.Child = null; PropertyContainer.Child = null; } #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); } } 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 }); } } } 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 }); } } 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); } } #endregion 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(); } } } }