using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using PDF_Office.CustomControl.CompositeControl; using PDF_Office.Helper; using PDF_Office.Model; using PDF_Office.Model.AnnotPanel; using PDF_Office.Model.PropertyPanel.AnnotPanel; using PDF_Office.ViewModels.Tools; using PDF_Office.ViewModels.Tools.AnnotManager; using PDFSettings; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Media; namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel { //橡皮擦的UI大小效果 public class EraseThicknessConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is double) { return (double)value * 6; } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } public class FreehandAnnotPropertyViewModel : BindableBase, INavigationAware { #region 文案 private void InitString() { } #endregion #region 属性 //手绘画笔橡皮擦的大小列表 public List PenSizeItems { get; protected set; } public List EraserSizeItems { get; protected set; } private AnnotCommon _basicVm = new AnnotCommon(); public AnnotCommon BasicVm { get { return _basicVm; } set => SetProperty(ref _basicVm, value); } private bool _isPen = true; public bool IsPen { get { return _isPen; } set => SetProperty(ref _isPen, value); } private double _erasethicknessLine = 10; public double EraseThicknessLine { get { return _erasethicknessLine; } set => SetProperty(ref _erasethicknessLine, value); } private DoubleCollection _strokeDashArray = new DoubleCollection(); public DoubleCollection StrokeDashArray { get { return _strokeDashArray; } set => SetProperty(ref _strokeDashArray, value); } #endregion 属性 public AnnotAttribEvent AnnotEvent { get; set; } private AnnotHandlerEventArgs Annot; private AnnotTransfer PropertyPanel; public DelegateCommand EraseCommand { get; set; } public DelegateCommand PenCommand { get; set; } public DelegateCommand SelectedColorChangedCommand { get; set; } public DelegateCommand SelectPenThickChangedCommand { get; set; } public DelegateCommand SetEraserThickCommand { get; set; } public DelegateCommand LineModeCheckedCommand { get; set; } public DelegateCommand SelectedOpacityValueCommand { get; set; } public FreehandAnnotPropertyViewModel() { //选择工具 EraseCommand = new DelegateCommand(Erase_Command); PenCommand = new DelegateCommand(Pen_Command); //手绘属性 SelectedColorChangedCommand = new DelegateCommand(SelectedColorChanged); SelectPenThickChangedCommand = new DelegateCommand(SelectPenThickChanged); SetEraserThickCommand = new DelegateCommand(SelectEraserThickChanged); LineModeCheckedCommand = new DelegateCommand(LineMode_Checked); SelectedOpacityValueCommand = new DelegateCommand(SelectedOpacityValue); //初始化 InitString(); InitVariable(); } private void InitVariable() { InitPenSizeItems(); InitEraserSizeItems(); InitColorItems(); } //画笔的大小列表 private void InitPenSizeItems() { PenSizeItems = new List(); ComboDataItem item = new ComboDataItem(1, "pt"); PenSizeItems.Add(item); item = new ComboDataItem(2, "pt"); PenSizeItems.Add(item); item = new ComboDataItem(4, "pt"); PenSizeItems.Add(item); item = new ComboDataItem(6, "pt"); PenSizeItems.Add(item); item = new ComboDataItem(8, "pt"); PenSizeItems.Add(item); } //橡皮擦的大小列表 private void InitEraserSizeItems() { EraserSizeItems = new List(); ComboDataItem item = new ComboDataItem(5, "pt"); EraserSizeItems.Add(item); item = new ComboDataItem(10, "pt"); EraserSizeItems.Add(item); item = new ComboDataItem(15, "pt"); EraserSizeItems.Add(item); item = new ComboDataItem(20, "pt"); EraserSizeItems.Add(item); } private void InitColorItems() { BasicVm.ColorItems = AnnotColorList.GetColorList(ColorSelectorType.Border); } //设置颜色 private void SelectedColorChanged(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { BasicVm.FontColor = new SolidColorBrush(colorValue); BasicVm.FontColor.Opacity = BasicVm.FillOpacity; PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Color, colorValue); if (BasicVm.IsMultiSelected == false) { PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, colorValue); } } } } //设置线条大小 private void SelectPenThickChanged(object obj) { if (obj != null && obj is double) { var thick = (double)obj; PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Thickness, thick); } } //设置橡皮擦大小 private void SelectEraserThickChanged(object obj) { if (obj != null && obj is double) { var eraser = (double)obj; AnnotEvent?.UpdateAttrib(AnnotAttrib.Thickness, eraser); AnnotEvent?.UpdateAnnot(); } } //设置线条样式 private void LineMode_Checked(object obj) { if (obj != null) { var tag = ((string)obj); DashStyle newDash = new DashStyle(); StrokeDashArray = new DoubleCollection(); switch (tag) { case "Dashed": newDash.Dashes.Add(2); newDash.Dashes.Add(2); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, newDash); StrokeDashArray.Add(1); StrokeDashArray.Add(1); break; case "Solid": PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.LineStyle, DashStyles.Solid); break; } } } //设置不透明度 private void SelectedOpacityValue(object obj) { if (obj != null) { BasicVm.FillOpacity = (double)obj; BasicVm.FontColor.Opacity = BasicVm.FillOpacity; PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity); PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, BasicVm.FillOpacity); } } //选择橡皮擦 private void Erase_Command(object obj) { var btn = obj as ToggleButton; if(btn.IsChecked == true) { //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值 CurrenFreeHandAnnot = PropertyPanel.annot as FreehandAnnotArgs; PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotErase, btn); IsPen = false; } } private FreehandAnnotArgs CurrenFreeHandAnnot;//用来切换橡皮擦时,保存当前的手绘; //选择画笔 private void Pen_Command(object obj) { var btn = obj as ToggleButton; if (btn.IsChecked == true) { PropertyPanel.InvokeToMyTools(AnnotArgsType.AnnotFreehand, CurrenFreeHandAnnot); IsPen = true;; if (CurrenFreeHandAnnot is FreehandAnnotArgs) { BasicVm.AnnotTypeTitle = "画笔"; } else { BasicVm.AnnotTypeTitle = "橡皮擦"; } } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { BasicVm.IsMultiSelected = false; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel); if (PropertyPanel != null) { AnnotEvent = PropertyPanel.AnnotEvent; Annot = PropertyPanel.annot; BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected; if(Annot is FreehandAnnotArgs) { BasicVm.AnnotTypeTitle = "画笔"; } else { BasicVm.AnnotTypeTitle = "橡皮擦"; } if (BasicVm.IsMultiSelected) { IsAttributeEquals(); } else { GetAnnotProperty(); } } } private List ConvertLists() { List Lists = new List(); foreach (var item in PropertyPanel.annotlists) { var selecteditem = item as FreehandAnnotArgs; if (selecteditem != null) { Lists.Add(selecteditem); } } if (Lists.Count != PropertyPanel.annotlists.Count) return null; else return Lists; } private void IsAttributeEquals() { var list = ConvertLists(); if (list != null) { var temp = list[0]; Dictionary isNoEqualsDir = new Dictionary(); isNoEqualsDir.Add("Color", false); isNoEqualsDir.Add("Thickness", false); isNoEqualsDir.Add("FontStyleFontWeight", false); isNoEqualsDir.Add("ThickSolidDashStyle", false); foreach (var item in list) { if (item == list[0]) continue; if (isNoEqualsDir["Color"] == false) { if (temp.InkColor.A != item.InkColor.A || temp.InkColor.R != item.InkColor.R || temp.InkColor.G != item.InkColor.G || temp.InkColor.B != item.InkColor.B) { BasicVm.FontColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff)); isNoEqualsDir["Color"] = true; } } if (isNoEqualsDir["Thickness"] == false) { isNoEqualsDir["Thickness"] = true; if (temp.LineWidth > item.LineWidth) { BasicVm.AnnotThickness = temp.LineWidth; } else { BasicVm.AnnotThickness = item.LineWidth; } } if(isNoEqualsDir["ThickSolidDashStyle"] == false) { if (AnnotTransfer.IsSolidStyle(temp.LineDash) != AnnotTransfer.IsSolidStyle(item.LineDash)) { isNoEqualsDir["ThickSolidDashStyle"] = true; } } } if (isNoEqualsDir["Color"] == false) { BasicVm.FontColor = new SolidColorBrush(temp.InkColor); } if (isNoEqualsDir["Thickness"] == false) { BasicVm.AnnotThickness = temp.LineWidth; } if(isNoEqualsDir["ThickSolidDashStyle"] == true) { BasicVm.IsSolidLine = BasicVm.IsDashLine = false; } else { var isSolid = AnnotTransfer.IsSolidStyle(temp.LineDash); BasicVm.IsSolidLine = isSolid; BasicVm.IsDashLine = !isSolid; } } } private void GetAnnotProperty() { if (Annot is FreehandAnnotArgs) { var annot = Annot as FreehandAnnotArgs; if (annot != null) { BasicVm.FillOpacity = annot.Transparency; BasicVm.FontColor = new SolidColorBrush(annot.InkColor); BasicVm.AnnotThickness = annot.LineWidth; BasicVm.Dash = annot.LineDash; var isSolid = AnnotTransfer.IsSolidStyle(annot.LineDash); BasicVm.IsSolidLine = isSolid; BasicVm.IsDashLine = !isSolid; IsPen = true; StrokeDashArray = new DoubleCollection(); if (isSolid == false) { StrokeDashArray.Add(1); StrokeDashArray.Add(1); } } } if (Annot is EraseArgs) { var annot = Annot as EraseArgs; if (annot != null) { EraseThicknessLine = annot.Thickness; IsPen = false; } } } } }