1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039 |
- 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;
- using System.ComponentModel;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- using PDF_Master.EventAggregators;
- using Prism.Events;
- using System.Windows.Media.Imaging;
- using PDFReader_WPF.Helper;
- using PDF_Master.Properties;
- using PDF_Master.Model.AnnotPanel;
- using System.Diagnostics;
- using ComPDFKit.PDFPage.Edit;
- using ComPDFKit.PDFPage;
- using PDFSettings;
- namespace PDF_Master.ViewModels.Tools
- {
- public class TextEditToolContentViewModel : BindableBase, INavigationAware
- {
- #region 属性与变量
- /// <summary>
- /// 本地配置中的编辑属性
- /// </summary>
- private PDFEditEvent pdfDefaultEditEvent;
- public ViewContentViewModel viewContentViewModel;
- private CPDFViewer PDFViewer;
- private IEventAggregator events;
- private IRegionManager regions;
- /// <summary>
- /// 用于区分事件的唯一码
- /// </summary>
- private string unicode;
- private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
- 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);
- }
- }
- private bool _flg = false;
- public bool flg
- {
- get { return _flg; }
- set { SetProperty(ref _flg, value); }
- }
- /// <summary>
- /// 替换图片指令
- /// </summary>
- private bool _ReplaceImgflg = false;
- public bool ReplaceImgflg
- {
- get { return _ReplaceImgflg; }
- set { SetProperty(ref _ReplaceImgflg, value); }
- }
- /// <summary>
- /// 完成裁剪图片指令
- /// </summary>
- private bool _CropImgflg = false;
- public bool CropImgflg
- {
- get { return _CropImgflg; }
- set { SetProperty(ref _CropImgflg, value); }
- }
- /// <summary>
- /// 刷新预览图片指令
- /// </summary>
- private bool _REImgflg = false;
- public bool REImgflg
- {
- get { return _REImgflg; }
- set { SetProperty(ref _REImgflg, 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 Command
- #region 初始化
- public TextEditToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
- {
- events = eventAggregator;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- 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("PropertyPanelContentEdit", "PropertyPanelContentEdit");
- }
- #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(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(3, 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 ContextMenu SelectImgPDFEdit(object sender)
- {
- var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //复制
- customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
- //裁剪
- customMenu.SetMenuBinding(4, new DelegateCommand(CropMode));
- //替换
- customMenu.SetMenuBinding(5, new DelegateCommand(ReplaceImg));
- //导出
- customMenu.SetMenuBinding(6, new DelegateCommand(ExportImg));
- return popMenu;
- }
- //选中裁剪图像时
- private ContextMenu CropImgPDFEdit(object sender)
- {
- var popMenu = App.Current.FindResource("CropImgMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //确认裁剪
- customMenu.SetMenuBinding(0, new DelegateCommand(CropImg));
- //取消裁剪
- customMenu.SetMenuBinding(1, new DelegateCommand(CancelCropImg));
- //还原裁剪
- customMenu.SetMenuBinding(2, new DelegateCommand(RestoreCropImg));
- return popMenu;
- }
- //多选图片右键
- private ContextMenu SelectMoreImage(object sender)
- {
- var popMenu = App.Current.FindResource("SelectMoreImageMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //复制
- customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
- //导出
- customMenu.SetMenuBinding(4, new DelegateCommand(ExportImg));
- 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)
- {
- if (PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditText) == 0)
- {
- PDFViewer.RemovePDFEditEmptyText();
- //只有在有画框的时候才进行
- if (PDFViewer.MouseMode == MouseModes.PDFEdit && PDFViewer.ToolManager.HasTool == true)
- {
- PDFViewer.RemoveTool(false);
- }
- else if (IsTextEdit == true || IsImgEdit == true)
- {
- 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();
- ShowPropertyPanel(false);
- }
- }
- {
- PDFViewer.ClearSelectPDFEdit();
- PDFViewer.RemovePDFEditEmptyText();
- PDFViewer.ReloadDocument();
- }
- }
- else if (PDFViewer.ToolManager != null && PDFViewer.GetPDFEditCreateType() == ComPDFKit.PDFPage.CPDFEditType.EditImage)
- {
- if (PDFViewer.GetPDFEditSelectionCount(ComPDFKit.PDFPage.CPDFEditType.EditImage) == 0)
- {
- PDFViewer.RemovePDFEditEmptyText();
- if (PDFViewer.MouseMode == MouseModes.PDFEdit && PDFViewer.ToolManager.HasTool == true)
- {
- PDFViewer.RemoveTool(false);
- }
- else if (IsTextEdit == true || IsImgEdit == true)
- {
- 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();
- ShowPropertyPanel(false);
- }
- }
- else
- {
- PDFViewer.ClearSelectPDFEdit();
- PDFViewer.ReloadDocument();
- }
- }
- else if (PDFViewer.ToolManager != null)
- {
- PDFViewer.ClearSelectPDFEdit();
- AddToPropertyPanel("PropertyPanelContentEdit", null);
- PDFViewer.ReloadDocument();
- }
- }
- }
- }
- catch
- {
- }
- }
- #endregion 快捷键
- //模式选择进入
- public void AddContent(object obj)
- {
- if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
- //判断是否已退出登录,限制编辑操作
- if (!App.IsLogin)
- {
- IsImgEdit = false;
- IsTextEdit = false;
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
- PDFViewer.ReloadDocument();
- App.mainWindowViewModel.OpenLogin();
- 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")
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditText);
- DataTrackingHelper.IsClearEntryPath = true;
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddText");
- IsImgEdit = false;
- IsTextEdit = true;
- //只框选文本
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
- pdfDefaultEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
- //单击添加文本
- PDFViewer.ToolManager.EnableClickCreate = true;
- PDFViewer.ToolManager.ClickCreateWidth = 60;
- PDFViewer.ToolManager.ClickCreateHeight = 30;
- List<PDFEditEvent> pdfEditEvents = new List<PDFEditEvent>();
- if (pdfDefaultEditEvent != null)
- {
- pdfEditEvents.Add(pdfDefaultEditEvent);
- }
- AddToPropertyPanel("Text", pdfEditEvents);
- ShowPropertyPanel(true);
- }
- else
- {
- DataTrackingHelper.SetSendInformation(DataTrackingHelper.EventType.Purchase_EditPDF, DataTrackingHelper.EntryPathKeyType.SubTbr_Tools);
- DataTrackingHelper.IsClearEntryPath = true;
- DataTrackingHelper.AddFirstAndSecondaryPath(DataTrackingHelper.FirstPath.Reading, DataTrackingHelper.SecondaryPath.EditImage);
- DataTrackingHelper.SendEvent(DataTrackingHelper.EventType.SubTbr_EditPDF, "SubTbr_Btn", "Btn_SubTbr_AddImage");
- IsImgEdit = true;
- IsTextEdit = false;
- //只框选图像
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- //未设置,将无法触发Active事件
- PDFViewer.ToolManager.EnableClickCreate = true;
- PDFViewer.ToolManager.ClickCreateWidth = 60;
- PDFViewer.ToolManager.ClickCreateHeight = 30;
- AddToPropertyPanel("PropertyPanelContentEdit", null);
- ShowPropertyPanel(true);
- }
- }
- else
- {
- IsImgEdit = false;
- IsTextEdit = false;
- //添加按钮取消选中 清除空文本和选中文本框的状态
- PDFViewer.ClearSelectPDFEdit();
- PDFViewer.RemovePDFEditEmptyText();
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
- //文本和图像都框选
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- ShowPropertyPanel(false);
- }
- }
- #region Command功能
- private void AddText()
- {
- EnterEditMode(true, "Text");
- }
- private void AddImg()
- {
- EnterEditMode(true, "Image");
- }
- //进入裁剪模式
- private void CropMode()
- {
- if (TextEditEvent != null)
- {
- TextEditEvent.ClipImage = true;
- TextEditEvent.UpdatePDFEditByEventArgs();
- flg = true;
- }
- }
- //取消裁剪
- private void CancelCropImg()
- {
- if (TextEditEvent != null)
- {
- TextEditEvent.ClipImage = false;
- TextEditEvent.CancelClip();
- flg = false;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- }
- //还原裁剪
- private void RestoreCropImg()
- {
- if (TextEditEvent != null)
- {
- TextEditEvent.RestoreClip();
- events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
- }
- }
- //完成裁剪
- private void CropImg()
- {
- CropImgflg = true;
- }
- //导出图片
- private void ExportImg()
- {
- if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return;
- System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
- folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
- if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- if (string.IsNullOrEmpty(folder.SelectedPath))
- return;
- var keyValueList = PDFViewer.GetSelectedImages();
- int i = 0;
- foreach (var bitmap in keyValueList)
- {
- foreach (var bitmapItem in bitmap.Value)
- {
- bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png);
- i++;
- }
- }
- var strFilePath = folder.SelectedPath + "\\0.png";
- CommonHelper.ShowFileBrowser(strFilePath);
- }
- }
- //替换图片
- private void ReplaceImg()
- {
- ReplaceImgflg = true;
- }
- //选中的图像
- private System.Windows.Media.Imaging.BitmapSource _currentImg;
- public System.Windows.Media.Imaging.BitmapSource CurrentImg
- { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } }
- private void GetImagePreView()
- {
- try
- {
- var list = PDFViewer.GetSelectedImages();
- if (list != null && list.Count > 0)
- {
- System.Drawing.Bitmap bitmap = null;
- foreach (var item in list)
- {
- if (item.Value.Count > 0)
- {
- bitmap = item.Value[0];
- break;
- }
- }
- if (bitmap != null)
- {
- IntPtr ip = bitmap.GetHbitmap();
- System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty,
- System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
- CurrentImg = bitmapSource;
- }
- }
- }
- catch
- {
- }
- }
- #endregion Command功能
- //传参数给属性面板
- 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);
- parameters.Add(ParameterNames.TextEditToolContentViewModel, this);
- System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
- {
- regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
- }));
- if (e != null)
- {
- ShowPropertyPanel(true);
- }
- else if (e == null && IsImgEdit == false && IsTextEdit == false)
- {
- ShowPropertyPanel(false);
- }
- }
- }
- //控制属性面板是否展示
- private void ShowPropertyPanel(bool show = true)
- {
- viewContentViewModel.IsPropertyOpen = show;
- }
- private void OnViewModel1PropertyChanged(object sender, PropertyChangedEventArgs e)
- {
- if (e.PropertyName == "TabSelectedIndex")
- {
- if (viewContentViewModel.ToolTooRow == new GridLength(40))
- {
- IsImgEdit = false;
- IsTextEdit = false;
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.EditText | ComPDFKit.PDFPage.CPDFEditType.EditImage);
- PDFViewer.SetMouseMode(MouseModes.PDFEdit);
- PDFViewer.ReloadDocument();
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
- AddToPropertyPanel("PropertyPanelContentEdit", null);
- ShowPropertyPanel(false);
- }
- else
- {
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
- PDFViewer.ReloadDocument();
- }
- }
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- if (PDFViewer != null)
- {
- viewContentViewModel.PropertyChanged += OnViewModel1PropertyChanged;
- //左键激活
- 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;
- pdfDefaultEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
- }
- }
- private async void PDFViewer_PDFEditHandler(object sender, List<PDFEditSelectionData> e)
- {
- #region 后续 Undo 操作
- //if (e != null && e.Count > 0)
- //{
- // foreach (var item in e)
- // {
- // if (item is PDFEditSelectionData editSelectionData)
- // {
- // switch (editSelectionData.Action)
- // {
- // case ActionType.Add:
- // PDFViewer.ClearSelectPDFEdit();
- // PDFViewer.SelectPDFEdit(e, true);
- // break;
- // case ActionType.Modify:
- // break;
- // }
- // }
- // }
- //}
- #endregion 后续 Undo 操作
- PDFViewer.ClearSelectPDFEdit();
- if (e != null)
- {
- //需要区分 图片和文本,不然框选添加 文本,会出现光标消失
- if (e[0].EditType == CPDFEditType.EditImage)
- {
- //延时 =》添加图片,弹出系统文件弹窗,双击选中图片后添加到页面上,需要选中图片
- await Task.Delay(50);
- }
- }
- PDFViewer.SelectPDFEdit(e, true);
- }
- protected PDFEditEvent TextEditEvent;
- //左键激活逻辑
- private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
- {
- PDFViewer.ToolManager.IgnoreMoveDistance = 5;
- //判断是否已退出登录,限制编辑操作
- if (!App.IsLogin)
- {
- IsImgEdit = false;
- IsTextEdit = false;
- PDFViewer.SetPDFEditType(ComPDFKit.PDFPage.CPDFEditType.None);
- PDFViewer.ReloadDocument();
- App.mainWindowViewModel.OpenLogin();
- return;
- }
- //从配置文件拿样式
- pdfDefaultEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
- if (e != null && e.Count > 0)
- {
- TextEditEvent = e[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)
- {
- if (e.Count == 1 && string.IsNullOrEmpty(e[0].FontName))
- {
- e = SynchronizeToPropertyPanel(e, defaultEdit);
- }
- AddToPropertyPanel("Text", e);
- }
- if (isText == false && isImg == true)
- {
- AddToPropertyPanel("Image", e);
- }
- if (isText == true && isImg == true)
- {
- AddToPropertyPanel("TextAndImage", e);
- }
- }
- ///未事先设置 if里面的内容,单击添加文本会失败
- //else if(IsTextEdit==true)
- //{
- // //单击添加文本
- // PDFViewer.ToolManager.EnableClickCreate = true;
- // PDFViewer.ToolManager.ClickCreateWidth = 60;
- // PDFViewer.ToolManager.ClickCreateHeight = 30;
- // AddToPropertyPanel("Text", null);
- //}
- else
- {
- Trace.WriteLine("PropertyPanelContentEdit");
- AddToPropertyPanel("PropertyPanelContentEdit", null);
- }
- events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
- }
- /// <summary>
- /// 拿配置文件的属性,刷新属性面板
- /// </summary>
- /// <param name="e"></param>
- /// <param name="defaultEdit"></param>
- /// <returns></returns>
- private List<PDFEditEvent> SynchronizeToPropertyPanel(List<PDFEditEvent> e, DefaultEditProperty defaultEdit)
- {
- List<PDFEditEvent> editEvents = new List<PDFEditEvent>();
- PDFEditEvent editEvent = e[0];
- if (defaultEdit != null)
- {
- editEvent.FontName = defaultEdit.FontName;
- editEvent.FontSize = defaultEdit.FontSize;
- editEvent.TextAlign = defaultEdit.TextAlign;
- editEvent.AutoBlock = false;
- //editEvent.ClipImage = defaultEdit.ClipImage;
- //editEvent.EditType = defaultEdit.EditType;
- editEvent.FontColor = defaultEdit.FontColor;
- //editEvent.HorizontalMirror = defaultEdit.HorizontalMirror;
- //editEvent.VerticalMirror = defaultEdit.VerticalMirror;
- editEvent.IsBold = defaultEdit.IsBold;
- editEvent.IsItalic = defaultEdit.IsItalic;
- //editEvent.ReplaceImagePath = defaultEdit.ReplaceImagePath;
- //editEvent.Rotate = defaultEdit.Rotate;
- //editEvent.Transparency = defaultEdit.Transparency;
- //PDFViewer.SetPDFEditParam(editEvent);
- e[0] = editEvent;
- editEvents = e;
- }
- return editEvents;
- }
- //右键点击逻辑
- 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.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);
- }
- }
- else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
- {
- if (e.IsClipState == true)
- {
- e.PopupMenu = CropImgPDFEdit(sender);
- }
- else
- {
- e.PopupMenu = SelectImgPDFEdit(sender);
- }
- }
- break;
- default:
- e.DoCommand();
- break;
- }
- if (e.PopupMenu != null)
- {
- e.Handle = true;
- }
- flg = e.IsClipState;
- }
- //图片添加逻辑
- private void PDFViewer_CustomNotifyHandler(object sender, CustomNotityData e)
- {
- //图片创建
- if (e.NotifyType == CustomNotifyType.PDFEditImageCreate)
- {
- OpenFileDialog openFileDialog = new OpenFileDialog();
- openFileDialog.Filter = "Image Files(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp;";
- if (openFileDialog.ShowDialog() == true)
- {
- PDFViewer.AddPDFEditImage(CompressImage(openFileDialog.FileName, 800), 500, 500);
- events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
- }
- }
- //图片调整大小
- else if (e.NotifyType == CustomNotifyType.PDFEditImageResize)
- {
- REImgflg = true;
- events.GetEvent<PageEditNotifyEvent>().Publish(new PageEditNotifyEventArgs(unicode));
- }
- }
- /// <summary>
- /// 压缩
- /// </summary>
- /// <param name="filePath"></param>
- /// <param name="maxwidth"></param>
- /// <returns></returns>
- public static string CompressImage(string filePath, int maxwidth = 600)
- {
- try
- {
- string sourcepath = filePath;
- var guid = Guid.NewGuid().ToString();
- string folder = Path.Combine(App.CurrentPath, "Temp");
- if (!Directory.Exists(folder))
- {
- Directory.CreateDirectory(folder);
- }
- var path = System.IO.Path.Combine(App.CurrentPath, "Temp", guid);
- Bitmap bitmap = new Bitmap(sourcepath);
- var b = bitmap;
- //bitmap.Dispose();
- double scale = Math.Min((double)maxwidth / b.Width, (double)maxwidth / b.Height);
- scale = Math.Min(scale, 1);
- System.Drawing.Size newsize = new System.Drawing.Size(maxwidth, maxwidth);
- newsize.Width = (int)(scale * b.Width);
- newsize.Height = (int)(scale * b.Height);
- if (!File.Exists(path))
- {
- if (CheckTextFile(sourcepath) == FileExtension.PNG)
- {
- using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
- {
- bp.Save(path, ImageFormat.Png);
- }
- }
- else
- {
- using (var bp = new Bitmap(b, (int)newsize.Width, (int)newsize.Height))
- {
- bp.Save(path, ImageFormat.Jpeg);
- }
- }
- }
- return path;
- }
- catch
- {
- return filePath;
- }
- }
- /// <summary>
- /// 根据图片数据判断图片类型
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static FileExtension CheckTextFile(string fileName)
- {
- FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
- System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
- string fileType = string.Empty; ;
- try
- {
- byte data = br.ReadByte();
- fileType += data.ToString();
- data = br.ReadByte();
- fileType += data.ToString();
- FileExtension extension;
- try
- {
- extension = (FileExtension)Enum.Parse(typeof(FileExtension), fileType);
- }
- catch
- {
- extension = FileExtension.VALIDFILE;
- }
- return extension;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- if (fs != null)
- {
- fs.Close();
- br.Close();
- }
- }
- }
- 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);
- }
- }
- }
|