Browse Source

compdfkit demo windows - 文字编辑 和图片编辑 增加右键菜单

liyuxuan 1 year ago
parent
commit
c662c9472d

+ 5 - 5
compdfkit_demo_windows/compdfkit/compdfkit-tools/Edit/PDFImageEdit/PDFImageEditControl/PDFImageEditControl.xaml.cs

@@ -49,10 +49,10 @@ namespace compdfkit_tools.Edit
                 {
                     try
                     {
-                        Dictionary<int, List<System.Drawing.Bitmap>> imageDict = PDFView.GetSelectedImages();
+                        Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
                         foreach (int pageIndex in imageDict.Keys)
                         {
-                            List<System.Drawing.Bitmap> imageList = imageDict[pageIndex];
+                            List<Bitmap> imageList = imageDict[pageIndex];
                             if(imageList.Count > 0)
                             {
                                 Bitmap bitmapImage= imageList[0];
@@ -172,7 +172,7 @@ namespace compdfkit_tools.Edit
         {
             if (PDFView != null)
             {
-                Dictionary<int, List<System.Drawing.Bitmap>> imageDict = PDFView.GetSelectedImages();
+                Dictionary<int, List<Bitmap>> imageDict = PDFView.GetSelectedImages();
                 if (imageDict != null && imageDict.Count > 0)
                 {
                     System.Windows.Forms.FolderBrowserDialog folderBrowser = new System.Windows.Forms.FolderBrowserDialog();
@@ -184,8 +184,8 @@ namespace compdfkit_tools.Edit
                         {
                             foreach (int pageIndex in imageDict.Keys)
                             {
-                                List<System.Drawing.Bitmap> imageList = imageDict[pageIndex];
-                                foreach (System.Drawing.Bitmap image in imageList)
+                                List<Bitmap> 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);

+ 0 - 3
compdfkit_demo_windows/compdfkit/compdfkit-tools/Edit/PDFImageEdit/PDFImageUI/CPDFImageRotateUI.xaml.cs

@@ -15,9 +15,6 @@ using System.Windows.Shapes;
 
 namespace compdfkit_tools.Edit
 {
-    /// <summary>
-    /// CPDFImageRotateUI.xaml 的交互逻辑
-    /// </summary>
     public partial class CPDFImageRotateUI : UserControl
     {
         public event EventHandler<double> RotationChanged;

+ 229 - 4
compdfkit_demo_windows/compdfkit/edit-ctrl-demo/MainWindow.xaml.cs

@@ -3,10 +3,12 @@ using compdfkit_tools.Edit;
 using compdfkit_tools.PDFControl;
 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;
@@ -54,6 +56,7 @@ namespace edit_ctrl_demo
             }
         }
 
+        private PDFEditEvent lastPDFEditEvent = null;
         public MainWindow()
         {
             InitializeComponent();
@@ -73,6 +76,27 @@ namespace edit_ctrl_demo
             TitleBarTool.OpenFileEvent += TitleBarTool_OpenFileEvent;
             PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
             PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
+            PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
+        }
+
+        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 UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -85,23 +109,30 @@ namespace edit_ctrl_demo
         /// </summary>
         private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
         {
+            lastPDFEditEvent = null;
             if (e == null)
             {
                 PDFEditContainer.Child= null;
                 return;
             }
 
-            if(e.EditType==CPDFEditType.EditText)
+            if(Mouse.RightButton==MouseButtonState.Pressed)
             {
-                PDFTextEditControl textEditControl= new PDFTextEditControl();
+                lastPDFEditEvent = e;
+                return;
+            }
+
+            if (e.EditType == CPDFEditType.EditText)
+            {
+                PDFTextEditControl textEditControl = new PDFTextEditControl();
                 textEditControl.SetPDFTextEditData(e);
                 PDFEditContainer.Child = textEditControl;
                 return;
             }
 
-            if(e.EditType== CPDFEditType.EditImage)
+            if (e.EditType == CPDFEditType.EditImage)
             {
-                PDFImageEditControl imageEditControl= new PDFImageEditControl();
+                PDFImageEditControl imageEditControl = new PDFImageEditControl();
                 imageEditControl.InitWithPDFViewer(PDFView);
                 imageEditControl.SetPDFImageEditData(e);
                 PDFEditContainer.Child = imageEditControl;
@@ -137,6 +168,7 @@ namespace edit_ctrl_demo
                 if (senderBtn.IsChecked == true)
                 {
                     PDFView?.SetPDFEditType(CPDFEditType.EditText);
+                    PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
                     PDFView?.SetMouseMode(MouseModes.PDFEdit);
                     PDFView?.ReloadDocument();
                 }
@@ -189,5 +221,198 @@ namespace edit_ctrl_demo
         {
             PDFView?.UndoManager?.Redo();
         }
+
+        private void PDFEditTextContextMenu(object sender,PDFEditCommand editCommand)
+        {
+            editCommand.PopupMenu = new ContextMenu();
+            if (lastPDFEditEvent != null)
+            {
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
+                MenuItem propertyMenu = new MenuItem();
+                propertyMenu.Header = "属性";
+                propertyMenu.Click += (o, p) =>
+                {
+                    if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditText)
+                    {
+                        PDFTextEditControl textEditControl = new PDFTextEditControl();
+                        textEditControl.SetPDFTextEditData(lastPDFEditEvent);
+                        PDFEditContainer.Child = textEditControl;
+                    }
+                };
+                editCommand.PopupMenu.Items.Add(propertyMenu);
+            }
+            else
+            {
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
+                if(editCommand.TextAreaCopied)
+                {
+                    editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴样式", 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 = "左旋转";
+                rotateLeftMenu.Click += (o, p) =>
+                {
+                    if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
+                    {
+                        lastPDFEditEvent.Rotate = -90;
+                        lastPDFEditEvent.UpdatePDFEditByEventArgs();
+                    }
+                };
+                editCommand.PopupMenu.Items.Add(rotateLeftMenu);
+
+                MenuItem rotateRightMenu = new MenuItem();
+                rotateRightMenu.Header = "右旋转";
+                rotateRightMenu.Click += (o, p) =>
+                {
+                    if (lastPDFEditEvent != null && lastPDFEditEvent.EditType == CPDFEditType.EditImage)
+                    {
+                        lastPDFEditEvent.Rotate = 90;
+                        lastPDFEditEvent.UpdatePDFEditByEventArgs();
+                    }
+                };
+                editCommand.PopupMenu.Items.Add(rotateRightMenu);
+
+                MenuItem replaceMenu = new MenuItem();
+                replaceMenu.Header = "替换";
+                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();
+                            PDFView?.ClearSelectPDFEdit();
+                        }
+                    }
+                };
+                editCommand.PopupMenu.Items.Add(replaceMenu);
+
+                MenuItem exportMenu = new MenuItem();
+                exportMenu.Header = "导出";
+                exportMenu.Click += (o, p) =>
+                {
+                    if (PDFView != null)
+                    {
+                        Dictionary<int, List<Bitmap>> imageDict = 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<Bitmap> 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 = "透明度";
+                editCommand.PopupMenu.Items.Add(opacityMenu);
+
+                AppendOpacityMenu(opacityMenu);
+
+                MenuItem horizonMirror = new MenuItem();
+                horizonMirror.Header = "水平镜像";
+                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 = "垂直镜像";
+                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 = "裁剪";
+                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 = "复制", Command = ApplicationCommands.Copy, CommandTarget = (UIElement)sender });
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "剪切", Command = ApplicationCommands.Cut, CommandTarget = (UIElement)sender });
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "删除", Command = ApplicationCommands.Delete, CommandTarget = (UIElement)sender });
+            }
+            else
+            {
+                editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
+            }
+        }
+
+        private void AppendOpacityMenu(MenuItem parentMenu)
+        {
+            List<int> opacityList = new List<int>()
+            {
+                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 / 100);
+                        lastPDFEditEvent.UpdatePDFEditByEventArgs();
+                    }
+                };
+                parentMenu.Items.Add(opacityMenu);
+            }
+        }
+        
     }
 }

+ 2 - 0
compdfkit_demo_windows/compdfkit/edit-ctrl-demo/edit-ctrl-demo.csproj

@@ -46,6 +46,8 @@
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Data" />
+    <Reference Include="System.Drawing" />
+    <Reference Include="System.Windows.Forms" />
     <Reference Include="System.Xml" />
     <Reference Include="Microsoft.CSharp" />
     <Reference Include="System.Core" />