using ComPDFKit.PDFAnnotation; using ComPDFKit.PDFDocument.Action; using ComPDFKit.PDFDocument; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Master.CustomControl; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Properties; using PDF_Master.ViewModels.BOTA; using PDF_Master.Views.BOTA; using PDFSettings; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Diagnostics; using PDF_Master.Model.AnnotPanel; using System.Windows.Input; using PDF_Master.ViewModels.Tools.AnnotManager; using ComPDFKit.Import; using System.Windows.Markup; namespace PDF_Master.ViewModels.Tools { public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware { #region 事件绑定和解绑 private void BindingEvent() { //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值 //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色 propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged; propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged; propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged; propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged; //快捷键 KeyEventsHelper.KeyDown -= ShortCut_KeyDown; KeyEventsHelper.KeyDown += ShortCut_KeyDown; } private void UnBindingEvent() { propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged; propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged; //快捷盘解绑 KeyEventsHelper.KeyDown -= ShortCut_KeyDown; } private void BindingPDFViewerHandler() { //来自PDFViewer的响应事件 if (PDFViewer != null) { PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler; PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler; PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler; PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler; PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler; PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler; PDFViewer.PDFActionHandler += PDFViewer_PDFActionHandler; PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler; PDFViewer.AnnotHoverHandler += PDFViewer_AnnotHoverHandler; PDFViewer.PreviewMouseLeftButtonDown -= PDFViewer_PreviewMouseLeftButtonDown; PDFViewer.PreviewMouseLeftButtonDown += PDFViewer_PreviewMouseLeftButtonDown; } } private void UnBindingPDFViewerHandler() { if (PDFViewer != null) { PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler; PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler; PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler; PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler; PDFViewer.PreviewMouseLeftButtonDown -= PDFViewer_PreviewMouseLeftButtonDown; } } #endregion 事件绑定和解绑 #region 与触发事件调用相关的函数 //鼠标左键双击注释 private void PDFViewer_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.ClickCount == 1) { //if (sender is CPDFViewer pDFViewer) //{ // if (pDFViewer.ToolManager.CurrentAnnotArgs != null) // { // if (pDFViewer.ToolManager.CurrentAnnotArgs is FreehandAnnotArgs freehand) // { // PDFViewer.SelectAnnotation(freehand.PageIndex, freehand.AnnotIndex); // } // } //} bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemBookMark"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true && bOTAContent.TabItemBookMark.IsSelected == true) { this.events.GetEvent().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true }); } } if (e.ClickCount == 2) { if (CurrentSelectedAnnot != null) { var type = CurrentSelectedAnnot.EventType; if (type != AnnotArgsType.AnnotSticky && type != AnnotArgsType.AnnotFreeText && type != AnnotArgsType.AnnotHighlight && type != AnnotArgsType.AnnotUnderline && type != AnnotArgsType.AnnotStrikeout) { int dpi = DpiHelpers.Dpi; var rect = CurrentSelectedAnnot.ClientRect; rect = new Rect( rect.Left * PDFViewer.ZoomFactor, rect.Top * PDFViewer.ZoomFactor, rect.Width * PDFViewer.ZoomFactor, rect.Height * PDFViewer.ZoomFactor); var ui = PDFViewer.Parent as ContentControl; if (ui != null) { var point = e.GetPosition(ui); int pageIndex = 0; Point pagePoint = new Point(0, 0); //Todo:等黎总需要更新ComPDFKit.dll //PDFViewer.ConvertViewPointToPage(point, out pageIndex, out pagePoint); if (pagePoint.X >= rect.Left && pagePoint.X <= rect.Right && pagePoint.Y >= rect.Top && pagePoint.Y <= rect.Bottom) PopAnnotNoteText(CurrentSelectedAnnot); } } if (type == AnnotArgsType.AnnotSticky) { (CurrentSelectedAnnot as StickyAnnotArgs).PopupEditWnd(); } } } } //超链接跳转 private void PDFViewer_PDFActionHandler(object sender, ComPDFKit.PDFDocument.Action.CPDFAction action) { if (action == null) { return; } switch (action.ActionType) { case C_ACTION_TYPE.ACTION_TYPE_GOTO: if (PDFViewer != null) { CPDFGoToAction gotoAction = action as CPDFGoToAction; CPDFDestination dest = gotoAction.GetDestination(PDFViewer.Document); if (dest != null) { PDFViewer.GoToPage(dest.PageIndex, new System.Windows.Point(0, 0)); } } break; case C_ACTION_TYPE.ACTION_TYPE_URI: { CPDFUriAction uriAction = action as CPDFUriAction; string uri = uriAction.GetUri(); try { if (!string.IsNullOrEmpty(uri)) { Process.Start(uri); } } catch (Exception ex) { } } break; } } private void GetSelectedAnnots(AnnotAttribEvent e) { var annot = e.AnnotItemsList[0]; switch (annot.EventType) { case AnnotArgsType.AnnotHighlight: GetHighLight(e.AnnotItemsList); break; case AnnotArgsType.AnnotUnderline: GetUnderLine(e.AnnotItemsList); break; case AnnotArgsType.AnnotStrikeout: GetStrikeout(e.AnnotItemsList); break; case AnnotArgsType.AnnotSquiggly: GetSquiggly(e.AnnotItemsList); break; case AnnotArgsType.AnnotFreehand: GetFreehand(e.AnnotItemsList); break; case AnnotArgsType.AnnotFreeText: GetFreetext(e.AnnotItemsList); break; case AnnotArgsType.AnnotSquare: GetRect(e.AnnotItemsList); break; case AnnotArgsType.AnnotCircle: GetCircle(e.AnnotItemsList); break; case AnnotArgsType.AnnotLine: bool isLine = true; if (e.Attribs.ContainsKey(AnnotAttrib.LineStart)) { if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_NONE) { isLine = false; } } if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd)) { if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_NONE) { isLine = false; } } if (isLine) GetArrowLine("Line", e.AnnotItemsList); else GetArrowLine("Arrow", e.AnnotItemsList); break; case AnnotArgsType.AnnotLink: //viewContentViewModel.IsCreateLink = false; //e.IsAnnotCreateReset= true; if (BtnLinkIsChecked == false) { propertyPanel.IsAddLink = false; } GetLink(e.AnnotItemsList, e); break; case AnnotArgsType.AnnotSticky: GetStickyNote(e.AnnotItemsList); customStickyPopup.GetCurrentAnnot = e.AnnotItemsList[0] as StickyAnnotArgs; customStickyPopup.GetPDFViewer = PDFViewer; break; case AnnotArgsType.AnnotStamp: GetStamp(); break; } } private void SelectedSignature(List annotlist) { if (annotlist == null || annotlist.Count == 0) return; var annot = annotlist[0]; if (annot.EventType != AnnotArgsType.AnnotStamp && annot.EventType != AnnotArgsType.AnnotFreehand) return; if (annot.PageIndex < 0 || annot.AnnotIndex < 0) return; var signAnnot = viewContentViewModel.AnnotSignatures.FirstOrDefault(temp => temp.Item1 == annot.PageIndex && temp.Item2 == annot.AnnotIndex); if (signAnnot == null) { if (annot.EventType == AnnotArgsType.AnnotStamp) { StrAnnotToolChecked = "Stamp"; } //else //{ // StrAnnotToolChecked = ""; //} } else { GetSignature(); StrAnnotToolChecked = "Signature"; } } private void AddAnnotSignature(AnnotHandlerEventArgs annot) { if ((annot.EventType != AnnotArgsType.AnnotStamp && annot.EventType != AnnotArgsType.AnnotFreehand) || (annot.PageIndex < 0 || annot.AnnotIndex < 0)) return; if (StrAnnotToolChecked == "Signature") { if (viewContentViewModel.AnnotSignatures == null) viewContentViewModel.AnnotSignatures = new List>(); var signAnnot = viewContentViewModel.AnnotSignatures.FirstOrDefault(temp => temp.Item1 == annot.PageIndex && temp.Item2 == annot.AnnotIndex); if (signAnnot == null) { viewContentViewModel.AnnotSignatures.Add(new Tuple(annot.PageIndex, annot.AnnotIndex)); } } } #endregion 与触发事件调用相关的函数 #region PDFViewer事件 private AnnotHandlerEventArgs CurrentSelectedAnnot = null; //选中和非选中注释 private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e) { if (e != null) { CurrentSelectedAnnot = null; var annot = e.AnnotItemsList[0]; if (annot != null) { if (e.AnnotItemsList.Count == 1) { //IsAnnotCreateReset:是否为创建注释的状态 if (e.IsAnnotCreateReset == false) { GetSelectedAnnots(e); SelectedSignature(e.AnnotItemsList); CurrentSelectedAnnot = annot; if (Settings.Default.AppProperties.InitialVIew.AutoExpandProperty) ShowPropertyPanel(true); } else { switch (annot.EventType) { case AnnotArgsType.AnnotFreeText: GetFreetext(e.AnnotItemsList); PDFViewer.SetToolParam(annot); break; case AnnotArgsType.AnnotSticky: customStickyPopup.GetCurrentAnnot = e.AnnotItemsList[0] as StickyAnnotArgs; customStickyPopup.GetPDFViewer = PDFViewer; break; case AnnotArgsType.AnnotLink: //viewContentViewModel.IsCreateLink = false; GetLink(e.AnnotItemsList, e); PDFViewer.SetToolParam(annot); break; } #region //TODO: 修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉 //注释代码原因:设计已经重新调整逻辑:所有注释创建后,编辑属性,所有属性会作用于下次创建的注释中。 //if (annot.EventType != AnnotArgsType.AnnotStrikeout && //annot.EventType != AnnotArgsType.AnnotUnderline && //annot.EventType != AnnotArgsType.AnnotHighlight && //annot.EventType != AnnotArgsType.AnnotSquiggly && //annot.EventType != AnnotArgsType.AnnotLink && //annot.EventType != AnnotArgsType.AnnotFreehand && //annot.EventType != AnnotArgsType.AnnotSticky && //) //{ // if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType)) // { // var strLineAnnotTag = ""; // if (e.AnnotItemsList[0] is LineAnnotArgs) // { // var lineAnnot = e.AnnotItemsList[0] as LineAnnotArgs; // if (lineAnnot.HeadLineType == C_LINE_TYPE.LINETYPE_NONE && lineAnnot.TailLineType == C_LINE_TYPE.LINETYPE_NONE) // { // strLineAnnotTag = "Line"; // } // else // { // strLineAnnotTag = "Arrow"; // } // } // foreach (var item in ToolExpandDict) // { // if (item.Value == e.AnnotItemsList[0].EventType) // { // annot = null;//新建注释时,回到默认值 // if (string.IsNullOrEmpty(strLineAnnotTag)) // { // FindAnnotTypeKey(item.Key, ref annot); // break; // } // else // { // if (strLineAnnotTag == item.Key) // { // FindAnnotTypeKey(item.Key, ref annot); // break; // } // } // } // } // PDFViewer.SetToolParam(annot); // } //} //设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏 //if (isRightMenuAddAnnot && annot.EventType!=AnnotArgsType.AnnotLink) //{ // ShowPropertyPanel(false); //} //else //{ // ShowPropertyPanel(); //} #endregion //TODO: 修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉 } } else { bool isDifferentAnnotTyle = AnnotTransfer.IsDifferentTypeAnnots(e.AnnotItemsList); if (isDifferentAnnotTyle) viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); else GetSelectedAnnots(e); } } //注释列表同步选中 var list = e.GetPageAnnotsIndex(); bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true && bOTAContent.TabItemAnnotation.IsSelected == true && list != null && list.Count > 0) { if (list.Keys.Count == 0) { return; } var pageindex = new List(list.Keys); List annotes = new List(); list.TryGetValue(pageindex[0], out annotes); int annoteindex = annotes[0]; AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); if (viewModel != null) { viewModel.ScrollToAnnot(pageindex[0], annoteindex, annotation.AnnotationList); } } } else { if (BtnLinkIsChecked == false) { if (PDFViewer.MouseMode != MouseModes.AnnotCreate) { if (PDFViewer.MouseMode == MouseModes.PanTool && propertyPanel.IsAddLink == false && propertyPanel.IsLocationLink == false) { viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } } else { if (propertyPanel.IsMultiSelected) viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } } } } //在注释工具的状态下,右键菜单 private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e) { if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)) return; if ((UIElement)sender != null) { //绑定命令执行前的拦截事件,对于未解密权限的文档进行复制限制 CommandManager.AddPreviewExecutedHandler((UIElement)sender, PreviewExcute); } switch (e.CommandType) { case CommandType.Context: if (e.AnnotEventArgsList.Count > 0) { if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction) { //绑定标记密文处右键菜单 events.GetEvent().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e }); } else { if (e.AnnotEventArgsList.Count == 1) { var selectedAnnot = e.AnnotEventArgsList[0]; PopMenuCheckedLineDash(selectedAnnot); PopMenuCheckedFontFamily(selectedAnnot); PopMenuCheckedTextAglin(selectedAnnot); switch (selectedAnnot.EventType) { case AnnotArgsType.AnnotHighlight: case AnnotArgsType.AnnotUnderline: case AnnotArgsType.AnnotStrikeout: case AnnotArgsType.AnnotSquiggly: if (selectedAnnot.EventType == AnnotArgsType.AnnotHighlight) { colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Highlight); } else { colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Border); } e.PopupMenu = HightAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotFreehand: e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotFreeText: e.PopupMenu = FreeTextAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotSticky: e.PopupMenu = StrickNoteAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotSquare: case AnnotArgsType.AnnotCircle: ShapeAnnotPopMenu.SetVisual("ShapeDirect", false); ShapeAnnotPopMenu.SetVisual("ShapeFillColor", true); e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotLine: ShapeAnnotPopMenu.SetVisual("ShapeFillColor", false); ShapeAnnotPopMenu.SetVisual("ShapeDirect", true); e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotLink: e.PopupMenu = LinkAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; case AnnotArgsType.AnnotStamp: e.PopupMenu = StampAnnotPopMenu.OpenMenu(selectedAnnot, sender); break; } } else { bool isHigh = true;//是否为高亮 foreach (var item in e.AnnotEventArgsList) { if (AnnotTransfer.IsHightAnnot(item) == false) { isHigh = false; break; } } MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh); MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh); e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender); } } if (e.PopupMenu != null) { e.Handle = true; } } else { if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection) { e.PopupMenu = SelectedTextOrImageContextMenu(sender, e); if (e.PopupMenu != null) { e.Handle = true; } } else { e.PopupMenu = ViewerContextMenu(sender); if (e.PopupMenu != null) { e.Handle = true; } } } break; } } private void PreviewExcute(object sender, ExecutedRoutedEventArgs e) { if (e.Command == ApplicationCommands.Copy) { VerifyPasswordResult result = SecurityHelper.VerifyPasswordForSelectedPermissions(PDFViewer.Document, Model.Dialog.ToolsDialogs.SaftyDialogs.EnumPermissionsSet.StatusAllowsPrinting, dialogs); if (result.IsDiscryptied) { if (result.Password != null) { string filePath = PDFViewer.Document.FilePath; var unlockresult = PDFViewer.Document.UnlockWithPassword(result.Password); } } else { e.Handled = true; } } } //右键菜单,选中字体样式按钮状态 private void PopMenuCheckedFontFamily(AnnotHandlerEventArgs annot) { if (annot != null) { if (annot.EventType == AnnotArgsType.AnnotFreeText) { var freeText = annot as FreeTextAnnotArgs; FreeTextAnnotPopMenu.SetIsChecked(freeText.FontFamily.ToString(), true); } } } //右键菜单,选中文本内容对齐按钮状态 private void PopMenuCheckedTextAglin(AnnotHandlerEventArgs annot) { if (annot != null) { if (annot.EventType == AnnotArgsType.AnnotFreeText) { var freeText = annot as FreeTextAnnotArgs; switch (freeText.Align) { case TextAlignment.Left: FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", true); break; case TextAlignment.Center: FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", true); break; case TextAlignment.Right: FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", true); break; case TextAlignment.Justify: FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", true); break; default: FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", false); FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", false); FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", false); FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", false); break; } } } } //右键菜单,选中虚实线按钮状态 private void PopMenuCheckedLineDash(AnnotHandlerEventArgs annot) { if (annot != null) { bool isSolidLineDash = true; if (annot.EventType == AnnotArgsType.AnnotFreehand) { var freeHand = annot as FreehandAnnotArgs; isSolidLineDash = AnnotTransfer.IsSolidStyle(freeHand.LineDash); FreeHandAnnotPopMenu.SetIsChecked(isSolidLineDash ? "FreeHandSolid" : "FreeHandDash", true); } else if (annot.EventType == AnnotArgsType.AnnotSquare) { var square = annot as SquareAnnotArgs; isSolidLineDash = AnnotTransfer.IsSolidStyle(square.LineDash); ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true); } else if (annot.EventType == AnnotArgsType.AnnotCircle) { var circle = annot as CircleAnnotArgs; isSolidLineDash = AnnotTransfer.IsSolidStyle(circle.LineDash); ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true); } else if (annot.EventType == AnnotArgsType.AnnotLine) { var line = annot as LineAnnotArgs; isSolidLineDash = AnnotTransfer.IsSolidStyle(line.LineDash); ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true); } } } /// /// 内容选择工具 /// /// /// private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e) { SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs; SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs; SnapshotEditMenuViewModel.PDFViewer = PDFViewer; SnapshotEditMenuViewModel.SnapToolEvent -= SnapshotEditMenuViewModel_SnapToolEvent; ; SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent; var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu; if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0) { e.PopupMenu = popMenu; e.Handle = true; if (popMenu != null && popMenu.Items.Count == 5) { //复制 MenuItem menuItem = popMenu.Items[0] as MenuItem; menuItem.CommandTarget = PDFViewer; menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand; //导出 menuItem = popMenu.Items[1] as MenuItem; menuItem.CommandTarget = PDFViewer; if (menuItem.Items.Count == 3) { MenuItem menuItem1 = menuItem.Items[0] as MenuItem; menuItem1.CommandTarget = PDFViewer; menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand; menuItem1 = menuItem.Items[1] as MenuItem; menuItem1.CommandTarget = PDFViewer; menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand; menuItem1 = menuItem.Items[2] as MenuItem; menuItem1.CommandTarget = PDFViewer; menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand; } //裁剪 menuItem = popMenu.Items[2] as MenuItem; menuItem.CommandTarget = PDFViewer; menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand; //缩放至所选区域 menuItem = popMenu.Items[3] as MenuItem; menuItem.CommandTarget = PDFViewer; menuItem.Visibility = Visibility.Collapsed; //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand; //打印 menuItem = popMenu.Items[4] as MenuItem; menuItem.CommandTarget = PDFViewer; menuItem.Command = SnapshotEditMenuViewModel.PrintCommand; } } } private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair e) { switch (e.Key) { case "CloseSnap": { #region to do //var item = PDFViewerTab.SelectedItem as TabItem; //if (item == null) //{ // ClearSelectedToolPanel(); // return; //} //Grid grid = item.Content as Grid; //if (grid == null || grid.Children.Count == 0) //{ // ClearSelectedToolPanel(); // return; //} //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl; //if (pdfViewer == null) //{ // ClearSelectedToolPanel(); // return; //} #endregion to do switch (PDFViewer.MouseMode) { case MouseModes.SelectTextTool: break; default: BtnSelecttoolIsChecked = false; BtnHandIsChecked = true; PDFViewer.EnableZoom(true); PDFViewer.EnableScroll(true); break; } } break; default: break; } } private void PDFViewer_AnnotEditHandler(object sender, List e) { if (e != null && e.Count > 0) { MultipleSelectionAnnot(e); for (int i = 0; i < e.Count; i++) { AnnotEditEvent editEvent = e[i]; switch (editEvent.EditAction) { case ActionType.Add: //BOTA bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); if (viewModel != null) { int pageindex = editEvent.PageIndex; int annotindex = editEvent.AnnotIndex; viewModel.UpdateAddedAnnot(pageindex, annotindex); } } //添加书签注释,用于未保存前跟图章手绘注释的区分 if (StrAnnotToolChecked == "Signature") { AddAnnotSignature(editEvent.EditAnnotArgs); } if (editEvent.EditAnnotArgs.EventType == AnnotArgsType.AnnotFreeText) { (editEvent.EditAnnotArgs as FreeTextAnnotArgs).LineWidth = 0; } break; case ActionType.Del: isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation"); if (isTabItemAnnotation) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); if (viewModel != null) { int pageindex = editEvent.PageIndex; int annotindex = editEvent.AnnotIndex; viewModel.UpdateModifiedAnnot(pageindex, annotindex, true); } } break; case ActionType.Modify: isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation"); if (bOTAContent.TabItemAnnotation.IsSelected) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation); if (viewModel != null) { int pageindex = editEvent.PageIndex; int annotindex = editEvent.AnnotIndex; viewModel.UpdateModifiedAnnot(pageindex, annotindex, false); } } break; case ActionType.TextEdit: break; default: break; } } //空白处 右键菜单 添加注释 为单次添加,不连续添加 if (IsNoSelectMenu) { IsNoSelectMenu = false; PDFViewer.SetMouseMode(MouseModes.PanTool); } } } //复制多个注释,粘贴后,默认注释未选中状态 private void MultipleSelectionAnnot(List e) { bool isAdd = false; Dictionary> selectAnnotDicts = new Dictionary>(); foreach (var item in e) { if (item.EditAction == ActionType.Add /*|| item.EditAction == ActionType.Modify*/) { var annot = item.EditAnnotArgs; if (annot.EventType == AnnotArgsType.AnnotSquare || annot.EventType == AnnotArgsType.AnnotCircle || annot.EventType == AnnotArgsType.AnnotLine || annot.EventType == AnnotArgsType.AnnotStamp || annot.EventType == AnnotArgsType.AnnotFreeText) { if (selectAnnotDicts.ContainsKey(item.PageIndex) == false) { selectAnnotDicts.Add(item.PageIndex, new List()); selectAnnotDicts[item.PageIndex].Add(item.AnnotIndex); } else { selectAnnotDicts[item.PageIndex].Add(item.AnnotIndex); } isAdd = true; } } } if (isAdd) { PDFViewer.ClearSelectAnnots(); PDFViewer.SelectAnnotation(selectAnnotDicts); } } //阅读页悬浮相应 private void PDFViewer_AnnotHoverHandler(object sender, AnnotHoverData e) { if (e != null && e.DrawContext != null) { Rect hoverRect = new Rect( e.PaintRect.Left - 2, e.PaintRect.Top - 2, e.PaintRect.Width + 4, e.PaintRect.Height + 4); Pen hoverPen = new Pen(new SolidColorBrush(Color.FromArgb(0xff, 0x11, 0x8A, 0xff)), 1); hoverPen.DashStyle = DashStyles.Dash; e.DrawContext?.DrawRectangle(null, hoverPen, hoverRect); //便签,显示ToolTip内容 if (e.Annot != null && e.Annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) { PopAnnotToolTip(e.Annot.GetRect(), hoverRect, e.Annot.GetContent()); return; } } CloseAnnotToolTip(); } private void PDFViewer_MouseMove(object sender, MouseEventArgs e) { if (sender != null && PDFViewer.ToolTip != null && PDFViewer.Parent != null) { //鼠标移到注释之外,关闭ToolTip浮窗 var newPoint = e.GetPosition(PDFViewer.Parent as ContentControl); var isOutw = newPoint.X > (oldRect.X + oldRect.Width + 4); var isOuth = newPoint.Y > (oldRect.Y + oldRect.Height + 4); if (newPoint.X < oldRect.X || newPoint.Y < oldRect.Y || isOutw || isOuth) { CloseAnnotToolTip(); } } } private Rect oldRect = new Rect(0, 0, 0, 0); private void PopAnnotToolTip(CRect placementRect, Rect hoverRect, string content) { //便签,显示ToolTip内容 if (PDFViewer.ToolTip == null) { if (string.IsNullOrEmpty(content) == false) { ToolTip TipChild = new ToolTip(); TipChild.Style = App.Current.Resources["FlowToolTip"] as Style; TipChild.MaxWidth = 246; TipChild.Content = content; TipChild.Visibility = Visibility.Visible; TipChild.IsOpen = true; TipChild.Placement = PlacementMode.Right; TipChild.PlacementRectangle = new Rect(placementRect.left, placementRect.top, placementRect.right, placementRect.bottom); TipChild.PlacementTarget = PDFViewer.Parent as ContentControl; TipChild.Placement = PlacementMode.MousePoint; PDFViewer.ToolTip = TipChild; oldRect = hoverRect; PDFViewer.MouseMove -= PDFViewer_MouseMove; PDFViewer.MouseMove += PDFViewer_MouseMove; } } } //关闭ToolTip private void CloseAnnotToolTip() { if (PDFViewer.ToolTip != null && PDFViewer.ToolTip is ToolTip) { ToolTip oldTips = (ToolTip)PDFViewer.ToolTip; oldTips.IsOpen = false; oldTips.Visibility = Visibility.Collapsed; PDFViewer.ToolTip = null; PDFViewer.MouseMove -= PDFViewer_MouseMove; oldRect = new Rect(0, 0, 0, 0); } } #endregion PDFViewer事件 #region BindingEvent事件 //同一属性面板,不同的注释类型 private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary e) { if (e != null) { AnnotHandlerEventArgs annotArgs = null; propertyPanel.SaveLastAnnot(); foreach (AnnotArgsType argsType in e.Keys) { if (propertyPanel.annot.EventType != argsType) { PDFViewer.SetMouseMode(MouseModes.PanTool); } switch (argsType) { case AnnotArgsType.AnnotSquare: annotArgs = GetRect(); break; case AnnotArgsType.AnnotCircle: annotArgs = GetCircle(); break; case AnnotArgsType.AnnotLine: var LineTag = e[argsType] as string; annotArgs = GetArrowLine(LineTag); break; } ShowPropertyPanel(); if (string.IsNullOrEmpty(StrAnnotToolChecked) || StrAnnotToolChecked != "Rect") { PDFViewer.SetMouseMode(MouseModes.PanTool); return; } if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); } } } } //来自属性面板的事件,更改颜色等属性而触发 private void AnnotPropertyPanel_DataChanged(object sender, Dictionary e) { if (e != null) { foreach (AnnotArgsType argsType in e.Keys) { switch (argsType) { case AnnotArgsType.AnnotHighlight: if (e[argsType] is Color) { HighLightColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { HighLightOpacity = (double)e[argsType]; } //创建注释后,修改注释属性,可用于下次创建的注释 if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate) { PDFViewer.SetToolParam(propertyPanel.annot); } break; case AnnotArgsType.AnnotUnderline: if (e[argsType] is Color) { UnderLineColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { underLineOpacity = (double)e[argsType]; } //创建注释后,修改注释属性,可用于下次创建的注释 if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate) { PDFViewer.SetToolParam(propertyPanel.annot); } break; case AnnotArgsType.AnnotSquiggly: if (e[argsType] is Color) { SquigglyColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { SquigglyOpacity = (double)e[argsType]; } //创建注释后,修改注释属性,可用于下次创建的注释 if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate) { PDFViewer.SetToolParam(propertyPanel.annot); } break; case AnnotArgsType.AnnotStrikeout: if (e[argsType] is Color) { StrikeoutColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { StrikeoutOpacity = (double)e[argsType]; } //创建注释后,修改注释属性,可用于下次创建的注释 if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate) { PDFViewer.SetToolParam(propertyPanel.annot); } break; case AnnotArgsType.AnnotFreehand: if (e[argsType] is FreehandAnnotArgs) { var annot = e[argsType] as FreehandAnnotArgs; if (annot != null) { if (propertyPanel != null) { //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值 var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib()); propertyPanel.AnnotEvent = AnnotEvent; var AnnotEvents = new List(); AnnotEvents.Add(AnnotEvent); propertyPanel.AnnotEvents = AnnotEvents; propertyPanel.annot = annot; //手绘注释工具按钮的属性 // FreehandPath.Opacity = annot.Transparency; // FreehandPath.Fill = new SolidColorBrush(annot.InkColor); } PDFViewer.SetToolParam(annot); } } break; case AnnotArgsType.AnnotErase: if (e[argsType] is ToggleButton) { ToggleButton clickBtn = e[argsType] as ToggleButton; if (clickBtn.IsChecked == true) { if (clickBtn.Tag.ToString() == "PenBtn") { CustomIconToggleBtn btn = new CustomIconToggleBtn(); btn.Tag = "Freehand"; btn.IsChecked = true; BtnMyTools_Click(btn); break; } CreateEraseArgs(); } } break; case AnnotArgsType.AnnotSquare: case AnnotArgsType.AnnotCircle: case AnnotArgsType.AnnotLine: case AnnotArgsType.AnnotFreeText: //创建注释后,修改注释属性,可用于下次创建的注释 if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate) { PDFViewer.SetToolParam(propertyPanel.annot); } break; } } } } private void CreateEraseArgs() { EraseArgs eraseArgs = new EraseArgs(); eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00); eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00); eraseArgs.Thickness = 10; DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase); if (annotProperty != null) { eraseArgs.Thickness = annotProperty.Thickness; } PDFViewer.ClearSelectAnnots(false); PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(eraseArgs); List eraseArgsList = new List(); if (eraseArgs != null) eraseArgsList.Add(eraseArgs); AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgsList); } #endregion BindingEvent事件 } }