AddAnnotationDialogViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using DryIoc;
  5. using Microsoft.Office.Interop.Word;
  6. using PDF_Master.Helper;
  7. using PDF_Master.Model;
  8. using PDF_Master.Model.BOTA;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using Prism.Services.Dialogs;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Annotations;
  20. using System.Windows.Controls;
  21. using System.Windows.Input;
  22. using System.Windows.Markup;
  23. using System.Windows.Media;
  24. namespace PDF_Master.ViewModels.Dialog.BOTA
  25. {
  26. public class AddAnnotationDialogViewModel : BindableBase, IDialogAware
  27. {
  28. private string title = "";
  29. public string Title
  30. {
  31. get { return title; }
  32. set { SetProperty(ref title, value); }
  33. }
  34. public event Action<IDialogResult> RequestClose;
  35. //private string markupContent;
  36. //public string MarkupContent
  37. //{
  38. // get { return markupContent; }
  39. // set
  40. // {
  41. // SetProperty(ref markupContent, value);
  42. // }
  43. //}
  44. private string contentText;
  45. public string ContentText
  46. {
  47. get { return contentText; }
  48. set
  49. {
  50. SetProperty(ref contentText, value);
  51. }
  52. }
  53. private string placeHolderText;
  54. public string PlaceHolderText
  55. {
  56. get { return placeHolderText; }
  57. set
  58. {
  59. SetProperty(ref placeHolderText, value);
  60. }
  61. }
  62. private string btnOKContent;
  63. public string BtnOKContent
  64. {
  65. get { return btnOKContent; }
  66. set
  67. {
  68. SetProperty(ref btnOKContent, value);
  69. }
  70. }
  71. private string btnCancelContent;
  72. public string BtnCancelContent
  73. {
  74. get { return btnCancelContent; }
  75. set
  76. {
  77. SetProperty(ref btnCancelContent, value);
  78. }
  79. }
  80. private string annotationName;
  81. public string AnnotationName
  82. {
  83. get { return annotationName; }
  84. set
  85. {
  86. SetProperty(ref annotationName, value);
  87. }
  88. }
  89. private Visibility tipVisibility = Visibility.Collapsed;
  90. public Visibility TipVisibility
  91. {
  92. get { return tipVisibility; }
  93. set
  94. {
  95. SetProperty(ref tipVisibility, value);
  96. }
  97. }
  98. public AnnotationHandlerEventArgs Annotation { get; set; }
  99. public AnnotAttribEvent AnnotEvent { get; set; }
  100. public DelegateCommand CreateCommnad { get; set; }
  101. public DelegateCommand CancelCommand { get; set; }
  102. public DelegateCommand<object> LostFocusCommand { get; set; }
  103. public DelegateCommand<object> KeyDownCommand { get; set; }
  104. public AddAnnotationDialogViewModel()
  105. {
  106. CancelCommand = new DelegateCommand(CancelEvent);
  107. CreateCommnad = new DelegateCommand(CreateEvent);
  108. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  109. KeyDownCommand = new DelegateCommand<object>(KeyDown);
  110. SetLange();
  111. }
  112. private void SetLange()
  113. {
  114. PlaceHolderText = App.MainPageLoader.GetString("PlaceText_AddNote");
  115. BtnOKContent = App.ServiceLoader.GetString("Text_ok");
  116. BtnCancelContent = App.ServiceLoader.GetString("Text_cancel");
  117. }
  118. private void KeyDown(object obj)
  119. {
  120. if (obj is CompositeCommandParameter composite)
  121. {
  122. if (composite.EventArgs is System.Windows.Input.KeyEventArgs eventArgs)
  123. {
  124. if (eventArgs.Key == Key.Enter)
  125. {
  126. LostFocusEvent(obj);
  127. }
  128. }
  129. }
  130. }
  131. private void LostFocusEvent(object obj)
  132. {
  133. if (obj is CompositeCommandParameter composite)
  134. {
  135. if (composite.Parameter is TextBox textBox)
  136. {
  137. //Annotation.MarkupContent = textBox.Text;
  138. AnnotEvent?.UpdateAttrib(AnnotAttrib.NoteText, textBox.Text);
  139. AnnotEvent?.UpdateAnnot();
  140. }
  141. }
  142. }
  143. private void CreateEvent()
  144. {
  145. DialogParameters valuePairs = new DialogParameters();
  146. valuePairs.Add(ParameterNames.Annotation, Annotation);
  147. valuePairs.Add(ParameterNames.AnnotEvent, AnnotEvent);
  148. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  149. }
  150. private void CancelEvent()
  151. {
  152. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  153. }
  154. public bool CanCloseDialog()
  155. {
  156. return true;
  157. }
  158. public void OnDialogClosed()
  159. {
  160. }
  161. public void OnDialogOpened(IDialogParameters parameters)
  162. {
  163. AnnotationHandlerEventArgs annotation;
  164. parameters.TryGetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation, out annotation);
  165. if (annotation != null)
  166. {
  167. Annotation = annotation;
  168. Dictionary<AnnotAttrib, object> annotAttribsList = annotation.AnnotHandlerEventArgs.GetAnnotAttrib();
  169. AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annotation.AnnotHandlerEventArgs, annotAttribsList);
  170. ContentText = annotation.Content;
  171. AnnotationName = "Page" + (annotation.PageIndex + 1).ToString();
  172. if (string.IsNullOrEmpty(ContentText))
  173. {
  174. TipVisibility = Visibility.Visible;
  175. }
  176. else
  177. {
  178. TipVisibility = Visibility.Collapsed;
  179. }
  180. return;
  181. }
  182. }
  183. }
  184. }