123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- using ComPDFKitViewer;
- 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 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
- private bool isSelectedEmpty;
- public bool IsSelectedEmpty
- {
- get { return isSelectedEmpty; }
- set { SetProperty(ref isSelectedEmpty, value); }
- }
- private ComPDFKit.PDFPage.Edit.TextAlignType _textAlign;
- public ComPDFKit.PDFPage.Edit.TextAlignType TextAlign
- {
- get { return _textAlign; }
- set { _textAlign = value; }
- }
- #endregion
- #region Command
- #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 CustomFontStyleCommand { get; set; }
- public DelegateCommand ReDefineFontStyleCommand { get; set; }
- public DelegateCommand RestoreDefaultStyleCommand { get; set; }
- public DelegateCommand<object> LayoutAlignCheckedCommand { get; set; }
- #endregion
- #endregion
- #region 初始化
- public TextEditPropertyViewModel()
- {
- InitVariable();
- InitCommand();
- }
- private void InitVariable()
- {
- InitBaseVariable();
- }
- private void InitCommand()
- {
- 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_Click);
- 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()
- {
- }
- private void CustomFontStyle_Click()
- {
- if(CurrentPresetFont != null)
- {
- ContextMenu menu;
- if (CurrentPresetFont.ValueStr == "custom")
- {
- menu = SelectAnnotContextMenu(false);
- }
- else
- {
- menu = SelectAnnotContextMenu(true);
- }
-
- if (menu != null)
- {
- menu.IsOpen = true;
- }
- }
-
- }
- private void ReDefineFontStyle()
- {
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- if (CurrentFontFamily.ValueStr == "Bold")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Bold;
- }
- else if (CurrentFontFamily.ValueStr == "Regular")
- {
- item.mFontStyle = FontStyles.Normal;
- item.mFontWeight = FontWeights.Normal;
- }
- else
- {
- item.mFontStyle = FontStyles.Italic;
- item.mFontWeight = FontWeights.Bold;
- }
- item.mFontSize = (int)CurrentFontSize.Value;
- }
- private void RestoreDefaultStyle()
- {
- var item = PresetFontList.FirstOrDefault(temp => temp.mTag == CurrentPresetFont.ValueStr);
- }
- 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":
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignLeft;
- break;
- case "AlignCenter":
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignMiddle;
- break;
- case "AlignRight":
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignRight;
- break;
- case "Align":
- TextEditEvent.TextAlign = ComPDFKit.PDFPage.Edit.TextAlignType.AlignNone;
- break;
- }
- TextAlign = TextEditEvent.TextAlign;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- }
- 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 (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);
-
- customMenu.SetVisibilityProperty(6, false);
- 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;
- }
- #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);
- }
- else if (e.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
- {
- //文字编辑
- if (e.PressOnBorder == true)
- {
- e.PopupMenu = SelectTextBorder(sender);
- }
- if (e.PressOnSelectedText == true)
- {
- e.PopupMenu = SelectTextContent(sender);
- }
- }
- break;
- default:
- e.DoCommand();
- break;
- }
- if (e.PopupMenu != null)
- {
- e.Handle = 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;
- }
- #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.FontFamily = item.mFontFamily;
- TextEditEvent.FontSize = item.mFontSize;
- TextEditEvent.FontWeight = item.mFontWeight;
- TextEditEvent.FontStyle = item.mFontStyle;
- TextEditEvent.UpdatePDFEditByEventArgs();
- }
- else
- {
- foreach (var itemlist in TextEditEventList)
- {
- itemlist.FontFamily = item.mFontFamily;
- itemlist.FontSize = item.mFontSize;
- itemlist.FontWeight = item.mFontWeight;
- itemlist.FontStyle = item.mFontStyle;
- itemlist.UpdatePDFEditByEventArgs();
- }
- }
-
- }
- }
- break;
- case FontSetModeType.FontFamilys:
- if(sender is string == true)
- {
- TextEditEvent.FontFamily = new FontFamily((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.FontWeight = FontWeightItem;
- TextEditEvent.FontStyle = FontStyleItem;
- 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()
- {
- if (TextEditEvent != null && TextEditEventList.Count == 1)
- {
- SelectColor = new SolidColorBrush(TextEditEvent.FontColor);
- GetCurrentFontSize((int)TextEditEvent.FontSize);
- if(TextEditEvent.FontFamily != null)
- GetCurrentFontFamily(TextEditEvent.FontFamily.ToString(), TextEditEvent.FontFamily.ToString());
-
- GetFontWeights_Style(TextEditEvent.FontStyle, TextEditEvent.FontWeight);
- 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;
- }
- }
- }
|