using ComPDFKit.PDFAnnotation; using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Office.CustomControl; using PDF_Office.EventAggregators; using PDF_Office.Helper; using PDF_Office.Properties; using PDF_Office.ViewModels.BOTA; using PDF_Office.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; namespace PDF_Office.ViewModels.Tools { public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware { #region 事件绑定和解绑 private void BindingEvent() { //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值 //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色 propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged; propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged; propertyPanel.DefaultStored -= AnnotProperty_DefaultStored; propertyPanel.DefaultStored += AnnotProperty_DefaultStored; propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged; propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged; } private void UnBindingEvent() { propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged; propertyPanel.DefaultStored -= AnnotProperty_DefaultStored; propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged; } 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.WidgetClickHander -= PDFViewer_WidgetClickHander; PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander; PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler; PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler; } } private void UnBindingPDFViewerHandler() { if (PDFViewer != null) { PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler; PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler; PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander; PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler; } } #endregion 事件绑定和解绑 #region PDFViewer事件 //选中和非选中注释,右键菜单 private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e) { if (e != null) { var annot = e.AnnotItemsList[0]; if (annot != null) { //IsAnnotCreateReset:是否为创建注释的状态 if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == false) { switch (annot.EventType) { case AnnotArgsType.AnnotHighlight: e.IsAnnotCreateReset = false; GetHighLight(annot as TextHighlightAnnotArgs); break; case AnnotArgsType.AnnotUnderline: GetUnderLine(annot as TextUnderlineAnnotArgs); break; case AnnotArgsType.AnnotStrikeout: GetStrikeout(annot as TextStrikeoutAnnotArgs); break; case AnnotArgsType.AnnotSquiggly: GetSquiggly(annot as TextSquigglyAnnotArgs); break; case AnnotArgsType.AnnotFreehand: GetFreehand(annot as FreehandAnnotArgs); break; case AnnotArgsType.AnnotFreeText: GetFreetext(annot as FreeTextAnnotArgs); break; case AnnotArgsType.AnnotSquare: GetRect(annot as SquareAnnotArgs); break; case AnnotArgsType.AnnotCircle: GetCircle(annot as CircleAnnotArgs); 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", annot as LineAnnotArgs); else GetArrowLine("Arrow", annot as LineAnnotArgs); break; case AnnotArgsType.AnnotLink: //viewContentViewModel.IsCreateLink = false; GetLink(annot as LinkAnnotArgs, e); break; case AnnotArgsType.AnnotSticky: GetStickyNote(annot as StickyAnnotArgs); break; } //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值 PDFViewer.SetToolParam(annot); } else if (e.AnnotItemsList.Count == 1 && e.IsAnnotCreateReset == true) { switch (annot.EventType) { case AnnotArgsType.AnnotLink: //viewContentViewModel.IsCreateLink = false; GetLink(annot as LinkAnnotArgs, e); break; } PDFViewer.SetToolParam(annot); //TODO: 设计已重新调整为:修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉 //if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType)) //{ // foreach (var item in ToolExpandDict) // { // if (item.Value == e.AnnotItemsList[0].EventType) // { // FindAnnotTypeKey(item.Key, ref annot); // break; // } // } //} //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏 if (isRightMenuAddAnnot) { ShowPropertyPanel(false); } else { ShowPropertyPanel(); } } } } else { viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } } //在注释工具的状态下,右键菜单 private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e) { if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool)) return; 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 { e.PopupMenu = SelectAnnotContextMenu(sender); } if (e.PopupMenu != null) { e.Handle = true; } } else { if (e.PressOnSelectedText) { e.PopupMenu = NoneSelectAnnotContextMenu(sender, e); if (e.PopupMenu != null) { e.Handle = true; } } else { e.PopupMenu = ViewerContextMenu(); if (e.PopupMenu != null) { e.Handle = true; } } } break; } } private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e) { SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs; SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs; SnapshotEditMenuViewModel.PDFViewer = PDFViewer; 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; break; } } break; default: break; } } private void PDFViewer_AnnotEditHandler(object sender, List e) { if (e != null && e.Count > 0) { for (int i = 0; i < e.Count; i++) { AnnotEditEvent editEvent = e[i]; switch (editEvent.EditAction) { case ActionType.Add: BOTAContentViewModel bOTAContentViewModel = null; BOTAContent bOTAContent = null; bool isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation"); if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel); if (viewModel != null) { int pageindex = editEvent.PageIndex; int annotindex = editEvent.AnnotIndex; viewModel.UpdateAddedAnnot(pageindex, annotindex); } } break; case ActionType.Del: isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation"); if (isTabItemAnnotation) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel); 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 (isTabItemAnnotation) { AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel); if (viewModel != null) { int pageindex = editEvent.PageIndex; int annotindex = editEvent.AnnotIndex; viewModel.UpdateModifiedAnnot(pageindex, annotindex, false); } } break; case ActionType.TextEdit: break; default: break; } } } } private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e) { } #endregion PDFViewer事件 #region BindingEvent事件 private void AnnotProperty_DefaultStored(object sender, object e) { } private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary e) { if (e != null) { AnnotHandlerEventArgs annotArgs = null; foreach (AnnotArgsType argsType in e.Keys) { 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; } if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); } ShowPropertyPanel(); } } } 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]; } break; case AnnotArgsType.AnnotUnderline: if (e[argsType] is Color) { UnderLineColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { underLineOpacity = (double)e[argsType]; } break; case AnnotArgsType.AnnotSquiggly: if (e[argsType] is Color) { SquigglyColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { SquigglyOpacity = (double)e[argsType]; } break; case AnnotArgsType.AnnotStrikeout: if (e[argsType] is Color) { StrikeoutColor = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { StrikeoutOpacity = (double)e[argsType]; } break; case AnnotArgsType.AnnotFreehand: if (e[argsType] is Color) { // FreehandPath.Fill = new SolidColorBrush((Color)e[argsType]); } if (e[argsType] is double) { // FreehandPath.Opacity = (double)e[argsType]; } 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; } 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); Dictionary annotAttribsList = new Dictionary(); annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor; annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor; annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness; AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList); } } break; } } } } #endregion BindingEvent事件 } }