123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- using ComPDFKit.PDFAnnotation;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Office.CustomControl;
- using PDF_Office.Helper;
- using PDF_Office.Model;
- using PDF_Office.Properties;
- using PDF_Office.ViewModels.PropertyPanel;
- using PDF_Office.Views.PropertyPanel.AnnotPanel;
- using PDFSettings;
- 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 System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace PDF_Office.ViewModels.Tools
- {
- public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
- {
- public AnnotToolContentViewModel(IRegionManager regionManager)
- {
- region = regionManager;
- MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
- PropertyRegionName = Guid.NewGuid().ToString();
- BindingEvent();
- InitDefaultValue();
- }
- private Dictionary<string, bool> ToolExpandDict = new Dictionary<string, bool>();
- public void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
- {
- //不创建注释,属于注释模板
- bool isTemplateAnnot = false;
- AnnotHandlerEventArgs annotArgs = null;
- if (annotBtn.IsChecked == true)
- {
- switch (annotBtn.Tag.ToString())
- {
- case "SnapshotEdit":
- break;
- case "HighLight":
- annotArgs = GetHighLight();
- break;
- case "UnderLine":
- annotArgs = GetUnderLine();
- break;
- case "Squiggly":
- annotArgs = GetSquiggly();
- break;
- case "Strikeout":
- annotArgs = GetStrikeout();
- break;
- case "Freehand":
- annotArgs = GetFreehand();
- break;
- case "Freetext":
- annotArgs = GetFreetext();
- break;
- case "StickyNote":
- annotArgs = GetStickyNote();
- break;
- case "Rect":
- annotArgs = GetRect();
- break;
- case "Circle":
- annotArgs = GetCircle();
- break;
- case "Arrow":
- case "Line":
- annotArgs = GetArrowLine(annotBtn.Tag.ToString());
- break;
- case "Stamp":
- annotArgs = GetStamp();
- isTemplateAnnot = true;
- break;
- case "Image":
- annotArgs = GetImage(annotBtn);
- break;
- case "Signature":
- annotArgs = GetSignature();
- isTemplateAnnot = true;
- break;
- case "Link":
- break;
- }
- if (annotArgs != null)
- {
- annotArgs.Author = Settings.Default.AppProperties.Description.Author;
- PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- PDFViewer.SetToolParam(annotArgs);
- }
- }
- //当不是注释模板,且无创建注释时,属性面板显示为空内容
- if(isTemplateAnnot == false && annotArgs == null)
- {
- PDFViewer.SetMouseMode(MouseModes.PanTool);
- viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
- }
- ShowPropertyPanel((bool)annotBtn.IsChecked);
- }
- private void AnnotProperty_DefaultStored(object sender, object e)
- {
- }
- private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> 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<AnnotArgsType, object> 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;
- //PDFViewerCtrl viewCtrl = GetCurrentViewer();
- // ClearSelectedToolPanel(BtnFreeHand);
- // if (viewCtrl != null && viewCtrl.PdfViewer != null)
- {
- 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<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
- annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
- annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
- annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
- AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgs, annotAttribsList);
- //清空选中注释
- //viewCtrl.PdfViewer.ClearSelectAnnots(false);
- //viewCtrl.PdfViewer.SetMouseMode(MouseModes.AnnotCreate);
- //viewCtrl.PdfViewer.SetToolParam(eraseArgs);
- //if (viewCtrl.PropPanel.Children.Count > 0)
- //{
- // FreehandAnnotProperty freehandPropPanel = viewCtrl.PropPanel.Children[0] as FreehandAnnotProperty;
- // if (freehandPropPanel == null)
- // {
- // viewCtrl.PropPanel.Children.Clear();
- // freehandPropPanel = new FreehandAnnotProperty();
- // freehandPropPanel.EraseSet = true;
- // freehandPropPanel.SetInkThickness(eraseArgs.Thickness);
- // freehandPropPanel.DataChanged += AnnotPropertyPanel_DataChanged;
- // viewCtrl.PropPanel.Children.Add(freehandPropPanel);
- // }
- // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
- // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
- // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
- // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
- // freehandPropPanel.AnnotEvent = null;
- // freehandPropPanel.SetEraseMode(true);
- // freehandPropPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
- //}
- //else
- //{
- // FreehandAnnotProperty freehandProperty = new FreehandAnnotProperty();
- // freehandProperty.DataChanged += AnnotPropertyPanel_DataChanged;
- // Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
- // annotAttribsList[AnnotAttrib.Color] = eraseArgs.UIBorderColor;
- // annotAttribsList[AnnotAttrib.FillColor] = eraseArgs.UIFillColor;
- // annotAttribsList[AnnotAttrib.Thickness] = eraseArgs.Thickness;
- // freehandProperty.SetInkThickness(eraseArgs.Thickness);
- // freehandProperty.EraseSet = true;
- // freehandProperty.SetEraseMode(true);
- // freehandProperty.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(eraseArgs, annotAttribsList);
- // viewCtrl.AddAnnotPropertyPanel(freehandProperty);
- //}
- }
- else
- {
- // BtnFreeHand.IsChecked = true;
- // BtnTool_Click(BtnFreeHand, new RoutedEventArgs());
- }
- }
- }
- break;
- }
- }
- }
- }
- public IRegionManager region;
- public string PropertyRegionName { get; set; }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- // navigationContext.Parameters.TryGetValue<PropertyPanelContentViewModel>(ParameterNames.PropertyPanelContentViewModel, out propertyPanelContentViewModel);
- 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;
- }
- }
- private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
- {
- }
- private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
- {
- }
- private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
- {
- if (e != null)
- {
- var annot = e.AnnotItemsList[0];
- if (annot != null)
- {
- switch (annot.EventType)
- {
- case AnnotArgsType.AnnotHighlight:
- 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;
- }
- ShowPropertyPanel();
- }
- }
- else
- {
- // PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
- // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
- }
- }
- private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
- {
- if (e.AnnotEventArgsList == null)
- return;
- // var annotlist = e.AnnotEventArgsList;
- switch (e.CommandType)
- {
- case CommandType.Context:
- if (e.AnnotEventArgsList.Count > 0)
- {
- e.PopupMenu = SelectAnnotContextMenu(sender);
- if (e.PopupMenu != null)
- {
- e.Handle = true;
- }
- }
- else
- {
- e.PopupMenu = NoneSelectAnnotContextMenu(sender);
- if (e.PopupMenu != null)
- {
- e.Handle = true;
- }
- }
- break;
- }
- }
- private ContextMenu SelectAnnotContextMenu(object sender)
- {
- var popMenu = App.Current.FindResource("SelectAnnotContextMenu") as ContextMenu;
- if (popMenu != null && popMenu.Items.Count == 4)
- {
- //剪切
- MenuItem menuItem = popMenu.Items[0] as MenuItem;
- menuItem.CommandTarget = (UIElement)sender;
- menuItem.Command = ApplicationCommands.Cut;
- //复制
- menuItem = popMenu.Items[1] as MenuItem;
- menuItem.CommandTarget = (UIElement)sender;
- menuItem.Command = ApplicationCommands.Copy;
- //删除
- menuItem = popMenu.Items[2] as MenuItem;
- menuItem.CommandTarget = (UIElement)sender;
- menuItem.Command = ApplicationCommands.Delete;
- //笔记
- //menuItem = popMenu.Items[2] as MenuItem;
- //menuItem.CommandTarget = (UIElement)sender;
- //menuItem.Command = ApplicationCommands.no;
- }
- return popMenu;
- }
- private ContextMenu NoneSelectAnnotContextMenu(object sender)
- {
- var popMenu = new ContextMenu();
- MenuItem menuItem = new MenuItem();
- menuItem.CommandTarget = (UIElement)sender;
- menuItem.Command = ApplicationCommands.Paste;
- popMenu.Items.Add(menuItem);
- menuItem = new MenuItem();
- menuItem.CommandTarget = (UIElement)sender;
- menuItem.Command = ApplicationCommands.Copy;
- popMenu.Items.Add(menuItem);
- return popMenu;
- }
- /// <summary>
- /// 展开显示属性面板
- /// </summary>
- private void ShowPropertyPanel(bool show = true)
- {
- viewContentViewModel.IsPropertyOpen = show;
- }
- }
- }
|