AddAnnotationDialogViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 annotationName;
  54. public string AnnotationName
  55. {
  56. get { return annotationName; }
  57. set
  58. {
  59. SetProperty(ref annotationName, value);
  60. }
  61. }
  62. private Visibility tipVisibility = Visibility.Collapsed;
  63. public Visibility TipVisibility
  64. {
  65. get { return tipVisibility; }
  66. set
  67. {
  68. SetProperty(ref tipVisibility, value);
  69. }
  70. }
  71. public AnnotationHandlerEventArgs Annotation { get; set; }
  72. public AnnotAttribEvent AnnotEvent { get; set; }
  73. public DelegateCommand CreateCommnad { get; set; }
  74. public DelegateCommand CancelCommand { get; set; }
  75. public DelegateCommand<object> LostFocusCommand { get; set; }
  76. public DelegateCommand<object> KeyDownCommand { get; set; }
  77. public AddAnnotationDialogViewModel()
  78. {
  79. CancelCommand = new DelegateCommand(CancelEvent);
  80. CreateCommnad = new DelegateCommand(CreateEvent);
  81. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  82. KeyDownCommand = new DelegateCommand<object>(KeyDown);
  83. }
  84. private void KeyDown(object obj)
  85. {
  86. if (obj is CompositeCommandParameter composite)
  87. {
  88. if (composite.EventArgs is System.Windows.Input.KeyEventArgs eventArgs)
  89. {
  90. if (eventArgs.Key == Key.Enter)
  91. {
  92. LostFocusEvent(obj);
  93. }
  94. }
  95. }
  96. }
  97. private void LostFocusEvent(object obj)
  98. {
  99. if (obj is CompositeCommandParameter composite)
  100. {
  101. if (composite.Parameter is TextBox textBox)
  102. {
  103. Annotation.Content = textBox.Text;
  104. AnnotEvent?.UpdateAttrib(AnnotAttrib.NoteText, textBox.Text);
  105. AnnotEvent?.UpdateAnnot();
  106. }
  107. }
  108. }
  109. private void CreateEvent()
  110. {
  111. DialogParameters valuePairs = new DialogParameters();
  112. valuePairs.Add(ParameterNames.Annotation, Annotation);
  113. valuePairs.Add(ParameterNames.AnnotEvent, AnnotEvent);
  114. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  115. }
  116. private void CancelEvent()
  117. {
  118. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  119. }
  120. public bool CanCloseDialog()
  121. {
  122. return true;
  123. }
  124. public void OnDialogClosed()
  125. {
  126. }
  127. public void OnDialogOpened(IDialogParameters parameters)
  128. {
  129. AnnotationHandlerEventArgs annotation;
  130. parameters.TryGetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation, out annotation);
  131. if (annotation != null)
  132. {
  133. Annotation = annotation;
  134. Dictionary<AnnotAttrib, object> annotAttribsList = annotation.AnnotHandlerEventArgs.GetAnnotAttrib();
  135. AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annotation.AnnotHandlerEventArgs, annotAttribsList);
  136. ContentText = annotation.Content;
  137. AnnotationName = "页面" + (annotation.PageIndex + 1).ToString();
  138. if (string.IsNullOrEmpty(ContentText))
  139. {
  140. TipVisibility = Visibility.Visible;
  141. }
  142. else
  143. {
  144. TipVisibility = Visibility.Collapsed;
  145. }
  146. return;
  147. }
  148. }
  149. }
  150. }