TextEditToolContentViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media;
  9. using System.Windows;
  10. using PDF_Office.Model.PropertyPanel.AnnotPanel;
  11. using System.Windows.Controls;
  12. using Prism.Regions;
  13. using ComPDFKitViewer.PdfViewer;
  14. using PDF_Office.Model;
  15. using Microsoft.Win32;
  16. using PDF_Office.CustomControl;
  17. using ComPDFKitViewer;
  18. namespace PDF_Office.ViewModels.Tools
  19. {
  20. public class TextEditToolContentViewModel: BindableBase, INavigationAware
  21. {
  22. #region Command
  23. public DelegateCommand<object> AddContentCommand { get; set; }
  24. #endregion
  25. private IRegionManager regions;
  26. private Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  27. public TextEditToolContentViewModel(IRegionManager regionManager)
  28. {
  29. regions = regionManager;
  30. InitCommand();
  31. InitBtnToProperty();
  32. }
  33. private void InitBtnToProperty()
  34. {
  35. btnToProperty.Add("Text", "TextEditProperty");
  36. btnToProperty.Add("Image", "ImageEditProperty");
  37. btnToProperty.Add("TextAndImage", "ImageTextEditProperty");
  38. btnToProperty.Add("PropertyPanelContent", "PropertyPanelContent");
  39. }
  40. private void AddToPropertyPanel(string type, List<PDFEditEvent> e)
  41. {
  42. if (btnToProperty.ContainsKey(type))
  43. {
  44. NavigationParameters parameters = new NavigationParameters();
  45. parameters.Add(ParameterNames.PDFViewer, PDFViewer);
  46. parameters.Add(ParameterNames.AnnotEvent, e);
  47. System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
  48. {
  49. regions.RequestNavigate(RegionNames.PropertyRegionName, btnToProperty[type], parameters);
  50. }));
  51. ShowPropertyPanel(true);
  52. }
  53. }
  54. private void ShowPropertyPanel(bool show = true)
  55. {
  56. viewContentViewModel.IsPropertyOpen = show;
  57. }
  58. private void InitCommand()
  59. {
  60. AddContentCommand = new DelegateCommand<object>(AddContent);
  61. }
  62. public void AddContent(object obj)
  63. {
  64. if (PDFViewer == null || obj == null || obj as CustomIconToggleBtn == null) return;
  65. var btn = obj as CustomIconToggleBtn;
  66. if(btn.IsChecked == true)
  67. {
  68. if (btn.Tag.ToString() == "Text")
  69. {
  70. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditText);
  71. }
  72. else
  73. {
  74. OpenFileDialog openFileDialog = new OpenFileDialog();
  75. openFileDialog.Filter = "png|*.png;|Image|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;";
  76. openFileDialog.Multiselect = true;
  77. if ((bool)openFileDialog.ShowDialog())
  78. {
  79. if (string.IsNullOrEmpty(openFileDialog.FileName) == false)
  80. {
  81. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  82. PDFViewer.AddPDFEditImage(openFileDialog.FileName);
  83. }
  84. }
  85. btn.IsChecked = false;
  86. }
  87. }
  88. else
  89. {
  90. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  91. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  92. }
  93. }
  94. public ViewContentViewModel viewContentViewModel;
  95. private CPDFViewer PDFViewer;
  96. public void OnNavigatedTo(NavigationContext navigationContext)
  97. {
  98. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  99. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  100. if (PDFViewer != null)
  101. {
  102. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  103. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  104. }
  105. }
  106. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  107. {
  108. if (e != null && e.Count > 0)
  109. {
  110. bool isText = false;
  111. bool isImg = false;
  112. foreach (var item in e)
  113. {
  114. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  115. {
  116. isImg = true;
  117. }
  118. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  119. {
  120. isText = true;
  121. }
  122. }
  123. if (isText == true && isImg == false)
  124. {
  125. AddToPropertyPanel("Text", e);
  126. }
  127. if (isText == false && isImg == true)
  128. {
  129. AddToPropertyPanel("Image", e);
  130. }
  131. if (isText == true && isImg == true)
  132. {
  133. AddToPropertyPanel("TextAndImage", e);
  134. }
  135. }
  136. else
  137. {
  138. AddToPropertyPanel("PropertyPanelContent", null);
  139. }
  140. }
  141. public bool IsNavigationTarget(NavigationContext navigationContext)
  142. {
  143. return true;
  144. }
  145. public void OnNavigatedFrom(NavigationContext navigationContext)
  146. {
  147. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  148. ShowPropertyPanel(false);
  149. }
  150. }
  151. }