using ComPDFKitViewer; using ComPDFKitViewer.AnnotEvent; using PDF_Office.CustomControl.CompositeControl; 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.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel { public class FreetextAnnotPropertyViewModel : BindableBase, INavigationAware { #region 文案 private string T_title; public string T_Title { get { return T_title; } set { SetProperty(ref T_title, value); } } private string T_font; public string T_Font { get { return T_font; } set { SetProperty(ref T_font, value); } } private string T_fill; public string T_Fill { get { return T_fill; } set { SetProperty(ref T_fill, value); } } private void InitString() { T_Title = App.MainPageLoader.GetString("FreeText_Title"); T_Font = App.MainPageLoader.GetString("FreeText_Font"); T_Fill = App.MainPageLoader.GetString("FreeText_Fill"); } #endregion private FontBoardVm _fontVm = new FontBoardVm(true); public FontBoardVm FontVm { get { return _fontVm; } set { SetProperty(ref _fontVm, value); } } private AnnotCommon _basicVm = new AnnotCommon(); public AnnotCommon BasicVm { get { return _basicVm; } set => SetProperty(ref _basicVm, value); } private bool _isFillFreeText = false; public bool IsFillFreeTextAnnot { get { return _isFillFreeText; } set => SetProperty(ref _isFillFreeText, value); } private bool _isCheckedTime = false; public bool IsCheckedTime { get { return _isCheckedTime; } set => SetProperty(ref _isCheckedTime, value); } private ComboDataItem _currrentDateFormat = new ComboDataItem("M/d", "format"); public ComboDataItem CurrrentDateFormat { get { return _currrentDateFormat; } set => SetProperty(ref _currrentDateFormat, value); } public List DateFormatItems { get; private set; } public AnnotAttribEvent AnnotEvent { get; set; } private FreeTextAnnotArgs Annot; private AnnotTransfer PropertyPanel; public DelegateCommand SelectedPresetFontCommand { get; set; } public DelegateCommand FontFamilyChangedCommand { get; set; } public DelegateCommand FontStyleWeightChangedCommand { get; set; } public DelegateCommand SelectedFillOpacityCommand { get; set; } public DelegateCommand SelectedColorCommand { get; set; } public DelegateCommand SelectedFillColorCommand { get; set; } public DelegateCommand FontSizeChangedCommand { get; set; } public DelegateCommand TextAlignCheckedCommand { get; set; } public DelegateCommand LineModeCheckedCommand { get; set; } public DelegateCommand SelectedOpacityValueCommand { get; set; } public DelegateCommand CustomFontStyleCommand { get; set; } public DelegateCommand ReDefineFontStyleCommand { get; set; } public DelegateCommand RestoreDefaultStyleCommand { get; set; } public DelegateCommand DateFormatChangedCommand { get; set; } public DelegateCommand ShowTimeCheckedChnagedCommand { get; set; } public event EventHandler LoadPropertyHandler; public FreetextAnnotPropertyViewModel() { InitString(); //文本设置 //预设文本样式 SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont); //文本字重、字体 FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged); //字体 FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged); //自定义 CustomFontStyleCommand = new DelegateCommand(CustomFontStyle); //重新定义 ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle); //恢复默认 RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle); //颜色 SelectedColorCommand = new DelegateCommand(SelectedColor_Command); //填充颜色 SelectedFillColorCommand = new DelegateCommand(SelectedFillColorChange); //填充颜色不透明度 SelectedFillOpacityCommand = new DelegateCommand(SelectedFillOpacity); //大小 FontSizeChangedCommand = new DelegateCommand(FontSizeChanged); //内容对齐 TextAlignCheckedCommand = new DelegateCommand(TextAlignChecked); //填写与签名:日期 DateFormatChangedCommand = new DelegateCommand(DateFormatChanged); ShowTimeCheckedChnagedCommand = new DelegateCommand(ShowTimeCheckedChnaged); //MVP不上 LineModeCheckedCommand = new DelegateCommand(LineMode_Checked); SelectedOpacityValueCommand = new DelegateCommand(SelectedOpacityValue); InitVariable(); } private void InitVariable() { DateFormatItems = new List(); DateFormatItems = TextFont.GetDateFormats(); UpdateDateFormatLists(); InitFillColorItems(); } private void InitFillColorItems() { BasicVm.FillColorItems = AnnotColorList.GetColorList(ColorSelectorType.Fill); } private void UpdateDateFormatLists() { string strTime = " HH:mm:ss"; foreach (var item in DateFormatItems) { if (IsCheckedTime) strTime = item.ValueStr + " HH:mm:ss"; else strTime = item.ValueStr; string stadate = System.DateTime.Now.ToString(strTime); item.SetContent(stadate); if (CurrrentDateFormat != null && item.ValueStr == CurrrentDateFormat.ValueStr) CurrrentDateFormat.SetContent(stadate); } CurrrentDateFormat = new ComboDataItem(CurrrentDateFormat.ValueStr, CurrrentDateFormat.Content); } private void TextAlignChecked(object obj) { if ((string)obj != null) { switch ((string)obj) { case "AlignLeft": GetAnnotAlign(TextAlignment.Left); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Left); break; case "AlignCenter": GetAnnotAlign(TextAlignment.Center); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Center); break; case "AlignRight": GetAnnotAlign(TextAlignment.Right); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Right); break; case "Justify": GetAnnotAlign(TextAlignment.Justify); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify); break; } } } private void LineMode_Checked(object obj) { if(obj != null) { var tag = ((string)obj); switch (tag) { case "Dashed": AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Dash); break; case "Solid": AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, DashStyles.Solid); break; } AnnotEvent?.UpdateAnnot(); } } //从预设样式设置文本 private void SelectedPresetFont() { if (FontVm.CurrentPresetFont != null) { var itemComboxItem = FontVm.CurrentPresetFont; var item = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr); if (item != null) { FontVm.GetCurrentFontSize(item.mFontSize); if (item.mFontFamily != null) FontVm.GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString()); FontVm.GetFontWeights_Style(item.mFontStyle, item.mFontWeight); Dictionary AnnotAttribDir = new Dictionary(); AnnotAttribDir.Add(AnnotAttrib.FontSize, item.mFontSize); AnnotAttribDir.Add(AnnotAttrib.FontFamily, item.mFontFamily); AnnotAttribDir.Add(AnnotAttrib.FontStyle, item.mFontStyle); AnnotAttribDir.Add(AnnotAttrib.FontWeight, item.mFontWeight); PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir); } } } //设置文本字重、样式 private void FontStyleWeightChanged() { if(FontVm.CurrrentFontWeightStyle != null) { FontVm.UpdateFontWeight_Style(); Dictionary AnnotAttribDir = new Dictionary(); AnnotAttribDir.Add(AnnotAttrib.FontStyle, FontVm.FontStyleItem); AnnotAttribDir.Add(AnnotAttrib.FontWeight, FontVm.FontWeightItem); PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir); } } private void SelectedFillOpacity(object obj) { if (obj != null) { BasicVm.FillOpacity = (double)obj; BasicVm.FillColor.Opacity = BasicVm.FillOpacity; PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity); } } private void SelectedOpacityValue(object obj) { if (obj != null && obj is double) { BasicVm.FillOpacity = (double)obj; BasicVm.FillColor.Opacity = BasicVm.FillOpacity; PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.Transparency, BasicVm.FillOpacity); } } //设置字体大小 private void FontSizeChanged() { if (FontVm.CurrentFontSize != null) { PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize, FontVm.CurrentFontSize.Value); if (BasicVm.IsMultiSelected == false) PropertyPanel.InvokeToMyTools(BasicVm.AnnotType, Annot); } } //设置字体样式 private void FontFamilyChanged() { if (string.IsNullOrEmpty(FontVm.CurrentFontFamily.ValueStr) == false) { PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontFamily, new FontFamily(FontVm.CurrentFontFamily.ValueStr)); } } private void SelectedFillColorChange(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { BasicVm.FillColor = new SolidColorBrush(colorValue); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FillColor, colorValue); // BasicVm.FillColor.Opacity = BasicVmFillOpacity; } } } private void SelectedColor_Command(object obj) { if (obj != null) { var colorValue = (Color)obj; if (colorValue != null) { FontVm.FontColor = new SolidColorBrush(colorValue); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontColor, colorValue); } } } private void CustomFontStyle() { if (FontVm.CurrentPresetFont != null) { ContextMenu menu; if (FontVm.CurrentPresetFont.ValueStr == "custom") { menu = SelectAnnotContextMenu(false); } else { menu = SelectAnnotContextMenu(true); } if (menu != null) { menu.IsOpen = true; } } } private ContextMenu SelectAnnotContextMenu(bool isEnable) { var popMenu = App.Current.FindResource("CustomFontStyleFlyoutMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 2) { //用所选部分重新定义 MenuItem menuItem = popMenu.Items[0] as MenuItem; menuItem.IsEnabled = isEnable; menuItem.Command = ReDefineFontStyleCommand; //恢复默认预设样式 menuItem = popMenu.Items[1] as MenuItem; menuItem.IsEnabled = isEnable; menuItem.Command = RestoreDefaultStyleCommand; } return popMenu; } public List FontStyleList = new List(); private void ReDefineFontStyle() { var item = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr); if (item == null) return; item.mFontFamily = new FontFamily(FontVm.CurrentFontFamily.ValueStr); if (FontVm.CurrrentFontWeightStyle.ValueStr == "Bold") { item.mFontStyle = FontStyles.Normal; item.mFontWeight = FontWeights.Bold; } else if (FontVm.CurrrentFontWeightStyle.ValueStr == "Regular") { item.mFontStyle = FontStyles.Normal; item.mFontWeight = FontWeights.Normal; } else { item.mFontStyle = FontStyles.Italic; item.mFontWeight = FontWeights.Bold; } item.mFontSize = (int)FontVm.CurrentFontSize.Value; } private void RestoreDefaultStyle() { var defaultlists = TextFont.GetPresetFontStyle(); if(FontVm.CurrentPresetFont.ValueStr != "custom") { var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr); if (defaulItem != null) { var currentItem = FontVm.PresetFontList.FirstOrDefault(temp => temp.mTag == FontVm.CurrentPresetFont.ValueStr); if (currentItem != null) { currentItem.mTagContent = defaulItem.mTagContent; currentItem.mFontStyle = defaulItem.mFontStyle; currentItem.mFontWeight = defaulItem.mFontWeight; currentItem.mFontFamily = defaulItem.mFontFamily; currentItem.mFontSize = defaulItem.mFontSize; FontVm.GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString()); FontVm.GetCurrentFontSize(currentItem.mFontSize); FontVm.GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight); } } } } private void DateFormatChanged() { if(CurrrentDateFormat != null) { if (IsCheckedTime) UpdateDateFormatLists(); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content); } } private void ShowTimeCheckedChnaged() { if (CurrrentDateFormat != null) { UpdateDateFormatLists(); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content); } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { BasicVm.IsMultiSelected = false; // IsCheckedTime = false; } public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PropertyPanelContentViewModel, out PropertyPanel); if (PropertyPanel != null) { AnnotEvent = PropertyPanel.AnnotEvent; Annot = PropertyPanel.annot as FreeTextAnnotArgs; LoadPropertyHandler?.Invoke(this, Annot); BasicVm.AnnotTypeTitle = "文字"; BasicVm.IsMultiSelected = PropertyPanel.IsMultiSelected; IsFillFreeTextAnnot = PropertyPanel.IsTextFill; if (BasicVm.IsMultiSelected) { IsAttributeEquals(); BasicVm.IsSolidLine = BasicVm.IsDashLine = false; } else { GetAnnotProperty(); } IsFillFreeTextAnnot = PropertyPanel.IsTextFill; } } /// /// 多选的注释 /// private List ConvertLists() { List FreeTextLists = new List(); foreach (var item in PropertyPanel.annotlists) { var itemFreeText = item as FreeTextAnnotArgs; if (itemFreeText != null) { FreeTextLists.Add(itemFreeText); } } if (FreeTextLists.Count != PropertyPanel.annotlists.Count) return null; else return FreeTextLists; } private void IsAttributeEquals() { var list = ConvertLists(); if (list != null) { var temp = list[0]; Dictionary isNoEqualsDir = new Dictionary(); isNoEqualsDir.Add("FontFamily", false); isNoEqualsDir.Add("FontSize", false); isNoEqualsDir.Add("FontStyleFontWeight", false); isNoEqualsDir.Add("FontColor", false); isNoEqualsDir.Add("FillColor", false); isNoEqualsDir.Add("TextAglin", false); foreach (var item in list) { if (item == list[0]) continue; if (temp.Align != item.Align) { isNoEqualsDir["TextAglin"] = true; FontVm.StrTextAlign = "None"; } if (temp.FontFamily != item.FontFamily) { isNoEqualsDir["FontFamily"] = true; FontVm.CurrentFontFamily = null; } if (temp.FontSize != item.FontSize) { isNoEqualsDir["FontSize"] = true; FontVm.CurrentFontSize = null; } if (temp.FontStyle != item.FontStyle || temp.FontWeight != item.FontWeight) { isNoEqualsDir["FontStyleFontWeight"] = true; FontVm.CurrrentFontWeightStyle = null; } if (isNoEqualsDir["FontFamily"] == true || isNoEqualsDir["FontSize"] == true || isNoEqualsDir["FontStyleFontWeight"] == true) { FontVm.CurrentPresetFont = null; } if (isNoEqualsDir["FontColor"] == false) { if (temp.FontColor.A != item.FontColor.A || temp.FontColor.R != item.FontColor.R || temp.FontColor.G != item.FontColor.G || temp.FontColor.B != item.FontColor.B) { FontVm.FontColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff)); isNoEqualsDir["FontColor"] = true; } } if (isNoEqualsDir["FillColor"] == false) { if (temp.BgColor.A != item.BgColor.A || temp.BgColor.R != item.BgColor.R || temp.BgColor.G != item.BgColor.G || temp.BgColor.B != item.BgColor.B) { BasicVm.FillColor = new SolidColorBrush(Color.FromArgb(0x01, 0xff, 0xff, 0xff)); isNoEqualsDir["FillColor"] = true; } } } ////以下是多选注释的属性相等时 if (isNoEqualsDir["TextAglin"] == false) { GetAnnotAlign(temp.Align); } if (isNoEqualsDir["FontFamily"] == false) { FontVm.GetCurrentFontFamily(temp.FontFamily.ToString(), temp.FontFamily.ToString()); } if (isNoEqualsDir["FontSize"] == false) { FontVm.GetCurrentFontSize((int)temp.FontSize); } if (isNoEqualsDir["FontStyleFontWeight"] == false) { FontVm.GetFontWeights_Style(temp.FontStyle, temp.FontWeight); } if (isNoEqualsDir["FontColor"] == false) { FontVm.FontColor = new SolidColorBrush(temp.FontColor); } if (isNoEqualsDir["FillColor"] == false) { FontVm.FontColor = new SolidColorBrush(temp.BgColor); } } } private void GetAnnotProperty() { BasicVm.FillColor = new SolidColorBrush(Annot.BgColor); BasicVm.FillOpacity = Annot.Transparency; FontVm.FontColor = new SolidColorBrush(Annot.FontColor); FontVm.GetCurrentFontSize((int)Annot.FontSize); FontVm.GetCurrentFontFamily(Annot.FontFamily.ToString(), Annot.FontFamily.ToString()); FontVm.GetFontWeights_Style(Annot.FontStyle, Annot.FontWeight); GetTimeFormat(); GetAnnotAlign(Annot.Align); } //填写与签名 private void GetTimeFormat() { if (IsFillFreeTextAnnot == false) return; if(string.IsNullOrEmpty(Annot.TextContent)) { if (CurrrentDateFormat != null) { if (IsCheckedTime) UpdateDateFormatLists(); PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.NoteText, CurrrentDateFormat.Content); } return; } DateTime dtDate; bool isTime = false; foreach (var item in DateFormatItems) { var strFormat = ""; var strArr = Annot.TextContent.Split(' '); if (strArr != null && strArr.Count() == 2) { strFormat = item.ValueStr + " HH:mm:ss"; } else { strFormat = item.ValueStr; } if (DateTime.TryParseExact(Annot.TextContent, strFormat, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out dtDate)) { if (strArr != null && strArr.Count() == 2) { isTime = true; } foreach (var item2 in DateFormatItems) { string stadate = System.DateTime.Now.ToString(strFormat); item.SetContent(stadate); } CurrrentDateFormat = new ComboDataItem(item.ValueStr, item.Content); break; } } IsCheckedTime = isTime; } //文本内容对齐 private void GetAnnotAlign(TextAlignment align) { switch (align) { case TextAlignment.Left: FontVm.StrTextAlign = "Left"; break; case TextAlignment.Center: FontVm.StrTextAlign = "Center"; break; case TextAlignment.Right: FontVm.StrTextAlign = "Right"; break; case TextAlignment.Justify: FontVm.StrTextAlign = "Justify"; break; default: FontVm.StrTextAlign = "None"; break; } } } }