using ComPDFKit.PDFAnnotation; using ComPDFKitViewer.AnnotEvent; using Dropbox.Api.Users; using DryIoc; using ImTools; using Microsoft.Office.Interop.Word; using PDF_Office.CustomControl.CompositeControl; using PDF_Office.Model; using PDF_Office.Model.BOTA; using Prism.Commands; using Prism.Common; 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.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Annotations; using System.Windows.Media; namespace PDF_Office.ViewModels.Dialog.BOTA { public class AuthorItem : BindableBase { private string name; public string Name { get { return name; } set { SetProperty(ref name, value); } } public AuthorItem(string name) { Name = name; } } public class Compare : IEqualityComparer { private Func _getField; public Compare(Func getfield) { this._getField = getfield; } public bool Equals(T x, T y) { return EqualityComparer.Default.Equals(_getField(x), _getField(y)); } public int GetHashCode(T obj) { return EqualityComparer.Default.GetHashCode(this._getField(obj)); } } public static class CommonHelper { /// /// 自定义Distinct扩展方法 /// /// 要去重的对象类 /// 自定义去重的字段类型 /// 要去重的对象 /// 获取自定义去重字段的委托 /// public static IEnumerable MyDistinct(this IEnumerable source, Func getfield) { return source.Distinct(new Compare(getfield)); } } internal class ScreenAnnotationDialogViewModel : BindableBase, IDialogAware { 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 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 CreateCommnad { 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 List annotationColors = new List(); //private List annotationAuthor = new List(); public ScreenAnnotationDialogViewModel() { CancelCommand = new DelegateCommand(CancelEvent); CreateCommnad = new DelegateCommand(CreateEvent); } private void CreateEvent() { DialogParameters valuePairs = new DialogParameters(); //valuePairs.Add(ParameterNames.Bookmark, Bookmark); RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs)); } private void CancelEvent() { RequestClose.Invoke(new DialogResult(ButtonResult.Cancel)); } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } private void SetColor(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)); //var bFind = AnnotationColors.All(p => p.Color == new SolidColorBrush(color)); //if (bFind) //{ // AnnotationColors.Add(new ColorItem(color)); //} } public void OnDialogOpened(IDialogParameters parameters) { ObservableCollection list; parameters.TryGetValue>(ParameterNames.AnnotationList, out list); AnnotationListItems = list; 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) { SetColor(textAnnotArgs.FontColor); } AnnotFreeTextVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotHighlight://高亮 if (data is TextHighlightAnnotArgs highlightAnnotArgs) { SetColor(highlightAnnotArgs.Color); } HighlightVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotFreehand://手绘 if (data is FreehandAnnotArgs freehandAnnotArgs) { SetColor(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) { SetColor(textStrikeoutAnnotArgs.Color); } AnnotStickyVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotSticky://便签 if (data is StickyAnnotArgs stickyAnnotArgs) { SetColor(stickyAnnotArgs.Color); } AnnotStickyVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotUnderline://下划线 if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs) { SetColor(textUnderlineAnnotArgs.Color); } UnderLineVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotLine: if ((item.AnnotHandlerEventArgs as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (item.AnnotHandlerEventArgs as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1) { //箭头 SharpArrowVisibility = Visibility.Visible; } else { //线 SharpLineVisibility = Visibility.Visible; } SetColor((item.AnnotHandlerEventArgs as LineAnnotArgs).LineColor); break; case AnnotArgsType.AnnotSquare://矩形 if (data is SquareAnnotArgs squareAnnotArgs) { SetColor(squareAnnotArgs.BgColor); SetColor(squareAnnotArgs.LineColor); } AnnotSquareVisibility = Visibility.Visible; break; case AnnotArgsType.AnnotCircle://圆 if (data is CircleAnnotArgs circleAnnotArgs) { SetColor(circleAnnotArgs.BgColor); SetColor(circleAnnotArgs.LineColor); } AnnotCircleVisibility = Visibility.Visible; break; } } //AnnotationAuthor = AnnotationAuthor.MyDistinct(s => s.Name) as ObservableCollection; //AnnotationColors = AnnotationColors.MyDistinct(s => s.Color) as ObservableCollection; AnnotationAuthor = new ObservableCollection(AnnotationAuthor.MyDistinct(s => s.Name)); //AnnotationColors = new ObservableCollection(AnnotationColors.MyDistinct(s => s.Color)); } } }