using ComPDFKitViewer.AnnotEvent; using ComPDFKitViewer.PdfViewer; using PDF_Office.Model; using Prism.Commands; 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 PDF_Office.ViewModels.Tools; using ComPDFKitViewer; using PDF_Office.Helper; using PDF_Office.Properties; using PDFSettings; using System.Windows.Media; using ComPDFKit.PDFDocument.Action; using ComPDFKit.PDFDocument; using System.Diagnostics; using ComPDFKit.PDFAnnotation; using PDF_Office.ViewModels.BOTA; using PDF_Office.Views.BOTA; using System.Windows.Forms; using Control = System.Windows.Controls.Control; using Microsoft.Office.Interop.Word; using Point = System.Windows.Point; namespace PDF_Office.ViewModels.FillAndSign { public class FillAndSignContentViewModel : BindableBase, INavigationAware { #region private CPDFViewer PDFViewer; private AnnotPropertyPanel propertyPanel = new AnnotPropertyPanel(); private ViewContentViewModel viewContentViewModel; private bool isRightMenuAddAnnot = false; private List> ShapePoints = new List>(); private string Shape = "HookShape"; private IRegionManager regions { get; set; } private Dictionary ToolExpandDict = new Dictionary(); public Brush SelectColor=new SolidColorBrush(Colors.GreenYellow); public double FillOpacity = 1; public double LineWidth = 1; #endregion #region Command public DelegateCommand CheckCommand { get; set; } /// /// 按钮名称和属性面板映射字典 /// public Dictionary btnToProperty = new Dictionary(); #endregion public FillAndSignContentViewModel(IRegionManager regionManager) { regions = regionManager; CheckCommand = new DelegateCommand(CheckedEvent); ToolExpandDict.Add("Freetext", AnnotArgsType.AnnotFreeText); InitDictionary(); } private void CheckedEvent(RoutedEventArgs e) { var control = e.OriginalSource as Control; //NavigateToProperty(control.Name); //不创建注释,属于注释模板 bool isTemplateAnnot = false; bool isSnapshotEdit = false; AnnotHandlerEventArgs annotArgs = null; var tag = control.Name; FindAnnotTypeKey(control.Name, ref annotArgs); if (control.Name == "RbtnSign") { isTemplateAnnot = true; } if (tag == "SnapshotEdit") { isSnapshotEdit = true; } else if (tag == "Signature" || tag == "Stamp") { isTemplateAnnot = true; } if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); isRightMenuAddAnnot = false; } //当不是注释模板,且无创建注释时,属性面板显示为空内容 if (isTemplateAnnot == false && annotArgs == null) { PDFViewer.SetMouseMode(MouseModes.PanTool); viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } else { if (isSnapshotEdit == true) { ShowPropertyPanel(false); isSnapshotEdit = false; } else { ShowPropertyPanel(true); } } } //注释工具 private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false) { switch (tag) { case "RbtnDate": case "RbtnText"://文本 annotArgs = GetFreetext(); break; case "Freetext"://文本 annotArgs = GetFreetext(); break; case "RbtnSign"://签名 annotArgs = GetSignature(); PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释 break; case "RbtnTick"://勾 Shape = "HookShape"; ShapePoints =new List> { new List { new Point(0.599976,7.0286), new Point(5.57775,11.8), new Point(13.4,1.40002)} }; annotArgs = GetStamp(); break; case "RbtnFork"://叉 Shape = "ForkShape"; ShapePoints = new List> { new List { new Point(3.19995, 3.20001), new Point(12.8, 12.8) } ,new List { new Point(12.8, 3.20001), new Point(3.20005, 12.8) }}; annotArgs = GetStamp(); break; case "RbtnRectangle"://矩形 Shape = "RectShape"; ShapePoints = new List> { new List { new Point(0, 5), new Point(28, 5),new Point(28,27),new Point(0,27) , new Point(0, 5) } }; annotArgs = GetStamp(); break; case "RbtnLine"://下划线 Shape = "LineShape"; ShapePoints = new List> { new List { new Point(12, 10), new Point(36, 10) } }; annotArgs = GetStamp(); break; case "RbtnPoint": Shape = "DotShape"; ShapePoints = new List> { new List { new Point(0, 0), new Point(2, 2) }, new List { new Point(2, 0), new Point(0,2 ) } }; annotArgs = GetStamp(); break; default://图章 break; } } /// /// 签名 /// /// private AnnotHandlerEventArgs GetSignature() { AddToPropertyPanel("SignatureAnnotProperty"); return null; } /// /// 图章 /// /// private AnnotHandlerEventArgs GetStamp() { StampAnnotArgs stampAnnotArgs = new StampAnnotArgs(); stampAnnotArgs.SetInkData(ShapePoints, LineWidth, (SelectColor as SolidColorBrush).Color); //PDFViewer.SetMouseMode(MouseModes.AnnotCreate); //PDFViewer.SetToolParam(stampArgs); //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs(); //stampAnnotArgs.Opacity = 1; //stampAnnotArgs.StampText = "APPROVED"; //stampAnnotArgs.Type = StampType.STANDARD_STAMP; // stampAnnotArgs.Type = StampType.STANDARD_STAMP; Dictionary annotAttribsList = new Dictionary(); annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity; DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp); List stampAnnotArgsList = new List(); if (stampAnnotArgs != null) stampAnnotArgsList.Add(stampAnnotArgs); AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList); return stampAnnotArgs; } public void SetStamp() { StampAnnotArgs stampAnnotArgs = new StampAnnotArgs(); stampAnnotArgs.SetInkData(ShapePoints, LineWidth, (SelectColor as SolidColorBrush).Color); //PDFViewer.SetMouseMode(MouseModes.AnnotCreate); //PDFViewer.SetToolParam(stampArgs); //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs(); //stampAnnotArgs.Opacity = 1; //stampAnnotArgs.StampText = "APPROVED"; //stampAnnotArgs.Type = StampType.STANDARD_STAMP; stampAnnotArgs.Opacity = FillOpacity; Dictionary annotAttribsList = new Dictionary(); annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity; DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp); List stampAnnotArgsList = new List(); if (stampAnnotArgs != null) stampAnnotArgsList.Add(stampAnnotArgs); AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList); bool isTemplateAnnot = false; bool isSnapshotEdit = false; AnnotHandlerEventArgs annotArgs = stampAnnotArgs; if (annotArgs != null) { annotArgs.Author = Settings.Default.AppProperties.Description.Author; PDFViewer.SetMouseMode(MouseModes.AnnotCreate); PDFViewer.SetToolParam(annotArgs); isRightMenuAddAnnot = false; } //当不是注释模板,且无创建注释时,属性面板显示为空内容 if (isTemplateAnnot == false && annotArgs == null) { PDFViewer.SetMouseMode(MouseModes.PanTool); viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); } else { if (isSnapshotEdit == true) { ShowPropertyPanel(false); isSnapshotEdit = false; } else { ShowPropertyPanel(true); } } } /// /// 文本 /// /// /// private AnnotHandlerEventArgs GetFreetext(List selectedArgs = null) { Dictionary annotAttribsList = new Dictionary(); FreeTextAnnotArgs freetextArgs = null; TextAlignment textAlignment; if (selectedArgs == null || selectedArgs.Count == 0) { freetextArgs = new FreeTextAnnotArgs(); freetextArgs.Align = TextAlignment.Left; freetextArgs.BgColor = Colors.Transparent; freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily); freetextArgs.FontColor = Colors.Black; freetextArgs.FontSize = 14; freetextArgs.Transparency = 1; freetextArgs.LineColor = Colors.Black; freetextArgs.LineWidth = 0; freetextArgs.TextContent = string.Empty; DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText); if (annotProperty != null) { freetextArgs.Align = annotProperty.TextAlign; freetextArgs.BgColor = annotProperty.BackgroundColor; freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily); freetextArgs.FontColor = annotProperty.ForgoundColor; freetextArgs.FontSize = annotProperty.FontSize; freetextArgs.Transparency = annotProperty.Opacity; freetextArgs.LineColor = annotProperty.BorderColor; freetextArgs.LineWidth = annotProperty.Thickness; freetextArgs.TextContent = annotProperty.NoteText; freetextArgs.FontWeight = annotProperty.FontWeight; freetextArgs.FontStyle = annotProperty.FontStyle; } int align = (int)Settings.Default.AppProperties.Annotate.TextAlign; if (align == 0) textAlignment = TextAlignment.Left; else if (align == 1) textAlignment = TextAlignment.Center; else textAlignment = TextAlignment.Right; if (freetextArgs != null) { selectedArgs = new List(); selectedArgs.Add(freetextArgs); } } else { freetextArgs = selectedArgs[0] as FreeTextAnnotArgs; textAlignment = freetextArgs.Align; } annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor; annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor; annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth; annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency; annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor; annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize; annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily; annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle; annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight; annotAttribsList[AnnotAttrib.TextAlign] = textAlignment; annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent; AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList); return freetextArgs; } /// /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线 /// /// 对应的注释面板 /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线 /// 注释 /// 更改注释属性的键值对,更改值后会自动记录undoRedo容器里 private void AddToPropertyPanel(string viewContent, string toolTag = null, List annots = null, Dictionary annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null,bool isUpData=false) { if (annots != null) { propertyPanel.annotlists = annots; propertyPanel.annot = annots[0]; } else { propertyPanel.annotlists = null; propertyPanel.annot = null; } if (annotAttribsList != null) { if (annots.Count > 1) { if (propertyPanel.AnnotEvents == null) propertyPanel.AnnotEvents = new List(); propertyPanel.AnnotEvents.Clear(); foreach (var itemAnnot in annots) { var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList); propertyPanel.AnnotEvents.Add(eventitem); } } propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList); if (annots[0] is LinkAnnotArgs && annotAttribEvent != null) { propertyPanel.AnnotEvent = annotAttribEvent; } } if (string.IsNullOrEmpty(viewContent) == false) { if (isUpData) { return; } if (viewContent == "ShapFillProperty") { NavigateToProperty(viewContent, propertyPanel); return; } viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel); } } private void NavigateToProperty(string btnName, AnnotPropertyPanel annotPropertyPanel) { NavigationParameters values = new NavigationParameters(); values.Add(ParameterNames.PDFViewer, PDFViewer); values.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel); values.Add("Shape", Shape); values.Add("FillAndSignContentViewModel", this); regions.RequestNavigate(RegionNames.PropertyRegionName,btnName,values); viewContentViewModel.IsPropertyOpen = true; } /// /// 初始化按钮名称-属性面板字典 /// private void InitDictionary() { btnToProperty["RbtnText"] = "TextFillProperty"; btnToProperty["RbtnTick"] = "ShapFillProperty"; btnToProperty["RbtnFork"] = "ShapFillProperty"; btnToProperty["RbtnRectangle"] = "ShapFillProperty"; btnToProperty["RbtnLine"] = "ShapFillProperty"; btnToProperty["RbtnPoint"] = "ShapFillProperty"; btnToProperty["RbtnDate"] = "DateFillProperty"; btnToProperty["RbtnSign"] = ""; } private void BindingPDFViewerHandler() { //来自PDFViewer的响应事件 if (PDFViewer != null) { //PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler; //PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler; } } private void UnBindingPDFViewerHandler() { if (PDFViewer != null) { /*PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler; */} } //选中和非选中注释,右键菜单 //private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e) //{ // if (e != null) // { // var annot = e.AnnotItemsList[0]; // if (annot != null) // { // if (e.AnnotItemsList.Count == 1) // { // //IsAnnotCreateReset:是否为创建注释的状态 // if (e.IsAnnotCreateReset == false) // { // GetSelectedAnnots(e); // //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值 // PDFViewer.SetToolParam(annot); // } // else // { // //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)) // { // foreach (var item in ToolExpandDict) // { // if (item.Value == e.AnnotItemsList[0].EventType) // { // annot = null;//新建注释时,回到默认值 // FindAnnotTypeKey(item.Key, ref annot); // break; // } // } // } // } // //else // //PDFViewer.SetToolParam(annot); // //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏 // if (isRightMenuAddAnnot) // { // ShowPropertyPanel(false); // } // else // { // ShowPropertyPanel(); // } // } // } // else // { // bool isDifferentAnnotTyle = false; // var lastAnnot = annot; // foreach (var item in e.AnnotItemsList) // { // if (lastAnnot.EventType != item.EventType) // { // if (isShapAnnot(annot) == true && isShapAnnot(item) == true) // { // lastAnnot = item; // continue; // } // lastAnnot = item; // isDifferentAnnotTyle = true; // break; // } // } // if (isDifferentAnnotTyle) // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); // else // GetSelectedAnnots(e); // } // } // } // else // { // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null); // } //} //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: // // bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent 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 (bOTAContent.TabItemAnnotation.IsSelected) // { // 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 bool isShapAnnot(AnnotHandlerEventArgs annot) { if (annot.EventType == AnnotArgsType.AnnotCircle || annot.EventType == AnnotArgsType.AnnotSquare || annot.EventType == AnnotArgsType.AnnotLine ) { return true; } else { return false; } } private void GetSelectedAnnots(AnnotAttribEvent e) { var annot = e.AnnotItemsList[0]; switch (annot.EventType) { case AnnotArgsType.AnnotFreeText: GetFreetext(e.AnnotItemsList); break; } } private void ShowPropertyPanel(bool show = true) { viewContentViewModel.IsPropertyOpen = show; } #region Navigation public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { UnBindingPDFViewerHandler(); } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.ViewContentViewModel, out viewContentViewModel); navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); BindingPDFViewerHandler(); } #endregion } }