123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- using Prism.Commands;
- using Prism.Mvvm;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using System.Windows;
- using PDF_Master.Model.PropertyPanel.AnnotPanel;
- using System.Windows.Controls;
- using Prism.Regions;
- using ComPDFKitViewer.PdfViewer;
- using PDF_Master.Model;
- using Microsoft.Win32;
- using PDF_Master.CustomControl;
- using ComPDFKitViewer;
- using System.Windows.Input;
- using PDF_Master.Helper;
- using ComPDFKitViewer.AnnotEvent;
- namespace PDF_Master.ViewModels.Tools
- {
- public class TextEditToolContentViewModel: BindableBase, INavigationAware
- {
- #region 变量
- public ViewContentViewModel viewContentViewModel;
- private CPDFViewer PDFViewer;
- private IRegionManager regions;
- private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
- #endregion
- #region 属性
- private bool _isTextEdit = false;
- public bool IsTextEdit
- {
- get { return _isTextEdit; }
- set { SetProperty(ref _isTextEdit, value); }
- }
- private bool _isImgEdit = false;
- public bool IsImgEdit
- {
- get { return _isImgEdit; }
- set { SetProperty(ref _isImgEdit, value); }
- }
- #endregion
- #region Command
- // 添加文本、图像
- public DelegateCommand<object> AddContentCommand { get; set; }
- public DelegateCommand AddTextCommand { get; set; }
- public DelegateCommand AddImgCommand { get; set; }
- public DelegateCommand EditTextModeCommand { get; set; }
- #endregion
- #region 初始化
- public TextEditToolContentViewModel(IRegionManager regionManager)
- {
-
-
- regions = regionManager;
- InitCommand();
- InitBtnToProperty();
- }
- private void InitCommand()
- {
- AddContentCommand = new DelegateCommand<object>(AddContent);
- AddTextCommand = new DelegateCommand(AddText);
- AddImgCommand = new DelegateCommand(AddImg);
- EditTextModeCommand = new DelegateCommand(EditTextMode);
- }
- private void InitBtnToProperty()
- {
- btnToProperty.Add("Text", "TextEditProperty");
- btnToProperty.Add("Image", "ImageEditProperty");
- btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
- btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
- }
- #endregion
- #region 右键菜单
- //点击空白处时
- private ContextMenu EmptyStateMenu(object sender)
- {
- var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //粘贴
- customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
- //添加文本
- customMenu.SetMenuBinding(1, AddTextCommand);
- //添加图像
- customMenu.SetMenuBinding(2, AddImgCommand);
- return popMenu;
- }
- //选中文字的框
- private ContextMenu SelectTextBorder(object sender)
- {
- var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
-
- //复制
- customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- customMenu.SetVisibilityProperty(6, false);
- return popMenu;
- }
- //选中文字内容
- private ContextMenu SelectTextContent(object sender)
- {
- var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //复制
- customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //粘贴并匹配样式
- customMenu.SetVisibilityProperty(3, false);
- //删除
- customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- //全部选定
- customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
- return popMenu;
- }
- //编辑中右键
- private ContextMenu EditTextContent(object sender)
- {
- var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- ////复制
- //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- ////剪切
- //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //全部选定
- customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
- //粘贴
- customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
- //粘贴并匹配样式
- customMenu.SetVisibilityProperty(2, false);
- ////删除
- //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- return popMenu;
- }
- #endregion
- #region 快捷键
- private void ShortCut_KeyDown(object sender, KeyEventArgs e)
- {
- try
- {
- if (e.Key == Key.Escape)
- {
- if (PDFViewer != null)
- {
- //缩小esc的操作范围
- if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditText)
- {
- PDFViewer.RemovePDFEditEmptyText();
- //PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- //PDFViewer.ReloadDocument();
- //ShowPropertyPanel(true);
- //只有在有画框的时候才进行
- if (PDFViewer.MouseMode == MouseModes.PDFEdit&& PDFViewer.ToolManager.HasTool == true)
- {
- PDFViewer.RemoveTool(false);
- }
- }
- else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
- {
- PDFViewer.RemovePDFEditEmptyText();
- //PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- //PDFViewer.ReloadDocument();
- //ShowPropertyPanel(true);
- if (PDFViewer.MouseMode == MouseModes.PDFEdit&&PDFViewer.ToolManager.HasTool == true)
- {
- PDFViewer.RemoveTool(false);
- }
- }
- }
- }
- }
- catch
- {
- }
-
-
- }
- #endregion
- public void AddContent(object obj)
- {
- if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
- var btn = obj as CustomIconToggleBtn;
- EnterEditMode((bool)btn.IsChecked, btn.Tag.ToString());
- }
- private void EditTextMode()
- {
- }
- private void EnterEditMode(bool isCheckMode,string modeType)
- {
- if (isCheckMode)
- {
- if (modeType == "Text")
- {
- IsImgEdit = false;
- IsTextEdit = true;
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
-
- }
- else
- {
- IsImgEdit = true;
- IsTextEdit = false;
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- }
- }
- else
- {
- IsImgEdit = false;
- IsTextEdit = false;
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- }
- }
- #region Command功能
- private void AddText()
- {
- EnterEditMode(true, "Text");
- }
- private void AddImg()
- {
- EnterEditMode(true, "Image");
- }
- #endregion
- private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
- {
- if (btnToProperty.ContainsKey(type))
- {
- NavigationParameters parameters = new NavigationParameters();
- parameters.Add(ParameterNames.PDFViewer, PDFViewer);
- parameters.Add(ParameterNames.AnnotEvent, e);
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
- }));
- ShowPropertyPanel(true);
- }
- }
- private void ShowPropertyPanel(bool show = true)
- {
- viewContentViewModel.IsPropertyOpen = show;
- }
-
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- if (PDFViewer != null)
- {
- //左键激活
- PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
- PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
- //右键
- PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
- PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
- //图片添加
- PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
- PDFViewer.CustomNotifyHandler += PDFViewer_CustomNotifyHandler;
- //选中
- PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
- PDFViewer.PDFEditHandler += PDFViewer_PDFEditHandler;
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- KeyEventsHelper.KeyDown += ShortCut_KeyDown;
- }
- }
- private void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
- {
- PDFViewer.SelectPDFEdit(e,true);
- }
- private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
- {
-
- if (e != null && e.Count > 0)
- {
- bool isText = false;
- bool isImg = false;
- foreach (var item in e)
- {
- if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
- {
- isImg = true;
- }
- if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
- {
- isText = true;
- }
- }
- if (isText == true && isImg == false)
- {
- AddToPropertyPanel("Text", e);
- }
- if (isText == false && isImg == true)
- {
- AddToPropertyPanel("Image", e);
- }
- if (isText == true && isImg == true)
- {
- AddToPropertyPanel("TextAndImage", e);
- }
- }
- else
- {
- AddToPropertyPanel("PropertyPanelContent", null);
- }
- }
- private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
- {
- if (e == null)
- return;
- switch (e.CommandType)
- {
- case CommandType.Context:
- if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)//点击空白区域
- {
- e.PopupMenu = EmptyStateMenu(sender);
- }
- else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
- {
- if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
- {
- //文字编辑
- if (e.PressOnBorder == true)
- {
- e.PopupMenu = SelectTextBorder(sender);
- }
- else if(e.PressOnSelectedText == true)
- {
- e.PopupMenu = SelectTextContent(sender);
- }
- else if(e.SelectText=="")
- {
- e.PopupMenu= EditTextContent(sender);
- }
- else
- {
- e.PopupMenu = SelectTextContent(sender);
- }
- }
- }
- break;
- default:
- e.DoCommand();
- break;
- }
- if (e.PopupMenu != null)
- {
- e.Handle = true;
- }
- }
- private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
- {
- if (e.NotifyType == CustomNotifyType.PDFEditImage)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- PDFViewer.AddPDFEditImage(openFileDialog.FileName,500,500);
- }
- }
-
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
-
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- IsImgEdit = false;
- IsTextEdit = false;
- PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
- PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
- PDFViewer.CustomNotifyHandler -= PDFViewer_CustomNotifyHandler;
- PDFViewer.PDFEditHandler -= PDFViewer_PDFEditHandler;
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- ShowPropertyPanel(false);
- }
- }
- }
|