12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- 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.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.Input;
- using System.Windows.Media;
- namespace PDF_Master.ViewModels.PropertyPanel.PDFEdit
- {
- public class TextEditPropertyViewModel : PDFEditVM, INavigationAware
- {
- #region 属性
- 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
- #region Command初始化
-
- 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());
- }
- 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>
- private void CustomFontStyle()
- {
- if (CurrentPresetFont != null)
- {
- ContextMenu menu;
- if (CurrentPresetFont.ValueStr == "Custom")
- {
- menu = SelectAnnotContextMenu(false);
- }
- else
- {
- menu = SelectAnnotContextMenu(false);
- var currentItem = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (currentItem.mTag != "Custom")
- {
- if (FontWeightItem != currentItem.mFontWeight ||
- FontStyleItem != currentItem.mFontStyle ||
- CurrentFontSize.Value != currentItem.mFontSize ||
- CurrentFontFamily.ValueStr != currentItem.mFontFamily.Source)
- {
- menu = SelectAnnotContextMenu(true);
- }
- }
- }
- if (menu != null)
- {
- menu.IsOpen = true;
- }
- }
- }
- //设置字体样式
- private void FontFamilyChanged()
- {
- if (string.IsNullOrEmpty(CurrentFontFamily.ValueStr) == false)
- {
-
- //GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
- }
- 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)
- {
- 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;
- }
- }
- }
- }
- }
- else
- {
- //bool isExist = GetCurrentPresetFont(Annot);
- //if (isExist)
- //{
- //}
- }
- }
- }
- /// <summary>
- /// 设置文本字重、样式
- /// </summary>
- private void FontStyleWeightChanged()
- {
- if (CurrrentFontWeightStyle != null)
- {
- UpdateFontWeight_Style();
- FontStyleItem = TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal;
- FontWeightItem = TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal;
- //Dictionary<AnnotAttrib, object> AnnotAttribDir = new Dictionary<AnnotAttrib, object>();
- //AnnotAttribDir.Add(AnnotAttrib.IsItalic,FontStyleItem == FontStyles.Italic);
- //AnnotAttribDir.Add(AnnotAttrib.IsBold, FontWeightItem == FontWeights.Bold);
- //PropertyPanel.UpdateAnnotAllAttribs(AnnotAttribDir);
- //GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, 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;
- }
- }
- }
- }
- }
- else
- {
- //bool isExist = GetCurrentPresetFont(Annot);
- //if (isExist)
- //{
- //}
- }
- }
- }
- }
- /// <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();
-
- }
- //重置定义
- private void RestoreDefaultStyle()
- {
- //var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- 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());
- GetCurrentFontSize(currentItem.mFontSize);
- GetFontWeights_Style(currentItem.mFontStyle, currentItem.mFontWeight);
- CurrentPresetFont = new ComboDataItem(defaulItem.mTag, defaulItem.mTagContent);
- SelectedPresetFont();
- }
- }
- }
- }
- 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);
- //GetCurrentFontSize((int)CurrentFontSize.Value);
- //PropertyPanel.UpdateAnnotAAttrib(AnnotAttrib.FontSize,CurrentFontSize.Value);
- }
- 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;
- }
- }
- }
- }
- }
- else
- {
- //bool isExist = GetCurrentPresetFont(Annot);
- //if (isExist)
- //{
- //}
- }
- }
- //if (obj != null)
- //{
- // var item = (ComboBoxItem)obj;
- // var content = (string)item.Content;
- // if (content != null)
- // {
- // var intData = int.Parse(content);
- // //TextFontSize = intData;
- // }
- //}
- }
- #endregion
- #region 布局处理
- private void ImgAlignChecked(object obj)
- {
- 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 ReLoadLayoutAlign(int count)
- {
- if (count >= 2)
- IsLayoutAlign = true;
- else
- IsLayoutAlign = false;
- if (count >= 3)
- IsLayoutAvgAlign = true;
- else
- IsLayoutAvgAlign = false;
- }
- #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;
- }
- //选中文字的框
- private ContextMenu SelectTextBorder(object sender)
- {
- var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //编辑
- customMenu.SetMenuBinding(0, EditTextModeCommand);
- //复制
- customMenu.SetMenuBinding(1, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(2, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(3, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- return popMenu;
- }
- //多选文本框
- private ContextMenu SelectMoreText(object sender)
- {
- var popMenu = App.Current.FindResource("SelectMoreTextMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //复制
- customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //删除
- customMenu.SetMenuBinding(3, ApplicationCommands.Delete);
- return popMenu;
- }
- //选中文字内容
- private ContextMenu SelectTextContent(object sender)
- {
- var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- //复制
- customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- //剪切
- customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //粘贴
- customMenu.SetMenuBinding(2, ApplicationCommands.Paste);
- //粘贴并匹配样式
- customMenu.SetVisibilityProperty(3, false);
- //删除
- customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- //全部选定
- customMenu.SetMenuBinding(5, ApplicationCommands.SelectAll);
- return popMenu;
- }
- //编辑中右键
- private ContextMenu EditTextContent(object sender)
- {
- var popMenu = App.Current.FindResource("NoneSelectContentMenu") as ContextMenu;
- CustomPopMenu customMenu = new CustomPopMenu(popMenu, sender);
- ////复制
- //customMenu.SetMenuBinding(0, ApplicationCommands.Copy);
- ////剪切
- //customMenu.SetMenuBinding(1, ApplicationCommands.Cut);
- //全部选定
- customMenu.SetMenuBinding(0, ApplicationCommands.SelectAll);
- //粘贴
- customMenu.SetMenuBinding(1, ApplicationCommands.Paste);
- //粘贴并匹配样式
- customMenu.SetVisibilityProperty(2, false);
- ////删除
- //customMenu.SetMenuBinding(4, ApplicationCommands.Delete);
- 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.EditText)
- // {
- // //文字编辑
- // if (e.PressOnBorder == true)
- // {
- // e.PopupMenu = SelectTextBorder(sender);
- // }
- // else if(e.PressOnSelectedText == true)
- // {
- // e.PopupMenu = SelectTextContent(sender);
- // }
- // else if(e.SelectText=="")
- // {
- // e.PopupMenu= EditTextContent(sender);
- // }
- // else
- // {
- // e.PopupMenu = SelectTextContent(sender);
- // }
- // }
- //else
- 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.IsEnabled = isEnable;
- menuItem.Command = ReDefineFontStyleCommand;
- //恢复默认预设样式
- menuItem = popMenu.Items[1] as MenuItem;
- menuItem.IsEnabled = isEnable;
- menuItem.Command = RestoreDefaultStyleCommand;
- }
- return popMenu;
- }
- #region 全局
- public event EventHandler ClearCheckedAglin;
- #endregion
- #endregion
- 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];
- 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;
- }
- }
- PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler;
- PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler;
- ChangedValue -= FontMode_ChangedValue;
- ChangedValue += FontMode_ChangedValue;
- IsCanSave = true;
- }
- }
- private void FontMode_ChangedValue(object sender, FontSetModeType e)
- {
- if (sender != null)
- {
- 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();
- }
- }
- }
- }
- break;
- case FontSetModeType.FontFamilys:
- if (sender is string == true)
- {
- TextEditEvent.FontName = (string)sender;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- break;
- case FontSetModeType.FontSizes:
- if (sender is double == true && (double)sender > 0 && TextEditEvent.FontSize > 0)
- {
- TextEditEvent.FontSize = (double)sender;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- break;
- case FontSetModeType.FontWeight_Style:
- UpdateFontWeight_Style();
- TextEditEvent.IsBold = FontWeightItem == FontWeights.Bold;
- TextEditEvent.IsItalic = FontStyleItem == FontStyles.Italic;
- TextEditEvent.UpdatePDFEditByEventArgs();
- 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();
- }
- }
- break;
- case FontSetModeType.TextAlignment:
- break;
- }
- }
- }
- private void GetPDFEdit()
- {
- 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;
- GetFontWeights_Style(FontStyleItem, FontWeightItem);
-
- bool isExist = false;
- //List<PresetFontItem> presetFontItems = TextFont.GetCachePresetFontList();
- //遍历样式
- foreach (var item in PresetFontList)
- {
- if (TextEditEvent.FontSize == item.mFontSize && TextEditEvent.IsBold == (item.mFontWeight == FontWeights.Bold) && TextEditEvent.IsItalic == (item.mFontStyle == FontStyles.Italic)
- && (TextEditEvent.FontName == item.mFontFamily.Source || TextEditEvent.FontName == "Arial" && item.mFontFamily.Source == "Helvetica")
- )
- {
- 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("Helvetica", "Helvetica");
- }
- else
- {
- GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
- }
- 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")
- {
- //CurrentFontSize = new ComboDataItem(TextEditEvent.FontSize);
- GetCurrentFontSize(item.mFontSize);
- if (item.mFontFamily != null)
- {
- //CurrentFontFamily = new ComboDataItem(TextEditEvent.FontName, TextEditEvent.FontName);
- GetCurrentFontFamily(item.mFontFamily.ToString(), item.mFontFamily.ToString());
- }
- //FontStyleItem = item.mFontStyle;
- //FontWeightItem = item.mFontWeight;
- 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();
-
- //if (TextEditEvent != null && TextEditEventList.Count == 1)
- //{
- // SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
- // GetCurrentFontSize((int)TextEditEvent.FontSize);
- // if (TextEditEvent.FontName != null)
- // {
- // GetCurrentFontFamily(TextEditEvent.FontName, TextEditEvent.FontName);
- // }
- // GetFontWeights_Style(TextEditEvent.IsItalic ? FontStyles.Italic : FontStyles.Normal, TextEditEvent.IsBold ? FontWeights.Bold : FontWeights.Normal);
- // TextAlign = TextEditEvent.TextAlign;
- //}
- }
- 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;
- }
- }
- }
|