CustomCreateDialogViewModel.cs 15 KB

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