CustomCreateDialogViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Office.Model;
  6. using Prism.Commands;
  7. using Prism.Mvvm;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Windows.Controls;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  22. {
  23. class CustomCreateDialogViewModel : BindableBase, IDialogAware
  24. {
  25. public string Title => ".";
  26. public event Action<IDialogResult> RequestClose;
  27. public DelegateCommand CancelCommand { get; set; }
  28. public DelegateCommand CreateCommnad { get; set; }
  29. public DelegateCommand UpDataDynamicCommnad { get; set; }
  30. public DelegateCommand OpenImageCommnad { get; set; }
  31. public DelegateCommand<object> KeyDown { get; set; }
  32. private BitmapSource textImageSource;
  33. public BitmapSource TextImageSource
  34. {
  35. get { return textImageSource; }
  36. set
  37. {
  38. SetProperty(ref textImageSource, value);
  39. }
  40. }
  41. private BitmapSource imagePreviewSource;
  42. public BitmapSource ImagePreviewSource
  43. {
  44. get { return imagePreviewSource; }
  45. set
  46. {
  47. SetProperty(ref imagePreviewSource, value);
  48. }
  49. }
  50. private string stampText = "Stamp Text";
  51. public string StampText
  52. {
  53. get { return stampText; }
  54. set
  55. {
  56. SetProperty(ref stampText, value);
  57. }
  58. }
  59. private bool isCheckedDate;
  60. public bool IsCheckedDate
  61. {
  62. get { return isCheckedDate; }
  63. set
  64. {
  65. SetProperty(ref isCheckedDate, value);
  66. UpDataStamp();
  67. }
  68. }
  69. private bool isCheckedTime;
  70. public bool IsCheckedTime
  71. {
  72. get { return isCheckedTime; }
  73. set
  74. {
  75. SetProperty(ref isCheckedTime, value);
  76. UpDataStamp();
  77. }
  78. }
  79. private Visibility showImageButton = Visibility.Visible;
  80. public Visibility ShowImageButton
  81. {
  82. get { return showImageButton; }
  83. set
  84. {
  85. SetProperty(ref showImageButton, value);
  86. }
  87. }
  88. private C_TEXTSTAMP_SHAPE shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  89. public C_TEXTSTAMP_SHAPE Shape
  90. {
  91. get { return shape; }
  92. set { shape = value; }
  93. }
  94. private C_TEXTSTAMP_COLOR color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  95. public C_TEXTSTAMP_COLOR Color
  96. {
  97. get { return color; }
  98. set { color = value; }
  99. }
  100. public string SaveToPath { get; private set; }
  101. public string StampTextDate { get; private set; }
  102. public int StampWidth { get; private set; }
  103. public int StampHeight { get; private set; }
  104. public StampType Type { get; private set; }
  105. public void SetStampStyle(int index)
  106. {
  107. switch (index)
  108. {
  109. case 1:
  110. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  111. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  112. break;
  113. case 2:
  114. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  115. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  116. break;
  117. case 3:
  118. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  119. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  120. break;
  121. case 4:
  122. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  123. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  124. break;
  125. case 5:
  126. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  127. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  128. break;
  129. case 6:
  130. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  131. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  132. break;
  133. case 7:
  134. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  135. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  136. break;
  137. case 8:
  138. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  139. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  140. break;
  141. case 9:
  142. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  143. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  144. break;
  145. case 10:
  146. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  147. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  148. break;
  149. case 11:
  150. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  151. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  152. break;
  153. default:
  154. break;
  155. }
  156. UpDataStamp();
  157. }
  158. public CustomCreateDialogViewModel()
  159. {
  160. CancelCommand = new DelegateCommand(Cancel);
  161. CreateCommnad = new DelegateCommand(Create);
  162. UpDataDynamicCommnad = new DelegateCommand(UpDataStamp);
  163. KeyDown = new DelegateCommand<object>(PreviewKeyDown);
  164. OpenImageCommnad = new DelegateCommand(OpenImage);
  165. }
  166. private void Cancel()
  167. {
  168. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  169. }
  170. private void Create()
  171. {
  172. if (Type == StampType.TEXT_STAMP)
  173. {
  174. SaveImageToPath();
  175. }
  176. DialogParameters valuePairs = new DialogParameters();
  177. valuePairs.Add(ParameterNames.DataModel, this);
  178. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  179. }
  180. private void SaveImageToPath()
  181. {
  182. string path = App.CachePath.CustomStampPath;
  183. string name = Guid.NewGuid().ToString();
  184. if (!string.IsNullOrEmpty(path))
  185. {
  186. BitmapEncoder encoder = new PngBitmapEncoder();
  187. encoder.Frames.Add(BitmapFrame.Create(TextImageSource));
  188. path = System.IO.Path.Combine(path, name);
  189. using (FileStream stream = new FileStream(path, FileMode.Create))
  190. {
  191. encoder.Save(stream);
  192. }
  193. SaveToPath = path;
  194. }
  195. else
  196. {
  197. SaveToPath = "";
  198. }
  199. }
  200. public bool CanCloseDialog()
  201. {
  202. return true;
  203. }
  204. public void OnDialogClosed()
  205. {
  206. return;
  207. }
  208. private void PreviewKeyDown(object e)
  209. {
  210. var args = e as KeyEventArgs;
  211. if (args == null)
  212. {
  213. return;
  214. }
  215. if (args.Key == Key.Enter)
  216. {
  217. StampText = (args.OriginalSource as TextBox).Text;
  218. UpDataStamp();
  219. }
  220. }
  221. public void UpDataStamp()
  222. {
  223. string date = "";
  224. string dateType = "";
  225. if (IsCheckedDate)
  226. {
  227. dateType = "yyyy-MM-dd";
  228. }
  229. if (IsCheckedTime)
  230. {
  231. dateType = dateType + " HH:mm:ss";
  232. }
  233. if (!String.IsNullOrEmpty(dateType))
  234. {
  235. date = DateTime.Now.ToString(dateType);
  236. }
  237. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
  238. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  239. if (bytes.Length > 0)
  240. {
  241. PixelFormat fmt = PixelFormats.Bgra32;
  242. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  243. TextImageSource = bps;
  244. StampTextDate = date;
  245. StampWidth = stampWidth;
  246. StampHeight = stampHeight;
  247. Type = StampType.TEXT_STAMP;
  248. }
  249. else
  250. {
  251. TextImageSource = null;
  252. }
  253. }
  254. public void OpenImage()
  255. {
  256. OpenFileDialog openFile = new OpenFileDialog();
  257. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  258. "(*.gif)|*.gif|" +
  259. "(*.jpeg)|*.jpeg|" +
  260. "(*.jpg)|*.jpg|" +
  261. "(*.png)|*.png|" +
  262. "(*.tiff)|*.tiff";
  263. if (openFile.ShowDialog() == false)
  264. {
  265. return;
  266. }
  267. string path = App.CachePath.CustomStampPath;
  268. string name = Guid.NewGuid().ToString();
  269. if (!string.IsNullOrEmpty(path))
  270. {
  271. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  272. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  273. scale = Math.Min(scale, 1);
  274. string ext = Path.GetExtension(openFile.FileName);
  275. if (ext.ToUpper() == ".PNG")
  276. {
  277. BitmapEncoder encoder = new PngBitmapEncoder();
  278. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  279. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  280. path = System.IO.Path.Combine(path, name);
  281. using (FileStream stream = new FileStream(path, FileMode.Create))
  282. {
  283. encoder.Save(stream);
  284. }
  285. if (!string.IsNullOrEmpty(SaveToPath))
  286. {
  287. App.CachePath.AddToDeleteFiles(SaveToPath);
  288. }
  289. SaveToPath = path;
  290. ImagePreviewSource = targetBitmap;
  291. StampWidth = targetBitmap.PixelWidth;
  292. StampHeight = targetBitmap.PixelHeight;
  293. Type = StampType.IMAGE_STAMP;
  294. ShowImageButton = Visibility.Collapsed;
  295. }
  296. else
  297. {
  298. BitmapEncoder encoder = new JpegBitmapEncoder();
  299. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  300. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  301. path = System.IO.Path.Combine(path, name);
  302. using (FileStream stream = new FileStream(path, FileMode.Create))
  303. {
  304. encoder.Save(stream);
  305. }
  306. if (!string.IsNullOrEmpty(SaveToPath))
  307. {
  308. App.CachePath.AddToDeleteFiles(SaveToPath);
  309. }
  310. SaveToPath = path;
  311. ImagePreviewSource = targetBitmap;
  312. StampWidth = targetBitmap.PixelWidth;
  313. StampHeight = targetBitmap.PixelHeight;
  314. Type = StampType.IMAGE_STAMP;
  315. ShowImageButton = Visibility.Collapsed;
  316. }
  317. }
  318. else
  319. {
  320. SaveToPath = "";
  321. }
  322. }
  323. public void OnDialogOpened(IDialogParameters parameters)
  324. {
  325. UpDataStamp();
  326. return;
  327. }
  328. }
  329. }