using ComPDFKit.PDFAnnotation; using ComPDFKitViewer.AnnotEvent; using Dropbox.Api.Users; using DryIoc; using ImTools; using Microsoft.Office.Interop.Word; using PDF_Master.CustomControl; using PDF_Master.CustomControl.CompositeControl; using PDF_Master.EventAggregators; using PDF_Master.Helper; using PDF_Master.Model; using PDF_Master.Model.BOTA; using Prism.Commands; using Prism.Common; using Prism.Events; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Drawing; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Annotations; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Forms; using System.Windows.Media; using DialogResult = Prism.Services.Dialogs.DialogResult; using ListBox = System.Windows.Controls.ListBox; namespace PDF_Master.ViewModels.Dialog.BOTA { internal class ScreenAnnotationDialogViewModel : BindableBase, IDialogAware { #region 文案 private string T_style; public string T_Style { get { return T_style; } set { SetProperty(ref T_style, value); } } private string T_color; public string T_Color { get { return T_color; } set { SetProperty(ref T_color, value); } } private string T_author; public string T_Author { get { return T_author; } set { SetProperty(ref T_author, value); } } private string T_clear; public string T_Clear { get { return T_clear; } set { SetProperty(ref T_clear, value); } } private string T_apply; public string T_Apply { get { return T_apply; } set { SetProperty(ref T_apply, value); } } private string T_cancel; public string T_Cancel { get { return T_cancel; } set { SetProperty(ref T_cancel, value); } } private void InitString() { T_Style = App.MainPageLoader.GetString("Annotation_DialogStyle"); T_Color = App.MainPageLoader.GetString("Annotation_DialogColor"); T_Author = App.MainPageLoader.GetString("Annotation_DialogAuthor"); T_Clear = App.MainPageLoader.GetString("Annotation_DialogClear"); T_Apply = App.MainPageLoader.GetString("Annotation_DialogYes"); T_Cancel = App.MainPageLoader.GetString("Annotation_DialogNo"); } #endregion 文案 public string Title => ""; public event Action RequestClose; #region 类型的显示隐藏 /// /// 高亮字体 /// private Visibility highlightVisibility = Visibility.Collapsed; public Visibility HighlightVisibility { get { return highlightVisibility; } set { SetProperty(ref highlightVisibility, value); } } /// /// 手绘 /// private Visibility freeHandVisibility = Visibility.Collapsed; public Visibility FreeHandVisibility { get { return freeHandVisibility; } set { SetProperty(ref freeHandVisibility, value); } } /// /// 便签 /// private Visibility annotStickyVisibility = Visibility.Collapsed; public Visibility AnnotStickyVisibility { get { return annotStickyVisibility; } set { SetProperty(ref annotStickyVisibility, value); } } /// /// 图章 /// private Visibility annotStampVisibility = Visibility.Collapsed; public Visibility AnnotStampVisibility { get { return annotStampVisibility; } set { SetProperty(ref annotStampVisibility, value); } } /// /// 线 /// private Visibility sharpLineVisibility = Visibility.Collapsed; public Visibility SharpLineVisibility { get { return sharpLineVisibility; } set { SetProperty(ref sharpLineVisibility, value); } } /// /// 箭头 /// private Visibility sharpArrowVisibility = Visibility.Collapsed; public Visibility SharpArrowVisibility { get { return sharpArrowVisibility; } set { SetProperty(ref sharpArrowVisibility, value); } } /// /// 圆 /// private Visibility annotCircleVisibility = Visibility.Collapsed; public Visibility AnnotCircleVisibility { get { return annotCircleVisibility; } set { SetProperty(ref annotCircleVisibility, value); } } /// /// 链接 /// private Visibility annotLinkVisible = Visibility.Collapsed; public Visibility AnnotLinkVisible { get { return annotLinkVisible; } set { SetProperty(ref annotLinkVisible, value); } } /// /// 矩形 /// private Visibility annotSquareVisibility = Visibility.Collapsed; public Visibility AnnotSquareVisibility { get { return annotSquareVisibility; } set { SetProperty(ref annotSquareVisibility, value); } } /// /// 文本注释 /// private Visibility annotFreeTextVisibility = Visibility.Collapsed; public Visibility AnnotFreeTextVisibility { get { return annotFreeTextVisibility; } set { SetProperty(ref annotFreeTextVisibility, value); } } /// /// 删除线 /// private Visibility annotStrikeoutVisibility = Visibility.Collapsed; public Visibility AnnotStrikeoutVisibility { get { return annotStrikeoutVisibility; } set { SetProperty(ref annotStrikeoutVisibility, value); } } /// /// 下划线 /// private Visibility underLineVisibility = Visibility.Collapsed; public Visibility UnderLineVisibility { get { return underLineVisibility; } set { SetProperty(ref underLineVisibility, value); } } #endregion 类型的显示隐藏 public DelegateCommand CancelCommand { get; set; } public DelegateCommand OkCommnad { get; set; } public DelegateCommand CleanCommand { get; set; } public DelegateCommand LoadedCommand { get; set; } public ObservableCollection AnnotationListItems { get; set; } private ObservableCollection annotationColors = new ObservableCollection(); public ObservableCollection AnnotationColors { get { return annotationColors; } set { SetProperty(ref annotationColors, value); } } private ObservableCollection annotationAuthor = new ObservableCollection(); public ObservableCollection AnnotationAuthor { get { return annotationAuthor; } set { SetProperty(ref annotationAuthor, value); } } private CustomIconToggleBtn btnHighlight = null; private CustomIconToggleBtn btnFreeHand = null; private CustomIconToggleBtn btnAnnotSticky = null; private CustomIconToggleBtn btnAnnotStamp = null; private CustomIconToggleBtn btnSharpLine = null; private CustomIconToggleBtn btnSharpArrow = null; private CustomIconToggleBtn btnAnnotCircle = null; private CustomIconToggleBtn btnAnnotSquare = null; private CustomIconToggleBtn btnAnnotFreeText = null; private CustomIconToggleBtn btnAnnotStrikeout = null; private CustomIconToggleBtn btnUnderLine = null; private CustomIconToggleBtn btnAnnotLink = null; private ListBox _ListColor = null; private ListBox _ListAuthor = null; private List iconToggleBtns = new List(); private List colors = new List(); private List authors = new List(); private List annotArgsTypes = new List(); public List Colors { get => colors; set => colors = value; } public List Authors { get => authors; set => authors = value; } public List AnnotArgsTypes { get => annotArgsTypes; set => annotArgsTypes = value; } public IEventAggregator events; public ScreenAnnotationDialogViewModel(IEventAggregator eventAggregator) { events = eventAggregator; //this.events.GetEvent().Subscribe(Loading, e => e.Unicode == App.mainWindowViewModel.SelectedItem.Unicode); LoadedCommand = new DelegateCommand(Loaded); CancelCommand = new DelegateCommand(CancelEvent); OkCommnad = new DelegateCommand(OkEvent); CleanCommand = new DelegateCommand(CleanEvent); InitString(); } private void Loaded(object obj) { if (obj is Object[] arrys1) { foreach (var item in arrys1) { if (item is CustomIconToggleBtn iconToggleBtn) { string tag = iconToggleBtn.Tag.ToString(); switch (tag) { case "Highlight": btnHighlight = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "FreeHand": btnFreeHand = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotSticky": btnAnnotSticky = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotStamp": btnAnnotStamp = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "SharpLine": btnSharpLine = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "SharpArrow": btnSharpArrow = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotCircle": btnAnnotCircle = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotSquare": btnAnnotSquare = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotFreeText": btnAnnotFreeText = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotStrikeout": btnAnnotStrikeout = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "Underline": btnUnderLine = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotLink": btnAnnotLink = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; } } } SetBtnSelectedState(arrys1); } if (obj is CompositeCommandParameter composite) { if (composite.Parameter is Object[] arrys) { foreach (var item in arrys) { if (item is CustomIconToggleBtn iconToggleBtn) { string tag = iconToggleBtn.Tag.ToString(); switch (tag) { case "Highlight": btnHighlight = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "FreeHand": btnFreeHand = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotSticky": btnAnnotSticky = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotStamp": btnAnnotStamp = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "SharpLine": btnSharpLine = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "SharpArrow": btnSharpArrow = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotCircle": btnAnnotCircle = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotSquare": btnAnnotSquare = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotFreeText": btnAnnotFreeText = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotStrikeout": btnAnnotStrikeout = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "Underline": btnUnderLine = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; case "AnnotLink": btnAnnotLink = iconToggleBtn; iconToggleBtns.Add(iconToggleBtn); break; } } } SetBtnSelectedState(arrys); } } } /// /// 记录设置按钮状态 /// /// private void SetBtnSelectedState(object[] arrys) { if (AnnotArgsTypes.Count > 0) { foreach (var item in AnnotArgsTypes) { switch (item) { case AnnotArgsType.AnnotNone: break; case AnnotArgsType.AnnotSquare: SetBtnType(btnAnnotSquare, false); break; case AnnotArgsType.AnnotCircle: SetBtnType(btnAnnotCircle, false); break; case AnnotArgsType.AnnotSticky: SetBtnType(btnAnnotSticky, false); break; case AnnotArgsType.AnnotLine: foreach (var annoitem in AnnotationListItems) { if (annoitem.AnnotHandlerEventArgs is LineAnnotArgs lineAnnotArgs) { if (lineAnnotArgs.HeadLineType >= (C_LINE_TYPE)1 || lineAnnotArgs.TailLineType >= (C_LINE_TYPE)1) { //箭头 SetBtnType(btnSharpArrow, false); break; } else { //线 SetBtnType(btnSharpLine, false); break; } } } break; case AnnotArgsType.AnnotFreehand: SetBtnType(btnFreeHand, false); break; case AnnotArgsType.AnnotErase: break; case AnnotArgsType.AnnotFreeText: SetBtnType(btnAnnotFreeText, false); break; case AnnotArgsType.AnnotStamp: SetBtnType(btnAnnotStamp, false); break; case AnnotArgsType.AnnotHighlight: SetBtnType(btnHighlight, false); break; case AnnotArgsType.AnnotUnderline: SetBtnType(btnUnderLine, false); break; case AnnotArgsType.AnnotStrikeout: SetBtnType(btnAnnotStrikeout, false); break; case AnnotArgsType.AnnotSquiggly: SetBtnType(btnAnnotSquare, false); break; case AnnotArgsType.AnnotLink: SetBtnType(btnAnnotLink, false); break; case AnnotArgsType.AnnotSelectTool: break; case AnnotArgsType.SnapshotTool: break; case AnnotArgsType.SnapshotWithEditTool: break; case AnnotArgsType.WidgetViewForm: break; case AnnotArgsType.AnnotSound: break; case AnnotArgsType.AnnotMedia: break; case AnnotArgsType.AnnotRedaction: break; default: break; } } } if (arrys[12] is System.Windows.Controls.ListBox listColor) { this._ListColor = listColor; if (Colors.Count > 0) { listColor.ItemsSource = AnnotationColors; foreach (var item in AnnotationColors) { foreach (var color in Colors) { if (item.Color.ToString() == color) { ListBoxItem myListBoxItem = (ListBoxItem)(listColor.ItemContainerGenerator.ContainerFromItem(item)); myListBoxItem.IsSelected = true; } } } } } if (arrys[13] is System.Windows.Controls.ListBox listAuthor) { this._ListAuthor = listAuthor; if (Authors.Count > 0) { listAuthor.ItemsSource = AnnotationAuthor; foreach (var item in AnnotationAuthor) { foreach (var author in Authors) { if (item.Name == author) { ListBoxItem myListBoxItem = (ListBoxItem)(listAuthor.ItemContainerGenerator.ContainerFromItem(item)); myListBoxItem.IsSelected = true; } } } } } } /// /// 清除筛选项 /// /// private void CleanEvent(Object obj) { for (int i = 0; i < iconToggleBtns.Count; i++) { SetBtnType(iconToggleBtns[i], true); } if (this._ListColor != null) { this._ListColor.SelectedItems.Clear(); } if (this._ListAuthor != null) { this._ListAuthor.SelectedItems.Clear(); } //DialogParameters valuePairs = new DialogParameters(); //valuePairs.Add(ParameterNames.AnnotationCleanState, true); //RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs)); } /// /// 设置类型按钮状态 /// /// private void SetBtnType(CustomIconToggleBtn btn, bool flag) { if (btn != null) { if (btn.IsChecked == flag) { btn.IsChecked = !flag; } } } /// /// 确认 /// /// private void OkEvent(Object obj) { List styleDic = GetAnnotArgsType(); List colors = new List(); List authors = new List(); if (this._ListColor != null) { if (this._ListColor.SelectedItems.Count > 0) { foreach (var item in this._ListColor.SelectedItems) { ColorItem color = item as ColorItem; colors.Add(color.Color.ToString()); } } } if (this._ListAuthor != null) { if (this._ListAuthor.SelectedItems.Count > 0) { foreach (var item in this._ListAuthor.SelectedItems) { AuthorItem author = item as AuthorItem; authors.Add(author.Name); } } } DialogParameters valuePairs = new DialogParameters(); valuePairs.Add(ParameterNames.AnnotArgsTypes, styleDic); valuePairs.Add(ParameterNames.AnnotationColors, colors); valuePairs.Add(ParameterNames.AnnotationAuthor, authors); valuePairs.Add(ParameterNames.AnnotationCleanState, false); RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs)); } /// /// 获取类型按钮状态 /// /// /// private List GetAnnotArgsType() { List styleDic = new List(); //CustomIconToggleBtn btnHighlight = arrys[0] as CustomIconToggleBtn; //CustomIconToggleBtn btnFreeHand = arrys[1] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotSticky = arrys[2] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotStamp = arrys[3] as CustomIconToggleBtn; //CustomIconToggleBtn btnSharpLine = arrys[4] as CustomIconToggleBtn; //CustomIconToggleBtn btnSharpArrow = arrys[5] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotCircle = arrys[6] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotSquare = arrys[7] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotFreeText = arrys[8] as CustomIconToggleBtn; //CustomIconToggleBtn btnAnnotStrikeout = arrys[9] as CustomIconToggleBtn; //CustomIconToggleBtn btnUnderLine = arrys[10] as CustomIconToggleBtn; GetBtnAnnotArgsTypeState(btnHighlight, styleDic, AnnotArgsType.AnnotHighlight); GetBtnAnnotArgsTypeState(btnFreeHand, styleDic, AnnotArgsType.AnnotFreehand); GetBtnAnnotArgsTypeState(btnAnnotSticky, styleDic, AnnotArgsType.AnnotSticky); GetBtnAnnotArgsTypeState(btnAnnotStamp, styleDic, AnnotArgsType.AnnotStamp); GetBtnAnnotArgsTypeState(btnSharpLine, styleDic, AnnotArgsType.AnnotLine); GetBtnAnnotArgsTypeState(btnSharpArrow, styleDic, AnnotArgsType.AnnotLine); GetBtnAnnotArgsTypeState(btnAnnotCircle, styleDic, AnnotArgsType.AnnotCircle); GetBtnAnnotArgsTypeState(btnAnnotSquare, styleDic, AnnotArgsType.AnnotSquare); GetBtnAnnotArgsTypeState(btnAnnotFreeText, styleDic, AnnotArgsType.AnnotFreeText); GetBtnAnnotArgsTypeState(btnAnnotStrikeout, styleDic, AnnotArgsType.AnnotStrikeout); GetBtnAnnotArgsTypeState(btnUnderLine, styleDic, AnnotArgsType.AnnotUnderline); GetBtnAnnotArgsTypeState(btnAnnotLink, styleDic, AnnotArgsType.AnnotLink); return styleDic; } /// /// 添加被选中的类型按钮到集合 /// /// /// /// private void GetBtnAnnotArgsTypeState(CustomIconToggleBtn btnType, List styleDic, AnnotArgsType annotArgsType) { if (btnType != null) { if (btnType.IsChecked == true) { if (styleDic.IndexOf(annotArgsType) == -1) { styleDic.Add(annotArgsType); } } } } private void CancelEvent() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } /// /// 添加筛选颜色 /// /// private void GetAnnotationColors(System.Windows.Media.Color color) { if (AnnotationColors.Count > 0) { for (int i = 0; i < AnnotationColors.Count; i++) { System.Windows.Media.Color color1 = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(AnnotationColors[i].Color.ToString()); if (color1.R == color.R && color1.G == color.G && color1.B == color.B && color1.A == color.A) { AnnotationColors.Remove(AnnotationColors[i]); } } } AnnotationColors.Add(new ColorItem(color)); } public void OnDialogOpened(IDialogParameters parameters) { ObservableCollection list; parameters.TryGetValue>(ParameterNames.AnnotationList, out list); AnnotationListItems = list; List colors = new List(); List authors = new List(); List annotArgsTypes = new List(); parameters.TryGetValue>(ParameterNames.AnnotationColors, out colors); parameters.TryGetValue>(ParameterNames.AnnotationAuthor, out authors); parameters.TryGetValue>(ParameterNames.AnnotArgsTypes, out annotArgsTypes); if (colors != null) { this.Colors = colors; } if (authors != null) { this.Authors = authors; } if (annotArgsTypes != null) { this.AnnotArgsTypes = annotArgsTypes; } if (AnnotationListItems.Count <= 0) { return; } foreach (var item in AnnotationListItems) { AnnotationAuthor.Add(new AuthorItem(item.Author)); AnnotHandlerEventArgs data = item.AnnotHandlerEventArgs; switch (item.EventType) { case AnnotArgsType.AnnotFreeText://文本 if (data is FreeTextAnnotArgs textAnnotArgs) { GetAnnotationColors(textAnnotArgs.FontColor); } AnnotFreeTextVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotHighlight://高亮 if (data is TextHighlightAnnotArgs highlightAnnotArgs) { GetAnnotationColors(highlightAnnotArgs.Color); } HighlightVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotFreehand://手绘 if (data is FreehandAnnotArgs freehandAnnotArgs) { GetAnnotationColors(freehandAnnotArgs.InkColor); } FreeHandVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotSquiggly://波浪线 break; case AnnotArgsType.AnnotStamp://图章 AnnotStampVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotStrikeout://删除线 if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs) { GetAnnotationColors(textStrikeoutAnnotArgs.Color); } AnnotStrikeoutVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotSticky://便签 if (data is StickyAnnotArgs stickyAnnotArgs) { GetAnnotationColors(stickyAnnotArgs.Color); } AnnotStickyVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotUnderline://下划线 if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs) { GetAnnotationColors(textUnderlineAnnotArgs.Color); } UnderLineVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotLine: if (data is LineAnnotArgs lineAnnotArgs) { if (lineAnnotArgs.HeadLineType >= (C_LINE_TYPE)1 || lineAnnotArgs.TailLineType >= (C_LINE_TYPE)1) { //箭头 SharpArrowVisibility = Visibility.Visible; } else { //线 SharpLineVisibility = Visibility.Visible; } GetAnnotationColors(lineAnnotArgs.LineColor); } break; case AnnotArgsType.AnnotSquare://矩形 if (data is SquareAnnotArgs squareAnnotArgs) { GetAnnotationColors(squareAnnotArgs.BgColor); GetAnnotationColors(squareAnnotArgs.LineColor); } AnnotSquareVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotCircle://圆 if (data is CircleAnnotArgs circleAnnotArgs) { GetAnnotationColors(circleAnnotArgs.BgColor); GetAnnotationColors(circleAnnotArgs.LineColor); } AnnotCircleVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotLink: AnnotLinkVisible = Visibility.Visible; break; } } AnnotationAuthor = new ObservableCollection(AnnotationAuthor.DistinctHelper(s => s.Name)); AnnotationAuthor = new ObservableCollection(AnnotationAuthor.Where(a => string.IsNullOrEmpty(a.Name) != true)); } } }