TextEditToolContentViewModel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.EditImage);
  75. }
  76. }
  77. else
  78. {
  79. PDFViewer.SetPDFEditCreateType(ComPDFKit.PDFPage.CPDFEditType.None);
  80. PDFViewer.SetMouseMode(MouseModes.PDFEdit);
  81. }
  82. }
  83. public ViewContentViewModel viewContentViewModel;
  84. private CPDFViewer PDFViewer;
  85. public void OnNavigatedTo(NavigationContext navigationContext)
  86. {
  87. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  88. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  89. if (PDFViewer != null)
  90. {
  91. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  92. PDFViewer.PDFEditActiveHandler += PDFViewer_PDFEditActiveHandler;
  93. }
  94. }
  95. private void PDFViewer_PDFEditActiveHandler(object sender, List<PDFEditEvent> e)
  96. {
  97. if (e != null && e.Count > 0)
  98. {
  99. bool isText = false;
  100. bool isImg = false;
  101. foreach (var item in e)
  102. {
  103. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditImage)
  104. {
  105. isImg = true;
  106. }
  107. if (item.EditType == ComPDFKit.PDFPage.CPDFEditType.EditText)
  108. {
  109. isText = true;
  110. }
  111. }
  112. if (isText == true && isImg == false)
  113. {
  114. AddToPropertyPanel("Text", e);
  115. }
  116. if (isText == false && isImg == true)
  117. {
  118. AddToPropertyPanel("Image", e);
  119. }
  120. if (isText == true && isImg == true)
  121. {
  122. AddToPropertyPanel("TextAndImage", e);
  123. }
  124. }
  125. else
  126. {
  127. AddToPropertyPanel("PropertyPanelContent", null);
  128. }
  129. }
  130. public bool IsNavigationTarget(NavigationContext navigationContext)
  131. {
  132. return true;
  133. }
  134. public void OnNavigatedFrom(NavigationContext navigationContext)
  135. {
  136. PDFViewer.PDFEditActiveHandler -= PDFViewer_PDFEditActiveHandler;
  137. ShowPropertyPanel(false);
  138. }
  139. }
  140. }