using ComPDFKitViewer; using ComPDFKitViewer.PdfViewer; using Microsoft.Win32; using PDF_Office.Model; using PDF_Office.Model.PropertyPanel.AnnotPanel; 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_Office.ViewModels.PropertyPanel { public class TextEditPropertyViewModel : BindableBase, INavigationAware { #region 属性 #region 编辑PDF全局 //多选时,选中的既是文本也是图像 private bool _isSelectTextAndImg = false; public bool IsSelectTextAndImg{get { return _isSelectTextAndImg; } set {SetProperty(ref _isSelectTextAndImg, value);}} //平均对齐布局 private bool _isLayoutAvgAlign = false; public bool IsLayoutAvgAlign { get { return _isLayoutAvgAlign; }set {SetProperty(ref _isLayoutAvgAlign, value); }} //对齐布局 private bool _isLayoutAlign = false; public bool IsLayoutAlign {get { return _isLayoutAlign; }set{SetProperty(ref _isLayoutAlign, value);}} //是否为文本 private bool _isTextEdit = true; public bool IsTextEdit {get { return _isTextEdit; }set{ SetProperty(ref _isTextEdit, value);} } #endregion #region 文本属性 private double _angle; public double Angle{ get { return _angle; }set{ SetProperty(ref _angle, value);}} private Brush selectColor = new SolidColorBrush(Colors.Black); public Brush SelectColor { get { return selectColor; } set{ SetProperty(ref selectColor, value); if (TextEditEvent != null) { bool isok = TextEditEvent.FontColor.A != (SelectColor as SolidColorBrush).Color.A ||TextEditEvent.FontColor.B != (SelectColor as SolidColorBrush).Color.B || TextEditEvent.FontColor.G != (SelectColor as SolidColorBrush).Color.G ||TextEditEvent.FontColor.R != (SelectColor as SolidColorBrush).Color.R; if (isok) { TextEditEvent.FontColor = (SelectColor as SolidColorBrush).Color; TextEditEvent.UpdatePDFEditByEventArgs(); } } } } private FontFamily fontFamily = new FontFamily("Courier"); public FontFamily TextFontFamily { get { return fontFamily; } set { SetProperty(ref fontFamily, value); if (TextEditEvent != null) { TextEditEvent.FontFamily = fontFamily; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private FontWeight fontWeights = FontWeights.Normal; public FontWeight TextFontWeights { get { return fontWeights; } set { SetProperty(ref fontWeights, value); if (TextEditEvent != null) { TextEditEvent.FontWeight = fontWeights; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private FontStyle fontStyle = FontStyles.Normal; public FontStyle TextFontStyle { get { return fontStyle; } set { SetProperty(ref fontStyle, value); if (TextEditEvent != null) { TextEditEvent.FontStyle = fontStyle; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private int fontSize = 24; public int TextFontSize { get { return fontSize; } set { SetProperty(ref fontSize, value); if (TextEditEvent != null) { TextEditEvent.FontSize = fontSize; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private List fontStyleList = new List(); public List FontStyleList { get { return fontStyleList; } set { SetProperty(ref fontStyleList, value); } } #endregion #region 图像属性 private bool _isCrop = false; public bool IsCrop { get { return _isCrop; } set { SetProperty(ref _isCrop, value); } } //选中的图像 private System.Windows.Media.Imaging.BitmapSource _currentImg; public System.Windows.Media.Imaging.BitmapSource CurrentImg { get { return _currentImg; } set { SetProperty(ref _currentImg, value); } } #endregion #endregion #region Command #region 全局 public event EventHandler ClearCheckedAglin; #endregion #region 文本Command public DelegateCommand AddTextCommand { get; set; } public DelegateCommand AddImgCommand { get; set; } public DelegateCommand SelectedColorCommand { get; set; } public DelegateCommand SelectedFontStyleCommand { get; set; } public DelegateCommand FontFamilyChangedCommand { get; set; } public DelegateCommand FontStyleChangedCommand { get; set; } public DelegateCommand FontSizeChangedCommand { get; set; } public DelegateCommand TextAlignCheckedCommand { get; set; } public DelegateCommand EditTextModeCommand { get; set; } #endregion #region 图像Command public DelegateCommand ReplaceImgCommand { get; set; } public DelegateCommand ExportImgCommand { get; set; } public DelegateCommand CropImgCommand { get; set; } public DelegateCommand ImgAlignCheckedCommand { get; set; } /// /// 逆时针旋转 /// public DelegateCommand AntiClockwiseCommand { get; set; } /// /// 顺时针旋转 /// public DelegateCommand ClockwiseCommand { get; set; } public DelegateCommand CropModeCommand { get; set; } public DelegateCommand CancelCropCommand { get; set; } #endregion #endregion #region 变量 private PDFEditEvent TextEditEvent; private CPDFViewer PDFViewer; #endregion #region 初始化 public TextEditPropertyViewModel() { InitVariable(); InitCommand(); } private void InitVariable() { InitFontStyles(); } private void InitFontStyles() { FontStyleList = LoadFontStyle.Load(); } private void InitCommand() { AddTextCommand = new DelegateCommand(AddText); AddImgCommand = new DelegateCommand(AddImg); ReplaceImgCommand = new DelegateCommand(ReplaceImg); ExportImgCommand = new DelegateCommand(ExportImg); CropImgCommand = new DelegateCommand(CropImg); ImgAlignCheckedCommand = new DelegateCommand(ImgAlignChecked); SelectedColorCommand = new DelegateCommand(SelectedColor); SelectedFontStyleCommand = new DelegateCommand(SelectedFontStyle); FontFamilyChangedCommand = new DelegateCommand(FontFamilyChanged); FontStyleChangedCommand = new DelegateCommand(FontStyleChanged); FontSizeChangedCommand = new DelegateCommand(FontSizeChanged); TextAlignCheckedCommand = new DelegateCommand(TextAlignChecked); EditTextModeCommand = new DelegateCommand(EditTextMode); AntiClockwiseCommand = new DelegateCommand(AntiClockwise); ClockwiseCommand = new DelegateCommand(Clockwise); CropModeCommand = new DelegateCommand(CropMode); CancelCropCommand = new DelegateCommand(CancelCropImg); } #endregion #region 文本处理 private void AddText() { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText); } private void EditTextMode() { } 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; } TextEditEvent.UpdatePDFEditByEventArgs(); } } private void Clockwise() { Angle = TextEditEvent.Rotate = TextEditEvent.Rotate + 90; TextEditEvent.UpdatePDFEditByEventArgs(); } private void AntiClockwise() { Angle = TextEditEvent.Rotate = TextEditEvent.Rotate - 90; 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 && (FontStyleItem)obj != null) { var item = (FontStyleItem)obj; } } private void FontFamilyChanged(object obj) { if (obj != null && (int)obj > -1) { switch ((int)obj) { case 0: TextFontFamily = new FontFamily("Courier"); break; case 1: TextFontFamily = new FontFamily("Helvetica"); break; case 2: TextFontFamily = new FontFamily("Times"); break; } } } private void FontStyleChanged(object obj) { if (obj != null && (ComboBoxItem)obj != null) { var item = (ComboBoxItem)obj; var content = (string)item.Content; if (content != null) { switch (content) { case "Regular": TextFontWeights = FontWeights.Normal; TextFontStyle = FontStyles.Normal; break; case "Bold": TextFontWeights = FontWeights.Bold; TextFontStyle = FontStyles.Normal; break; case "Italic": TextFontWeights = FontWeights.Normal; TextFontStyle = FontStyles.Italic; break; case "Bold Italic": TextFontWeights = FontWeights.Bold; TextFontStyle = FontStyles.Italic; break; } } } } 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 CancelCropImg() { if (TextEditEvent != null) { TextEditEvent.ClipImage = false; TextEditEvent.CancelClip(); TextEditEvent.UpdatePDFEditByEventArgs(); IsCrop = false; } } private void CropImg() { if (TextEditEvent != null) { TextEditEvent.SaveClip(); TextEditEvent.ClipImage = false; TextEditEvent.UpdatePDFEditByEventArgs(); IsCrop = false; } } private void ExportImg() { if (PDFViewer == null || TextEditEvent == null || TextEditEvent.EditType != ComPDFKit.PDFPage.CPDFEditType.EditImage) return; System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog(); folder.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (string.IsNullOrEmpty(folder.SelectedPath)) return; var keyValueList = PDFViewer.GetSelectedImages(); int i = 0; foreach (var bitmap in keyValueList) { foreach (var bitmapItem in bitmap.Value) { bitmapItem.Save(folder.SelectedPath + "\\" + i + ".png", System.Drawing.Imaging.ImageFormat.Png); i++; } } } } private void AddImg() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;"; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { if (string.IsNullOrEmpty(openFileDialog.FileName) == false) { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); PDFViewer.AddPDFEditImage(openFileDialog.FileName); } } } private void ReplaceImg() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;"; openFileDialog.Multiselect = true; if ((bool)openFileDialog.ShowDialog()) { if (string.IsNullOrEmpty(openFileDialog.FileName) == false) { PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage); TextEditEvent.ReplaceImagePath = openFileDialog.FileName; TextEditEvent.UpdatePDFEditByEventArgs(); } } } private void CropMode() { IsCrop = true; if (TextEditEvent != null) { TextEditEvent.ClipImage = true; TextEditEvent.UpdatePDFEditByEventArgs(); } } #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; if (popMenu != null && popMenu.Items.Count == 3) { //粘贴 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Paste); //添加文本 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, AddTextCommand); //添加图像 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, AddImgCommand); } return popMenu; } //选中图像时 private ContextMenu SelectImgPDFEdit(object sender) { var popMenu = App.Current.FindResource("SelectImgMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 7) { //复制 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy); //剪切 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut); //粘贴 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste); //删除 SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Delete); //裁剪 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, CropModeCommand); //替换 SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ReplaceImgCommand); //导出 SetPopMenuItem(popMenu.Items[6] as MenuItem, sender, ExportImgCommand); } return popMenu; } //选中文字的框 private ContextMenu SelectTextBorder(object sender) { var popMenu = App.Current.FindResource("SelectTextMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 5) { //编辑 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, EditTextModeCommand); //复制 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Copy); //剪切 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Cut); //粘贴 SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Paste); //删除 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete); } return popMenu; } //选中文字内容 private ContextMenu SelectTextContent(object sender) { var popMenu = App.Current.FindResource("SelectContentMenu") as ContextMenu; if (popMenu != null && popMenu.Items.Count == 6) { //复制 SetPopMenuItem(popMenu.Items[0] as MenuItem, sender, ApplicationCommands.Copy); //剪切 SetPopMenuItem(popMenu.Items[1] as MenuItem, sender, ApplicationCommands.Cut); //粘贴 SetPopMenuItem(popMenu.Items[2] as MenuItem, sender, ApplicationCommands.Paste); //粘贴并匹配样式 SetPopMenuItem(popMenu.Items[3] as MenuItem, sender, ApplicationCommands.Paste); //删除 SetPopMenuItem(popMenu.Items[4] as MenuItem, sender, ApplicationCommands.Delete); ////全部选定 SetPopMenuItem(popMenu.Items[5] as MenuItem, sender, ApplicationCommands.SelectAll); } return popMenu; } private void SetPopMenuItem(MenuItem menu, object sender, ICommand command) { MenuItem menuItem = menu; menuItem.CommandTarget = (UIElement)sender; menuItem.Command = command; } #endregion #region 编辑PDF内容触发的事件 /// /// 右键菜单的事件 /// 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.EditImage) { e.PopupMenu = SelectImgPDFEdit(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; } } /// /// 选中编辑PDF内容的事件 /// private void PDFViewer_PDFEditActiveHandler(object sender, List e) { if (e != null && e.Count > 0) { ReLoadLayoutAlign(e.Count); IsTextEdit = (e[0].EditType == ComPDFKit.PDFPage.CPDFEditType.EditText); TextEditEvent = e[0]; if (IsTextEdit == false) { var list = PDFViewer.GetSelectedImages(); if (list != null && list.Count > 0) { System.Drawing.Bitmap bitmap = null; foreach (var item in list) { if (item.Value.Count > 0) { bitmap = item.Value[0]; break; } } if (bitmap != null) { IntPtr ip = bitmap.GetHbitmap(); System.Windows.Media.Imaging.BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); CurrentImg = bitmapSource; } } } bool isText = false; bool isImg = false; foreach (var item in e) { if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText) { isText = true; } if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage) { isImg = true; } } if (isImg == true && isText == true) IsSelectTextAndImg = true; else IsSelectTextAndImg = false; } else { IsLayoutAlign = false; IsLayoutAvgAlign = false; IsSelectTextAndImg = false; IsTextEdit = true; ClearCheckedAglin?.Invoke(null, null); } } #endregion public void OnNavigatedTo(NavigationContext navigationContext) { navigationContext.Parameters.TryGetValue(ParameterNames.PDFViewer, out PDFViewer); if (PDFViewer != null) { PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler; PDFViewer.PDFEditCommandHandler -= PDFViewer_PDFEditCommandHandler; PDFViewer.PDFEditCommandHandler += PDFViewer_PDFEditCommandHandler; } } public bool IsNavigationTarget(NavigationContext navigationContext){ return true; } public void OnNavigatedFrom(NavigationContext navigationContext){} } }