123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- using ComPDFKit.PDFPage;
- 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;
- 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 System.Xml.Linq;
- namespace edit_ctrl_demo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window,INotifyPropertyChanged
- {
- /// <summary>
- /// 是否能够撤销
- /// </summary>
- public bool CanUndo
- {
- get
- {
- if (PDFView != null)
- {
- return PDFView.UndoManager.CanUndo;
- }
- return false;
- }
- }
- /// <summary>
- /// 是否能够重做
- /// </summary>
- public bool CanRedo
- {
- get
- {
- if (PDFView != null)
- {
- return PDFView.UndoManager.CanRedo;
- }
- return false;
- }
- }
- /// <summary>
- /// 上一次的PDF编辑对象
- /// </summary>
- private PDFEditEvent lastPDFEditEvent = null;
- /// <summary>
- /// 缩放比例
- /// </summary>
- private double[] zoomLevel = { 1.00f, 8f, 12f, 25, 33f, 50, 66f, 75, 100, 125, 150, 200, 300, 400, 600, 800, 1000 };
- public MainWindow()
- {
- InitializeComponent();
- Loaded += MainWindow_Loaded;
- DataContext = this;
- }
- /// <summary>
- /// 属性更改事件
- /// </summary>
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- ///触发属性更改事件通知
- /// </summary>
- protected void OnPropertyChanged([CallerMemberName] string name = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
- }
- private void MainWindow_Loaded(object sender, RoutedEventArgs e)
- {
- LoadDefaultDocument();
- TitleBarTool.OpenFileEvent += TitleBarTool_OpenFileEvent;
- PDFView.PDFEditActiveHandler += PDFView_PDFEditActiveHandler;
- PDFView.UndoManager.PropertyChanged += UndoManager_PropertyChanged;
- PDFView.PDFEditCommandHandler += PDFView_PDFEditCommandHandler;
- PDFView.MouseWheelZoomHandler += PDFView_MouseWheelZoomHandler;
- }
- /// <summary>
- /// 鼠标滚轮缩放事件
- /// </summary>
- private void PDFView_MouseWheelZoomHandler(object sender, bool e)
- {
- double newZoom = CheckZoomLevel(PDFView.ZoomFactor + (e ? 0.01 : -0.01), e);
- PDFView?.Zoom(newZoom);
- }
- /// <summary>
- /// 检查缩放比例
- /// </summary>
- 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 < zoomLevel.Length - 1; i++)
- {
- if (zoom > zoomLevel[i] && zoom <= zoomLevel[i + 1] && IsGrowth)
- {
- standardZoom = zoomLevel[i + 1];
- break;
- }
- if (zoom >= zoomLevel[i] && zoom < zoomLevel[i + 1] && !IsGrowth)
- {
- standardZoom = zoomLevel[i];
- break;
- }
- }
- return standardZoom / 100;
- }
- /// <summary>
- /// 文字编辑和图片编辑右键点击处理
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 撤销重做状态更新事件通知
- /// </summary>
-
- private void UndoManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- OnPropertyChanged(e.PropertyName);
- }
- /// <summary>
- /// 文字或图片块选中事件
- /// </summary>
- private void PDFView_PDFEditActiveHandler(object sender, PDFEditEvent e)
- {
- PDFEditEvent tempEvent = lastPDFEditEvent;
- lastPDFEditEvent = e;
- if (e == null)
- {
- PDFEditContainer.Child= null;
- return;
- }
-
- if (Mouse.RightButton==MouseButtonState.Pressed)
- {
- return;
- }
- if (e.EditType == CPDFEditType.EditText)
- {
- PDFTextEditControl textEditControl = new PDFTextEditControl();
- textEditControl.SetPDFTextEditData(e);
- PDFEditContainer.Child = textEditControl;
- return;
- }
- if (e.EditType == CPDFEditType.EditImage)
- {
- PDFImageEditControl imageEditControl = new PDFImageEditControl();
- imageEditControl.InitWithPDFViewer(PDFView);
- imageEditControl.SetPDFImageEditData(e);
- PDFEditContainer.Child = imageEditControl;
- return;
- }
- }
- /// <summary>
- /// 工具栏选择文件对话框事件通知
- /// </summary>
- private void TitleBarTool_OpenFileEvent(object sender, string filePath)
- {
- ClearPDFEditState();
- PDFView?.CloseDocument();
- PDFView?.InitDocument(filePath);
- PDFView?.Load();
- PDFView?.SetMouseMode(MouseModes.PanTool);
- }
- /// <summary>
- /// 加载显示默认文档
- /// </summary>
- private void LoadDefaultDocument()
- {
- string defaultFilePath = "..\\..\\..\\..\\developer_guide_windows.pdf";
- PDFView?.InitDocument(defaultFilePath);
- PDFView?.Load();
- }
- /// <summary>
- /// 文字编辑模式
- /// </summary>
- private void PDFTextEditButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton senderBtn=sender as ToggleButton;
- if(senderBtn != null)
- {
- ClearPDFEditState(senderBtn);
- if (senderBtn.IsChecked == true)
- {
- PDFView?.SetPDFEditType(CPDFEditType.EditText);
- PDFView?.SetPDFEditCreateType(CPDFEditType.EditText);
- PDFView?.SetMouseMode(MouseModes.PDFEdit);
- PDFView?.ReloadDocument();
- }
- else
- {
- PDFView?.SetPDFEditType(CPDFEditType.None);
- PDFView?.SetMouseMode(MouseModes.PanTool);
- PDFView?.ReloadDocument();
- }
- }
- }
- /// <summary>
- /// 图片编辑模式
- /// </summary>
- private void PDFImageEditButton_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton senderBtn = sender as ToggleButton;
- if (senderBtn != null)
- {
- ClearPDFEditState(senderBtn);
- senderBtn.IsChecked = false;
- PDFView?.SetPDFEditType(CPDFEditType.EditImage);
- PDFView?.SetMouseMode(MouseModes.PDFEdit);
- PDFView?.ReloadDocument();
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- PDFView?.SetPDFEditCreateType(CPDFEditType.EditImage);
- PDFView?.AddPDFEditImage(openFileDialog.FileName);
- }
- }
- }
- /// <summary>
- /// 清除编辑相关按钮选中状态
- /// </summary>
- private void ClearPDFEditState(ToggleButton ignoreBtn=null)
- {
- List<ToggleButton> clearBtnList = new List<ToggleButton>()
- {
- PDFTextEditButton,
- PDFImageEditButton,
- PDFEditBtn,
- };
- foreach (ToggleButton item in clearBtnList)
- {
- if(ignoreBtn==item)
- {
- continue;
- }
- item.IsChecked = false;
- }
-
- PDFEditContainer.Child = null;
- }
- /// <summary>
- /// 撤销操作
- /// </summary>
- private void UndoBtn_Click(object sender, RoutedEventArgs e)
- {
- PDFView?.UndoManager?.Undo();
- }
- /// <summary>
- /// 重做操作
- /// </summary>
- private void RedoBtn_Click(object sender, RoutedEventArgs e)
- {
- PDFView?.UndoManager?.Redo();
- }
- /// <summary>
- /// 文字编辑右键菜单
- /// </summary>
-
- 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 });
- }
- }
- }
- /// <summary>
- /// 图片编辑右键菜单
- /// </summary>
- 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
- {
- if(Clipboard.ContainsImage())
- {
- editCommand.PopupMenu.Items.Add(new MenuItem() { Header = "粘贴", Command = ApplicationCommands.Paste, CommandTarget = (UIElement)sender });
- }
- }
- }
- /// <summary>
- /// 图片编辑透明度二级菜单
- /// </summary>
- 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) Math.Ceiling(opacity * 255 / 100D);
- lastPDFEditEvent.UpdatePDFEditByEventArgs();
- }
- };
- parentMenu.Items.Add(opacityMenu);
- }
- }
- /// <summary>
- /// 进入文字和图片编辑
- /// </summary>
- private void PDFEditBtn_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton senderBtn = sender as ToggleButton;
- if (senderBtn != null)
- {
- ClearPDFEditState(senderBtn);
- if (senderBtn.IsChecked == true)
- {
- PDFView?.SetPDFEditType(CPDFEditType.EditText|CPDFEditType.EditImage);
- PDFView?.SetPDFEditCreateType(CPDFEditType.None);
- PDFView?.SetMouseMode(MouseModes.PDFEdit);
- PDFView?.ReloadDocument();
- }
- else
- {
- PDFView?.SetPDFEditType(CPDFEditType.None);
- PDFView?.SetMouseMode(MouseModes.PanTool);
- PDFView?.ReloadDocument();
- }
- }
- }
- }
- }
|