CustomCreateDialogViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  17. {
  18. class CustomCreateDialogViewModel : BindableBase, IDialogAware
  19. {
  20. public string Title => ".";
  21. public event Action<IDialogResult> RequestClose;
  22. public DelegateCommand CancelCommand { get; set; }
  23. public DelegateCommand CreateCommnad { get; set; }
  24. public DelegateCommand UpDataDynamicCommnad { get; set; }
  25. public DelegateCommand<object> KeyDown { get; set; }
  26. private BitmapSource imageSource;
  27. public BitmapSource ImageSource
  28. {
  29. get { return imageSource; }
  30. set
  31. {
  32. SetProperty(ref imageSource, value);
  33. }
  34. }
  35. private string stampText = "Stamp Text";
  36. public string StampText
  37. {
  38. get { return stampText; }
  39. set
  40. {
  41. SetProperty(ref stampText, value);
  42. }
  43. }
  44. private bool isCheckedDate;
  45. public bool IsCheckedDate
  46. {
  47. get { return isCheckedDate; }
  48. set
  49. {
  50. SetProperty(ref isCheckedDate, value);
  51. UpDataStamp();
  52. }
  53. }
  54. private bool isCheckedTime;
  55. public bool IsCheckedTime
  56. {
  57. get { return isCheckedTime; }
  58. set
  59. {
  60. SetProperty(ref isCheckedTime, value);
  61. UpDataStamp();
  62. }
  63. }
  64. private C_TEXTSTAMP_SHAPE shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  65. public C_TEXTSTAMP_SHAPE Shape
  66. {
  67. get { return shape; }
  68. set { shape = value; }
  69. }
  70. private C_TEXTSTAMP_COLOR color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  71. public C_TEXTSTAMP_COLOR Color
  72. {
  73. get { return color; }
  74. set { color = value; }
  75. }
  76. public void SetStampStyle(int index)
  77. {
  78. switch (index)
  79. {
  80. case 1:
  81. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  82. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  83. break;
  84. case 2:
  85. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  86. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  87. break;
  88. case 3:
  89. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  90. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  91. break;
  92. case 4:
  93. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  94. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  95. break;
  96. case 5:
  97. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  98. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  99. break;
  100. case 6:
  101. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  102. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  103. break;
  104. case 7:
  105. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  106. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  107. break;
  108. case 8:
  109. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  110. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  111. break;
  112. case 9:
  113. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  114. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  115. break;
  116. case 10:
  117. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  118. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  119. break;
  120. case 11:
  121. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  122. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  123. break;
  124. default:
  125. break;
  126. }
  127. UpDataStamp();
  128. }
  129. public CustomCreateDialogViewModel()
  130. {
  131. CancelCommand = new DelegateCommand(Cancel);
  132. CreateCommnad = new DelegateCommand(Create);
  133. UpDataDynamicCommnad = new DelegateCommand(UpDataStamp);
  134. KeyDown = new DelegateCommand<object>(PreviewKeyDown);
  135. }
  136. private void Cancel()
  137. {
  138. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  139. }
  140. private void Create()
  141. {
  142. }
  143. public bool CanCloseDialog()
  144. {
  145. return true;
  146. }
  147. public void OnDialogClosed()
  148. {
  149. return;
  150. }
  151. private void PreviewKeyDown(object e)
  152. {
  153. var args = e as KeyEventArgs;
  154. if (args == null)
  155. {
  156. return;
  157. }
  158. if (args.Key == Key.Enter)
  159. {
  160. StampText = (args.OriginalSource as TextBox).Text;
  161. UpDataStamp();
  162. }
  163. }
  164. private void UpDataStamp()
  165. {
  166. string date = "";
  167. string dateType = "";
  168. if (IsCheckedDate)
  169. {
  170. dateType = "yyyy-MM-dd";
  171. }
  172. if (IsCheckedTime)
  173. {
  174. dateType= dateType+ " HH:mm:ss";
  175. }
  176. if (!String.IsNullOrEmpty(dateType))
  177. {
  178. date = DateTime.Now.ToString(dateType);
  179. }
  180. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
  181. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  182. if (bytes.Length > 0)
  183. {
  184. PixelFormat fmt = PixelFormats.Bgra32;
  185. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  186. ImageSource = bps;
  187. }
  188. else
  189. {
  190. ImageSource = null;
  191. }
  192. }
  193. public void OnDialogOpened(IDialogParameters parameters)
  194. {
  195. UpDataStamp();
  196. return;
  197. }
  198. }
  199. }