1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030 |
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer;
- using ComPDFKitViewer.AnnotEvent;
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Master.CustomControl.CompositeControl;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Model.AnnotPanel;
- using PDF_Master.Model.PropertyPanel.AnnotPanel;
- using PDF_Master.Properties;
- using PDF_Master.ViewModels.Tools.AnnotManager;
- using PDFSettings;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using static Dropbox.Api.Files.WriteMode;
- namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
- {
- public class TextEditPropertyViewModel : PDFEditVM, INavigationAware
- {
- #region 属性
- public event EventHandler ClearCheckedAglin;
- public TextEditPropertyViewModel()
- {
- InitVariable();
- InitCommand();
- }
- private void InitVariable()
- {
- InitBaseVariable();
- }
- private bool isSelectedEmpty;
- public bool IsSelectedEmpty
- {
- get { return isSelectedEmpty; }
- set { SetProperty(ref isSelectedEmpty, value); }
- }
- private string title;
- public string Title
- {
- get { return title; }
- set
- {
- SetProperty(ref title, value);
- }
- }
- private FontBoardVm _fontVm = new FontBoardVm(true);
- public FontBoardVm FontVm
- {
- get { return _fontVm; }
- set { SetProperty(ref _fontVm, value); }
- }
- private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign;
- public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign
- {
- get { return _textAlign; }
- set { _textAlign = value; }
- }
- #endregion 属性
- #region Command
- public DelegateCommand AddTextCommand { get; set; }
- public DelegateCommand AddImgCommand { get; set; }
- public DelegateCommand<object> SelectedColorCommand { get; set; }
- public DelegateCommand<object> SelectedFontStyleCommand { get; set; }
- public DelegateCommand<object> FontSizeChangedCommand { get; set; }
- public DelegateCommand<object> TextAlignCheckedCommand { get; set; }
- //进入属性面板编辑
- public DelegateCommand EditTextModeCommand { get; set; }
- public DelegateCommand FontFamilyChangedCommand { get; set; }
- public DelegateCommand CustomFontStyleCommand { get; set; }
- public DelegateCommand FontStyleWeightChangedCommand { get; set; }
- public DelegateCommand SelectedPresetFontCommand { get; set; }
- //重定义
- public DelegateCommand ReDefineFontStyleCommand { get; set; }
- public DelegateCommand RestoreDefaultStyleCommand { get; set; }
- public DelegateCommand<object> LayoutAlignCheckedCommand { get; set; }
- #endregion Command
- #region 初始化
- private void InitCommand()
- {
- //预设文本样式
- SelectedPresetFontCommand = new DelegateCommand(SelectedPresetFont);
- //字体
- FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged);
- //文本字重、字体
- FontStyleWeightChangedCommand = new DelegateCommand(FontStyleWeightChanged);
- //添加文本
- AddTextCommand = new DelegateCommand(AddText);
- //添加图片
- AddImgCommand = new DelegateCommand(AddImg);
- //选择颜色
- SelectedColorCommand = new DelegateCommand<object>(SelectedColor);
- //选择字体样式
- SelectedFontStyleCommand = new DelegateCommand<object>(SelectedFontStyle);
- //大小
- FontSizeChangedCommand = new DelegateCommand<object>(FontSizeChanged);
- //内容对齐
- TextAlignCheckedCommand = new DelegateCommand<object>(TextAlignChecked);
- //编辑模式
- EditTextModeCommand = new DelegateCommand(EditTextMode);
- //自定义
- CustomFontStyleCommand = new DelegateCommand(CustomFontStyle);
- //重新定义
- ReDefineFontStyleCommand = new DelegateCommand(ReDefineFontStyle);
- //恢复默认
- RestoreDefaultStyleCommand = new DelegateCommand(RestoreDefaultStyle);
- //文本对齐
- LayoutAlignCheckedCommand = new DelegateCommand<object>(LayoutAlignChecked);
- }
- #endregion 初始化
- #region 文本处理逻辑
- private void AddText()
- {
- //设置对应创建模式
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
- }
- private void AddImg()
- {
- PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
- }
- private void EditTextMode()
- {
- }
- /// <summary>
- /// 从预设样式设置文本
- /// </summary>
- private void SelectedPresetFont()
- {
- if (CurrentPresetFont != null)
- {
- var itemComboxItem = CurrentPresetFont;
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
- if (item != null)
- {
- if (item.mTag != "Custom")
- {
- GetCurrentFontSize(item.mFontSize);
- if (item.mFontFamily != null)
- {
- //GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
- if (GetCurrentFontFamily(item) == false)
- {
- GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
- }
- }
- GetFontWeights_Style(item.mFontStyle, item.mFontWeight);
- //CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
- //CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
- //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
- //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
- }
- }
- }
- }
- /// <summary>
- /// 编辑 获取预设字体,需要做处理,有些字体带空格,不做处理无法识别
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- private bool GetCurrentFontFamily(PresetFontItem item)
- {
- DefaultEditProperty defaultEdit = SettingHelper.GetPDFEditDefaultProperty(CPDFEditType.EditText);
- if (defaultEdit != null && defaultEdit.SystemFontNameList != null)
- {
- List<string> itemq = defaultEdit.SystemFontNameList.FindAll(p => p.Replace(" ", "").Equals(item.mFontFamily.ToString()));
- if (itemq != null && itemq.Count > 0)
- {
- GetCurrentFontFamily(item.mFontFamily.ToString(), itemq[0]);
- return true;
- }
- }
- return false;
- }
- /// <summary>
- /// 自定义文字样式
- /// </summary>
- private void CustomFontStyle()
- {
- if (CurrentPresetFont != null)
- {
- ContextMenu menu;
- if (CurrentPresetFont.ValueStr == "Custom")
- {
- menu = SelectAnnotContextMenu(false);
- }
- else
- {
- menu = SelectAnnotContextMenu(false);
- var defaultlists = TextFont.GetPresetFontStyle();
- var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (currentItem.mTag != "Custom")
- {
- if (FontWeightItem != defaulItem.mFontWeight ||
- FontStyleItem != defaulItem.mFontStyle ||
- CurrentFontSize.Value != defaulItem.mFontSize ||
- CurrentFontFamily.ValueStr != defaulItem.mFontFamily.Source)
- {
- menu = SelectAnnotContextMenu(true);
- }
- }
- }
- if (menu != null)
- {
- menu.IsOpen = true;
- }
- }
- }
- //设置字体样式
- private void FontFamilyChanged()
- {
- if (CurrentPresetFont != null)
- {
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (currentItem.mTag != "Custom")
- {
- foreach (var item in PresetFontList)
- {
- if (item.mTag == currentItem.mTag)
- {
- if (CurrentFontFamily.ValueStr != item.mFontFamily.Source)
- {
- if (noAddAnAsterisk(currentItem, item) == false)
- {
- break;
- }
- }
- else
- {
- if (noAddAnAsterisk(currentItem, item) == false)
- {
- break;
- }
- }
- }
- }
- }
- }
- }
- private bool noAddAnAsterisk(PresetFontItem currentItem, PresetFontItem item)
- {
- if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && (CurrentFontFamily.ValueStr == item.mFontFamily.Source || (CurrentFontFamily.ValueStr == "Arial" && item.mFontFamily.Source == "Helvetica")))
- {
- string txt = currentItem.mTagContent;
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- return true;
- }
- else
- {
- addAnAsterisk(currentItem);
- return false;
- }
- }
- private void addAnAsterisk(PresetFontItem currentItem)
- {
- string txt = string.Format($" * {currentItem.mTagContent}");
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- }
- /// <summary>
- /// 设置文本字重、样式
- /// </summary>
- private void FontStyleWeightChanged()
- {
- if (CurrrentFontWeightStyle != null)
- {
- UpdateFontWeight_Style();
- //FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
- //FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
- if (CurrentPresetFont != null)
- {
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (currentItem.mTag != "Custom")
- {
- foreach (var item in PresetFontList)
- {
- if (item.mTag == currentItem.mTag)
- {
- if (FontWeightItem != item.mFontWeight || FontStyleItem != item.mFontStyle)
- {
- string txt = string.Format($" * {currentItem.mTagContent}");
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- break;
- }
- else
- {
- if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
- {
- string txt = currentItem.mTagContent;
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- }
- }
- }
- }
- }
- }
- }
- }
- /// <summary>
- /// 用所选部分重新定义
- /// </summary>
- private void ReDefineFontStyle()
- {
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (item == null) return;
- item.mFontFamily = new FontFamily(CurrentFontFamily.ValueStr);
- if (CurrrentFontWeightStyle.ValueStr == "Bold")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Bold;
- }
- else if (CurrrentFontWeightStyle.ValueStr == "Regular")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Normal;
- }
- else if (CurrrentFontWeightStyle.ValueStr == "Italic")
- {
- item.mFontStyle = FontStyles.Italic;
- item.mFontWeight = FontWeights.Normal;
- }
- else
- {
- item.mFontStyle = FontStyles.Italic;
- item.mFontWeight = FontWeights.Bold;
- }
- item.mFontSize = (int)CurrentFontSize.Value;
- CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
- //SelectedPresetFont();
- //更改后 保存到本地缓存
- TextFont.SavePresetEditFontList(PresetFontList);
- }
- //重置定义
- private void RestoreDefaultStyle()
- {
- var defaultlists = TextFont.GetPresetFontStyle();
- if (CurrentPresetFont.ValueStr != "Custom")
- {
- var defaulItem = defaultlists.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (defaulItem != null)
- {
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == 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;
- //GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
- if (GetCurrentFontFamily(currentItem) == false)
- {
- GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
- }
- GetCurrentFontSize(currentItem.mFontSize);
- GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
- CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
- SelectedPresetFont();
- TextFont.SavePresetEditFontList(PresetFontList);
- }
- }
- }
- }
- //设置多选对齐方式
- private void LayoutAlignChecked(object obj)
- {
- if (obj != null)
- {
- if (obj != null)
- {
- switch ((string)obj)
- {
- case "AlignLeft":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignLeft);
- break;
- case "AlignHorizonCenter":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignHorizonCenter);
- break;
- case "AlignRight":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignRight);
- break;
- case "DistributeHorizontal":
- PDFViewer.SetPDFEditAligment(AlignModes.DistributeHorizontal);
- break;
- case "AlignTop":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignTop);
- break;
- case "AlignVerticalCenter":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignVerticalCenter);
- break;
- case "AlignBottom":
- PDFViewer.SetPDFEditAligment(AlignModes.AlignBottom);
- break;
- case "DistributeVertical":
- PDFViewer.SetPDFEditAligment(AlignModes.DistributeVertical);
- break;
- }
- }
- }
- }
- //设置文本框内对齐方式
- private void TextAlignChecked(object obj)
- {
- if ((string)obj != null && TextEditEvent != null)
- {
- switch ((string)obj)
- {
- case "AlignLeft":
- GetAnnotAlign(TextAlignment.Left);
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
- break;
- case "AlignCenter":
- GetAnnotAlign(TextAlignment.Center);
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
- break;
- case "AlignRight":
- GetAnnotAlign(TextAlignment.Right);
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
- break;
- case "Align":
- GetAnnotAlign(TextAlignment.Justify);
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
- break;
- }
- TextAlign = TextEditEvent.TextAlign;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- }
- //设置文本内容对齐属性面板显示
- private void GetAnnotAlign(TextAlignment align)
- {
- switch (align)
- {
- case TextAlignment.Left:
- StrTextAlign = "Left";
- break;
- case TextAlignment.Center:
- StrTextAlign = "Center";
- break;
- case TextAlignment.Right:
- StrTextAlign = "Right";
- break;
- case TextAlignment.Justify:
- StrTextAlign = "Justify";
- break;
- default:
- StrTextAlign = "None";
- break;
- }
- }
- //设置文本颜色
- private void SelectedColor(object obj)
- {
- if (obj != null)
- {
- var colorValue = (Color)obj;
- if (colorValue != null)
- {
- SelectColor = new SolidColorBrush(colorValue);
- }
- }
- }
- //设置文本样式
- private void SelectedFontStyle(object obj)
- {
- if (obj != null && (PresetFontItem)obj != null)
- {
- var item = (PresetFontItem)obj;
- }
- }
- //设置字体大小
- private void FontSizeChanged(object obj)
- {
- //if (CurrentFontSize != null)
- //{
- // CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
- //}
- if (CurrentPresetFont != null)
- {
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (currentItem.mTag != "Custom")
- {
- foreach (var item in PresetFontList)
- {
- if (item.mTag == currentItem.mTag)
- {
- if (CurrentFontSize.Value != item.mFontSize)
- {
- string txt = string.Format($" * {currentItem.mTagContent}");
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- break;
- }
- else
- {
- if (FontWeightItem == item.mFontWeight && FontStyleItem == item.mFontStyle && CurrentFontSize.Value == item.mFontSize && CurrentFontFamily.ValueStr == item.mFontFamily.Source)
- {
- string txt = currentItem.mTagContent;
- if (Title == txt)
- {
- Title = "";
- }
- Title = txt;
- }
- }
- }
- }
- }
- }
- }
- #endregion 文本处理逻辑
- #region 右键菜单
- //点击空白处时
- private ContextMenu EmptyStateMenu(object sender)
- {
- var popMenu = App.Current.FindResource("NoneMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //粘贴
- customMenu.SetMenuBinding(0, ApplicationCommands.Paste);
- //添加文本
- customMenu.SetMenuBinding(1, AddTextCommand);
- //添加图像
- customMenu.SetMenuBinding(2, AddImgCommand);
- return popMenu;
- }
- #endregion 右键菜单
- #region 编辑PDF内容触发的事件
- /// <summary>
- /// 右键菜单的事件
- /// </summary>
- private void PDFViewer_PDFEditCommandHandler(object sender, PDFEditCommand e)
- {
- //if (e == null)
- // return;
- //switch (e.CommandType)
- //{
- // case CommandType.Context:
- // if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.None)
- // {
- // e.PopupMenu = EmptyStateMenu(sender);
- // }
- // break;
- // default:
- // e.DoCommand();
- // break;
- //}
- //if (e.PopupMenu != null)
- //{
- // e.Handle = true;
- //}
- }
- /// <summary>
- /// 更多菜单
- /// </summary>
- /// <param name="isEnable"></param>
- /// <returns></returns>
- 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 = popMenu.Items[0] as MenuItem;
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- menuItem.IsEnabled = false;
- if (currentItem.mTag != "Custom")
- {
- if (FontWeightItem != currentItem.mFontWeight ||
- FontStyleItem != currentItem.mFontStyle ||
- CurrentFontSize.Value != currentItem.mFontSize ||
- CurrentFontFamily.ValueStr != currentItem.mFontFamily.Source)
- {
- menuItem.IsEnabled = true;
- menuItem.Command = ReDefineFontStyleCommand;
- }
- }
- //恢复默认预设样式
- menuItem = popMenu.Items[1] as MenuItem;
- menuItem.IsEnabled = isEnable;
- menuItem.Command = RestoreDefaultStyleCommand;
- }
- return popMenu;
- }
- #endregion 编辑PDF内容触发的事件
- protected List<PDFEditEvent> TextEditEventList;
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- navigationContext.Parameters.TryGetValue<List<PDFEditEvent>>(ParameterNames.AnnotEvent, out TextEditEventList);
- if (PDFViewer != null)
- {
- if (TextEditEventList != null && TextEditEventList.Count > 0)
- {
- TextEditEvent = TextEditEventList[0];
- if (TextEditEvent.FontName != "" && TextEditEvent.FontName != null)
- {
- GetPDFEdit();
- }
- if (TextEditEventList.Count > 1)
- IsSelectedEmpty = true;
- else
- IsSelectedEmpty = false;
- if (TextEditEventList.Count == 2)
- {
- IsLayoutAlign = true;
- IsLayoutAvgAlign = false;
- }
- else if (TextEditEventList.Count > 2)
- {
- IsLayoutAlign = true;
- IsLayoutAvgAlign = true;
- }
- else
- {
- IsLayoutAlign = false;
- IsLayoutAvgAlign = false;
- }
- }
- else if (TextEditEventList == null || TextEditEvent.FontName == null || TextEditEvent.FontName == "")
- {
- var defaultlists = TextFont.GetPresetFontStyle();
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == "Custom");
- if (currentItem != null)
- {
- SelectColor = new SolidColorBrush(Colors.Black);
- CurrentFontSize = new ComboDataItem(currentItem.mFontSize);
- CurrentPresetFont = new ComboDataItem(currentItem.mTag, currentItem.mTagContent);
- CurrentFontFamily = new ComboDataItem(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
- FontStyleItem = FontStyles.Normal;
- FontWeightItem = FontWeights.Normal;
- //GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
- if (GetCurrentFontFamily(currentItem) == false)
- {
- GetCurrentFontFamily(currentItem.mFontFamily.ToString(), currentItem.mFontFamily.ToString());
- }
- GetCurrentFontSize(currentItem.mFontSize);
- GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
- SelectedPresetFont();
- }
- }
- PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
- PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
- ChangedValue -= FontMode_ChangedValue;
- ChangedValue += FontMode_ChangedValue;
- IsCanSave = true;
- }
- }
- private DefaultEditProperty GetEditDefault(ComPDFKit.PDFPage.CPDFEditType editType)
- {
- var edit = SettingHelper.GetPDFEditDefaultProperty(editType);
- if (edit == null)
- {
- edit = new DefaultEditProperty();
- edit.EditType = editType;
- }
- return edit;
- }
- /// <summary>
- /// 文本内容改变触发
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void FontMode_ChangedValue(object sender, FontSetModeType e)
- {
- if (sender != null && TextEditEvent != null)
- {
- PDFEditEvent pdfEditEvent = PDFEditHelper.GetPDFEditDefaultProperty(out DefaultEditProperty defaultEdit, PDFViewer);
- switch (e)
- {
- case FontSetModeType.PresetFontStyes:
- if (PresetFontList != null && sender is string == true)
- {
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == (string)sender);
- if (item != null && TextEditEvent != null)
- {
- if (TextEditEventList.Count == 1 && IsSelectedEmpty == false)
- {
- TextEditEvent.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
- TextEditEvent.FontSize = item.mFontSize;
- TextEditEvent.IsBold = item.mFontWeight == FontWeights.Bold;
- TextEditEvent.IsItalic = item.mFontStyle == FontStyles.Italic;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- else
- {
- foreach (var itemlist in TextEditEventList)
- {
- itemlist.FontName = item.mFontFamily != null ? item.mFontFamily.Source : string.Empty;
- itemlist.FontSize = item.mFontSize;
- itemlist.IsBold = item.mFontWeight == FontWeights.Bold;
- itemlist.IsItalic = item.mFontStyle == FontStyles.Italic;
- itemlist.UpdatePDFEditByEventArgs();
- }
- }
- defaultEdit.FontName = TextEditEvent.FontName;
- defaultEdit.FontSize = TextEditEvent.FontSize;
- defaultEdit.IsBold = TextEditEvent.IsBold;
- defaultEdit.IsItalic = TextEditEvent.IsItalic;
- }
- }
- break;
- case FontSetModeType.FontFamilys:
- if (sender is string == true)
- {
- TextEditEvent.FontName = (string)sender;
- TextEditEvent.UpdatePDFEditByEventArgs();
- defaultEdit.FontName = TextEditEvent.FontName;
- }
- break;
- case FontSetModeType.FontSizes:
- if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize >= 0)
- {
- TextEditEvent.FontSize = (double)sender;
- TextEditEvent.UpdatePDFEditByEventArgs();
- defaultEdit.FontSize = TextEditEvent.FontSize;
- }
- break;
- case FontSetModeType.FontWeight_Style:
- UpdateFontWeight_Style();
- TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
- TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
- TextEditEvent.UpdatePDFEditByEventArgs();
- defaultEdit.IsBold = TextEditEvent.IsBold;
- defaultEdit.IsItalic = TextEditEvent.IsItalic;
- break;
- case FontSetModeType.FontColor:
- if (sender is Color == true)
- {
- var setColor = (Color)sender;
- var eventColor = TextEditEvent.FontColor;
- bool isok = eventColor.A != setColor.A || eventColor.B != setColor.B ||
- eventColor.G != setColor.G || eventColor.R != setColor.R;
- if (isok)
- {
- TextEditEvent.FontColor = setColor;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- }
- defaultEdit.FontColor = TextEditEvent.FontColor;
- break;
- case FontSetModeType.TextAlignment:
- break;
- }
- PDFEditHelper.GetPDFEditEvent(defaultEdit, PDFViewer);
- SettingHelper.SetPDFEditProperty(defaultEdit);
- Settings.Default.Save();
- }
- }
- //获取文本参数
- private void GetPDFEdit()
- {
- DefaultEditProperty defaultEdit = SettingHelper.GetPDFEditDefaultProperty(CPDFEditType.EditText);
- if (!App.IsGetTextFamily)
- {
- if (TextEditEvent.SystemFontNameList.Count > 0)
- {
- EditHelper.FontFamily = TextEditEvent.SystemFontNameList;
- App.IsGetTextFamily = true;
- }
- InitBase_FontFamilys();
- if (defaultEdit != null)
- {
- if (defaultEdit.SystemFontNameList.Count < EditHelper.FontFamily.Count)
- {
- defaultEdit.SystemFontNameList = EditHelper.FontFamily;
- SettingHelper.SetPDFEditProperty(defaultEdit);
- Settings.Default.Save();
- }
- }
- }
- SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
- CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
- CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
- FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
- FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
- //GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
- GetFontWeights_Style(FontStyleItem, FontWeightItem);
- //判断样式列表中是否存在对应样式
- bool isExist = false;
- foreach (var item in PresetFontList)
- {
- string itemmFontFamily = item.mFontFamily.Source;
- if (item.mFontFamily.Source == "TimesRoman")
- {
- itemmFontFamily = "Times-Roman";
- }
- if (defaultEdit != null && defaultEdit.SystemFontNameList != null)
- {
- List<string> itemq = defaultEdit.SystemFontNameList.FindAll(p => p.Replace(" ", "").Equals(item.mFontFamily.ToString()));
- if (itemq != null && itemq.Count > 0)
- {
- itemmFontFamily = itemq[0];
- }
- }
- if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
- && (TextEditEvent.FontName == itemmFontFamily || TextEditEvent.FontName == "Arial")
- )
- {
- if (item.mTag != "Custom")
- {
- CurrentPresetFont = new ComboDataItem(item.mTag, item.mTagContent);
- isExist = true;
- }
- break;
- }
- }
- if (isExist == false)
- {
- FontVm.CurrentPresetFont = new ComboDataItem("Custom", "Custom");
- CurrentPresetFont = new ComboDataItem("Custom", "Custom");
- GetCurrentFontSize((int)TextEditEvent.FontSize);
- if (TextEditEvent.FontName == "" || TextEditEvent.FontName == null)
- {
- GetCurrentFontFamily("Arial", "Arial");
- }
- else
- {
- GetCurrentFontFamily(TextEditEvent.FontName.Replace(" ", ""), TextEditEvent.FontName);
- //List<ComboDataItem> itemq = FontFamilyItems.FindAll(p => p.ValueStr == currentItem.mFontFamily.ToString());
- //if (itemq != null && itemq.Count > 0)
- //{
- // GetCurrentFontFamily(TextEditEvent.FontName, itemq[0].Content);
- //}
- }
- GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
- }
- else
- {
- if (CurrentPresetFont != null)
- {
- var itemComboxItem = CurrentPresetFont;
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == itemComboxItem.ValueStr);
- if (item != null)
- {
- if (item.mTag != "Custom")
- {
- GetCurrentFontSize(item.mFontSize);
- if (item.mFontFamily != null)
- {
- if (GetCurrentFontFamily(item) == false)
- {
- GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
- }
- }
- GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
- }
- }
- }
- }
- //初始化传入的对齐方式
- switch (TextEditEvent.TextAlign.ToString())
- {
- case "AlignLeft":
- GetAnnotAlign(TextAlignment.Left);
- break;
- case "AlignCenter":
- GetAnnotAlign(TextAlignment.Center);
- break;
- case "AlignRight":
- GetAnnotAlign(TextAlignment.Right);
- break;
- case "Align":
- GetAnnotAlign(TextAlignment.Justify);
- break;
- }
- TextAlign = TextEditEvent.TextAlign;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- { return true; }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- IsCanSave = false;
- TextEditEvent = null;
- IsSelectedEmpty = false;
- ClearCheckedAglin?.Invoke(null, null);
- PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
- ChangedValue -= FontMode_ChangedValue;
- }
- }
- }
|