using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using Microsoft.Office.Interop.Excel; using Microsoft.Office.Interop.Word; using Microsoft.Win32; using PDF_Office.CustomControl; using PDF_Office.EventAggregators; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Properties; using PDF_Office.ViewModels.BOTA; using PDF_Office.ViewModels.PropertyPanel; using PDF_Office.ViewModels.PropertyPanel.AnnotPanel; using PDF_Office.Views.BOTA; using PDF_Office.Views.PropertyPanel.AnnotPanel; using PDFSettings; using PDFSettings.Settings; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Input; using System.Windows.Media; using ContextMenu = System.Windows.Controls.ContextMenu; using KeyEventArgs = System.Windows.Input.KeyEventArgs; using MenuItem = System.Windows.Controls.MenuItem; namespace PDF_Office.ViewModels.Tools { public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware { #region 初始化 public AnnotToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService) { region = regionManager; events = eventAggregator; dialogs = dialogService; InitCommand(); InitDefaultValue(); InitToolDict(); InitToolTipDict(); InitPopMenu(); SubscribeEvent(); } //事件聚合器 private void SubscribeEvent() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode; events.GetEvent().Subscribe(FromFillAndSign, e => e.AppUnicode == Unicode); } private void InitPopMenu() { InitSelectHightAnnotMenu(); InitSelectFreeHandAnnotMenu(); InitSelectFreeTextAnnotMenu(); InitSelectStrickNoteAnnotMenu(); InitSelectShapeAnnotMenu(); InitSelectLinkAnnotMenu(); InitSelectStampAnnotMenu(); InitSelectMultiAnnotMenu(); } private void InitCommand() { MyToolsCommand = new DelegateCommand(BtnMyTools_Click); SetAddAnnotationCommand = new DelegateCommand(AddAnnotation_Click); AddBookMarkCommand = new DelegateCommand(AddBookMark_Click); PropertyRegionName = Guid.NewGuid().ToString(); HandCommand = new DelegateCommand(Hand_Click); #region 注释 - 右键菜单 //公共command AnnotDefaultValue_MenuCommand = new DelegateCommand(AnnotDefaultValues_Menu); AnnotColorPalette_MenuCommand = new DelegateCommand(AnnotColorPalette_Menu); AnnotAddNoteText_MenuCommand = new DelegateCommand(AnnotAddNoteText_Menu); //高亮、下划线、删除 HightAnnotCopyText_MenuCommand = new DelegateCommand(HightAnnotCopyText_Menu); //手绘 FreeHandLineStyle_MenuCommand = new DelegateCommand(FreeHandLineStyle_Menu); //文本 FreeTextFontFamily_MenuCommand = new DelegateCommand(FreeTextFontFamily_Menu); FreeTextAglin_MenuCommand = new DelegateCommand(FreeTextAglin_Menu); //便签 StrikeNoteEditStrike_MenuCommand = new DelegateCommand(StrikeNoteEditStrike_Menu); //形状 ShapeLineStyle_MenuCommand = new DelegateCommand(ShapeLineStyle_Menu); ShapeLineDirect_MenuCommand = new DelegateCommand(ShapeLineDirect_Menu); //图章 StampExportPicture_MenuCommand = new DelegateCommand(StampExportPicture_Menu); //链接 Link_MenuCommand = new DelegateCommand(Link_Menu); #endregion 注释 - 右键菜单 } private void Hand_Click(object obj) { HandToolIsCheckedEvent(BtnHandIsChecked); if (BtnHandIsChecked) { //其余注释工具 取消选中 StrAnnotToolChecked = ""; BtnLinkIsChecked = false; viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } } private void HandToolIsCheckedEvent(bool isChecked) { if (isChecked == false) { //鼠标框选多选注释 if (PDFViewer.MouseMode != MouseModes.AnnotCreate) { PDFViewer.SetMouseMode(MouseModes.AnnotCreate); var annotArgs = new SelectToolArgs(); PDFViewer.SetToolParam(annotArgs); } else { PDFViewer.SetMouseMode(MouseModes.PanTool); } } else { //鼠标滚动页面 PDFViewer.SetMouseMode(MouseModes.Scroll); PDFViewer.SetToolParam(new AnnotHandlerEventArgs()); } } #endregion 初始化 #region Command实现 //点击注释工具 private void BtnMyTools_Click(CustomIconToggleBtn annotBtn) { //不创建注释,属于注释模板 bool isTemplateAnnot = false; bool isSnapshotEdit = false; AnnotHandlerEventArgs annotArgs = null; if (annotBtn.Name == "BtnShowAnnot") { PDFViewer.SetDrawAnnot((bool)annotBtn.IsChecked); return; } if (annotBtn.IsChecked == true) { if (BtnHandIsChecked) { BtnHandIsChecked = false; } PDFViewer.ClearSelectAnnots(); var tag = annotBtn.Tag.ToString(); FindAnnotTypeKey(tag, ref annotArgs); if (tag == "SnapshotEdit") { isSnapshotEdit = true; } else if (tag == "Signature" || tag == "Stamp") { isTemplateAnnot = true; } if (tag != "Link") BtnLinkIsChecked = false; else StrAnnotToolChecked = "Link"; if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); if (tag == "Rect" || tag == "Circle" || tag == "Arrow" || tag == "Line") { PDFViewer.ToolManager.EnableClickCreate = true; PDFViewer.ToolManager.ClickCreateWidth = 140; PDFViewer.ToolManager.ClickCreateHeight = 140; } else { PDFViewer.ToolManager.EnableClickCreate = false; } #region 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏 //isRightMenuAddAnnot = false; #endregion 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏 } } else { propertyPanel.IsAddLink = false; propertyPanel.IsLocationLink = false; PDFViewer.ToolManager.EnableClickCreate = false; } //当不是注释模板,且无创建注释时,属性面板显示为空内容 if (isTemplateAnnot == false && annotArgs == null) { PDFViewer.SetMouseMode(MouseModes.PanTool); viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } else { if (isSnapshotEdit == true) { ShowPropertyPanel(false); isSnapshotEdit = false; } else { ShowPropertyPanel((bool)annotBtn.IsChecked); } } } #endregion Command实现 #region BOTA private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel, out AnnotationContent annotation) { AnnotationContentViewModel viewModel = null; annotation = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is AnnotationContent annotation1) { annotation = annotation1; viewModel = annotation1.DataContext as AnnotationContentViewModel; break; } } } } return viewModel; } /// /// 判断Bota的TabItem是否已显示 /// /// /// /// /// private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText) { bool isTabItem = false; bOTAContentViewModel = null; bOTAContent = null; if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName)) { var views = region.Regions[viewContentViewModel.BOTARegionName].Views; var model = views.FirstOrDefault(q => q is BOTAContent); if (model is BOTAContent bOTAContent1) { bOTAContent = bOTAContent1; bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel; if (bOTAContentViewModel.CurrentBar == tabItemText) { isTabItem = true; } } //foreach (var item in views) //{ // if (item is BOTAContent bOTAContent1) // { // bOTAContent = bOTAContent1; // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel; // if (bOTAContentViewModel.CurrentBar == tabItemText) // { // isTabItem = true; // break; // } // } //} } return isTabItem; } private void Select_Click(object sender, RoutedEventArgs e) { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch"); if (isBook == false) { bOTAContent.TabItemSearch.IsSelected = true; } } #endregion BOTA #region 阅读页右键菜单 private void PageDisplay(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.IsCheckable = false; menuItem.Click -= PageDisplay_Click; menuItem.Click += PageDisplay_Click; } } } private void PageDisplay_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "SingleView": PDFViewer.ChangeViewMode(ViewMode.Single); break; case "SingleContinueView": PDFViewer.ChangeViewMode(ViewMode.SingleContinuous); break; case "DoubleView": PDFViewer.ChangeViewMode(ViewMode.Double); break; case "DoubleContinueView": PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous); break; case "BookMode": PDFViewer.ChangeViewMode(ViewMode.Book); break; } } } private void ViewZoom(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { //switch (PDFViewer.ModeFit) //{ // case FitMode.FitWidth: // break; // case FitMode.FitHeight: // break; // case FitMode.FitSize: // break; // case FitMode.FitFree: // break; // default: // break; //} menuItem.IsCheckable = false; menuItem.Click -= ViewZoom_Click; menuItem.Click += ViewZoom_Click; } } } private void ViewZoom_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "FitWidth": PDFViewer.ChangeFitMode(FitMode.FitWidth); break; case "FitHeight": PDFViewer.ChangeFitMode(FitMode.FitHeight); break; case "FitSize": PDFViewer.ChangeFitMode(FitMode.FitSize); break; case "enlarge": double newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + 0.01), true); PDFViewer.Zoom(newZoom); break; case "narrow": newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + -0.01), false); PDFViewer.Zoom(newZoom); break; } } } private void UnReadModel_Click(object sender, RoutedEventArgs e) { viewContentViewModel.UnReadModel(); } private void ReadModel_Click(object sender, RoutedEventArgs e) { viewContentViewModel.RbtnReadMode(); } private void SetToolMode(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { menuItem.IsCheckable = false; menuItem.Click -= ToolMode_Click; menuItem.Click += ToolMode_Click; } } } private void ToolMode_Click(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { switch (menuItem.Tag.ToString()) { case "default": HandToolIsCheckedEvent(false); BtnHandIsChecked = false; ShowPropertyPanel(false); break; case "roll": HandToolIsCheckedEvent(true); BtnHandIsChecked = true; ShowPropertyPanel(false); break; case "content": AnnotHandlerEventArgs annotHandler = GetSnapshotEdit(); BtnSelecttoolIsChecked = true; if (annotHandler != null) { annotHandler.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotHandler); ShowPropertyPanel(false); } break; default: break; } } } private void DelBookMark_Click(object sender, RoutedEventArgs e) { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); if (isBook == false) { bOTAContent.TabItemBookMark.IsSelected = true; } BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark); int index = PDFViewer.CurrentIndex; //检测是否已存在相同数据 CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index); if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null && list != null) { bookmarkContentViewModel.DeleteCommand.Execute(list); isAddBookMark = bookmarkContentViewModel.isAddBookMark; } // bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); //if (isBook == false || viewContentViewModel.OpenBOTA == false) //{ // viewContentViewModel.OpenBOTA = true; // bOTAContent.TabItemBookMark.IsSelected = true; //} } private void AddBookMark_Click() { if (viewContentViewModel.OpenBOTA == false) { viewContentViewModel.OpenBOTA = true; } bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); if (isBook == false) { bOTAContent.TabItemBookMark.IsSelected = true; } BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentViewModel, out BookmarkContent bookmark); if (bookmarkContentViewModel != null && bookmark != null && bOTAContent != null) { //bookmarkContentViewModel.AddBookmarkCommand.Execute(bookmark.BookMarkListView); bookmark.BtnAddBookmark_Click(null, null); isAddBookMark = bookmarkContentViewModel.isAddBookMark; return; } } private BookmarkContentViewModel GetBookmarkContentViewModel(BOTAContentViewModel bOTAContentViewModel, out BookmarkContent bookmark) { BookmarkContentViewModel viewModel = null; bookmark = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is BookmarkContent bookmark1) { bookmark = bookmark1; viewModel = bookmark1.DataContext as BookmarkContentViewModel; break; } } } } return viewModel; } private OutLineControlViewModel GetOutLineControlViewModel(BOTAContentViewModel bOTAContentViewModel, out OutLineControl outLineControl) { OutLineControlViewModel viewModel = null; outLineControl = null; if (bOTAContentViewModel != null) { if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName)) { var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views; foreach (var item in views) { if (item is OutLineControl outLineControl1) { outLineControl = outLineControl1; viewModel = outLineControl1.DataContext as OutLineControlViewModel; break; } } } } return viewModel; } private void DisplayAnnot_Click(object sender, RoutedEventArgs e) { PDFViewer.SetDrawAnnot(true); BtnShowAnnotIsChecked = true; bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); viewModel.DisplayAnnot(); } } private void HiddenAnnot_Click(object sender, RoutedEventArgs e) { PDFViewer.SetDrawAnnot(false); BtnShowAnnotIsChecked = false; bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); viewModel.HiddenAnnot(); } } private void SetAddAnnotation(ItemCollection items) { foreach (var item in items) { if (item is MenuItem menuItem) { string str = menuItem.Tag.ToString(); if (str == AddAnnotType.AnnotLink.ToString() || str == AddAnnotType.AnnotStamp.ToString() || str == AddAnnotType.AnnotAutograph.ToString() ) { InBookModeSetIsEnabled(menuItem); } menuItem.CommandParameter = item; menuItem.Command = SetAddAnnotationCommand; } } } private void AddLink_Click(object sender, RoutedEventArgs e) { var args = PDFViewer.ToolManager.CurrentAnnotArgs; BtnLinkIsChecked = true; } #endregion 阅读页右键菜单 #region 快捷键 private void ShortCut_KeyDown(object sender, KeyEventArgs e) { if (KeyEventsHelper.IsSingleKey(Key.Escape)) { BtnHandIsChecked = true; PDFViewer.SetMouseMode(MouseModes.Scroll); PDFViewer.ClearSelectAnnots(); StrAnnotToolChecked = ""; ShowPropertyPanel(false); } else if (KeyEventsHelper.IsModifierKey(ModifierKeys.Shift, Key.None)) { //按住Shift等比缩放 } AnnotToolCtrlAltKey(); } /// /// 按下Ctrl + Alt + 序列键,响应注释工具 /// private void AnnotToolCtrlAltKey() { var tuple = GetCtrlAltKey(); if (tuple != null) { if (tuple.Item1 == Key.L) BtnLinkIsChecked = false; AnnotHandlerEventArgs annotArgs = null; StrAnnotToolChecked = tuple.Item2; FindAnnotTypeKey(tuple.Item2, ref annotArgs); if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); PDFViewer.ToolManager.EnableClickCreate = false; } } } private Tuple GetCtrlAltKey() { Dictionary AnnotToolDir = new Dictionary(); AnnotToolDir.Add(Key.H, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.H)); AnnotToolDir.Add(Key.U, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.U)); AnnotToolDir.Add(Key.S, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.S)); AnnotToolDir.Add(Key.P, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.P)); AnnotToolDir.Add(Key.E, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.E)); AnnotToolDir.Add(Key.T, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.T)); AnnotToolDir.Add(Key.N, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.N)); AnnotToolDir.Add(Key.R, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.R)); AnnotToolDir.Add(Key.O, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.O)); AnnotToolDir.Add(Key.A, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.A)); AnnotToolDir.Add(Key.L, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.L)); Key key = Key.None; foreach (var item in AnnotToolDir.Keys) { if (AnnotToolDir[item] == true) { key = item; break; } } if (key == Key.None) return null; var tag = ""; if (key == Key.H || key == Key.U || key == Key.S) { var str = PDFViewer.GetSelectedText(); if (string.IsNullOrEmpty(str) == false) { if (key == Key.H) tag = "HighLight"; else if (key == Key.U) tag = "UnderLine"; else if (key == Key.S) tag = "Strikeout"; } else { return null; } } else { if (AnnotToolDir[Key.P]) tag = "Freehand"; else if (AnnotToolDir[Key.T]) tag = "Freetext"; else if (AnnotToolDir[Key.N]) tag = "StickyNote"; else if (AnnotToolDir[Key.R]) tag = "Rect"; else if (AnnotToolDir[Key.O]) tag = "Circle"; else if (AnnotToolDir[Key.A]) tag = "Arrow"; else if (AnnotToolDir[Key.L]) tag = "Line"; } if (tag == "") return null; return new Tuple(key, tag); } #endregion 快捷键 #region Navigation public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { StrAnnotToolChecked = ""; UnBindingPDFViewerHandler(); UnBindingEvent(); } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (viewContentViewModel != null) { OpenFileInfo = viewContentViewModel.OpenFileInfo; } BindingPDFViewerHandler(); BindingEvent(); LoaedStickyNotePopup(); BtnHandIsChecked = (PDFViewer.MouseMode == MouseModes.Scroll ? true : false); } #endregion Navigation } }