CustomCreateDialogViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer.AnnotEvent;
  3. using ComPDFKitViewer.PdfViewer;
  4. using Microsoft.Win32;
  5. using PDF_Master.Model;
  6. using PDF_Master.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_Master.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 = 1;
  90. public int RadioButtonIndex
  91. {
  92. get { return radioButtonIndex; }
  93. set
  94. {
  95. SetProperty(ref radioButtonIndex, value);
  96. }
  97. }
  98. /// <summary>
  99. /// 用于判断是否更换了文件,更换了文件则需要重新计算去背功能
  100. /// </summary>
  101. private bool IsNewFile = false;
  102. private BitmapSource originalimagePreviewSource;
  103. private BitmapSource removeBackgroundImagePreviewSource;
  104. private bool issRemoveBackground;
  105. public bool IsRemoveBackground
  106. {
  107. get { return issRemoveBackground; }
  108. set
  109. {
  110. SetProperty(ref issRemoveBackground, value);
  111. if (issRemoveBackground)
  112. {
  113. if (IsNewFile)
  114. {
  115. removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
  116. }
  117. ImagePreviewSource = removeBackgroundImagePreviewSource;
  118. }
  119. else
  120. {
  121. ImagePreviewSource = originalimagePreviewSource;
  122. }
  123. }
  124. }
  125. private C_TEXTSTAMP_SHAPE shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  126. public C_TEXTSTAMP_SHAPE Shape
  127. {
  128. get { return shape; }
  129. set { shape = value; }
  130. }
  131. private C_TEXTSTAMP_COLOR color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  132. public C_TEXTSTAMP_COLOR Color
  133. {
  134. get { return color; }
  135. set { color = value; }
  136. }
  137. public string SaveToPath { get; private set; }
  138. public string RemoveBackgroundSaveToPath { get; private set; }
  139. public string StampTextDate { get; private set; }
  140. public int StampWidth { get; private set; }
  141. public int StampHeight { get; private set; }
  142. public StampType Type { get; private set; }
  143. public void SetStampStyle(int index)
  144. {
  145. RadioButtonIndex = index;
  146. switch (index)
  147. {
  148. case 1:
  149. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  150. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  151. break;
  152. case 2:
  153. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  154. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  155. break;
  156. case 3:
  157. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  158. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  159. break;
  160. case 4:
  161. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  162. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  163. break;
  164. case 5:
  165. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  166. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  167. break;
  168. case 6:
  169. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  170. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  171. break;
  172. case 7:
  173. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  174. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  175. break;
  176. case 8:
  177. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  178. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  179. break;
  180. case 9:
  181. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  182. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  183. break;
  184. case 10:
  185. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  186. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  187. break;
  188. case 11:
  189. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  190. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  191. break;
  192. default:
  193. break;
  194. }
  195. UpDataStamp();
  196. }
  197. public CustomCreateDialogViewModel()
  198. {
  199. CancelCommand = new DelegateCommand(Cancel);
  200. CreateCommnad = new DelegateCommand(Create);
  201. UpDataDynamicCommnad = new DelegateCommand(UpDataStamp);
  202. KeyDown = new DelegateCommand<object>(PreviewKeyDown);
  203. OpenImageCommnad = new DelegateCommand(OpenImage);
  204. }
  205. private void Cancel()
  206. {
  207. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  208. }
  209. private void Create()
  210. {
  211. if (Type == StampType.TEXT_STAMP)
  212. {
  213. SaveImageToPath();
  214. }
  215. DialogParameters valuePairs = new DialogParameters();
  216. valuePairs.Add(ParameterNames.DataModel, this);
  217. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  218. }
  219. private void SaveImageToPath()
  220. {
  221. string path = App.CachePath.CustomStampPath;
  222. string name = Guid.NewGuid().ToString();
  223. if (!string.IsNullOrEmpty(path))
  224. {
  225. BitmapEncoder encoder = new PngBitmapEncoder();
  226. encoder.Frames.Add(BitmapFrame.Create(TextImageSource));
  227. path = System.IO.Path.Combine(path, name);
  228. using (FileStream stream = new FileStream(path, FileMode.Create))
  229. {
  230. encoder.Save(stream);
  231. }
  232. SaveToPath = path;
  233. }
  234. else
  235. {
  236. SaveToPath = "";
  237. }
  238. }
  239. public bool CanCloseDialog()
  240. {
  241. return true;
  242. }
  243. public void OnDialogClosed()
  244. {
  245. return;
  246. }
  247. private void PreviewKeyDown(object e)
  248. {
  249. var args = e as KeyEventArgs;
  250. if (args == null)
  251. {
  252. return;
  253. }
  254. if (args.Key == Key.Enter)
  255. {
  256. StampText = (args.OriginalSource as TextBox).Text;
  257. UpDataStamp();
  258. }
  259. }
  260. public void UpDataStamp()
  261. {
  262. string date = "";
  263. string dateType = "";
  264. if (IsCheckedDate)
  265. {
  266. dateType = "yyyy-MM-dd";
  267. }
  268. if (IsCheckedTime)
  269. {
  270. dateType = dateType + " HH:mm:ss";
  271. }
  272. if (!String.IsNullOrEmpty(dateType))
  273. {
  274. date = DateTime.Now.ToString(dateType);
  275. }
  276. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
  277. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  278. if (bytes.Length > 0)
  279. {
  280. PixelFormat fmt = PixelFormats.Bgra32;
  281. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  282. TextImageSource = bps;
  283. StampTextDate = date;
  284. StampWidth = stampWidth;
  285. StampHeight = stampHeight;
  286. Type = StampType.TEXT_STAMP;
  287. }
  288. else
  289. {
  290. TextImageSource = null;
  291. }
  292. }
  293. public void OpenImage()
  294. {
  295. OpenFileDialog openFile = new OpenFileDialog();
  296. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  297. "(*.gif)|*.gif|" +
  298. "(*.jpeg)|*.jpeg|" +
  299. "(*.jpg)|*.jpg|" +
  300. "(*.png)|*.png|" +
  301. "(*.tiff)|*.tiff";
  302. if (openFile.ShowDialog() == false)
  303. {
  304. return;
  305. }
  306. string path = App.CachePath.CustomStampPath;
  307. string name = Guid.NewGuid().ToString();
  308. if (!string.IsNullOrEmpty(path))
  309. {
  310. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  311. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  312. scale = Math.Min(scale, 1);
  313. string ext = Path.GetExtension(openFile.FileName);
  314. if (ext.ToUpper() == ".PNG")
  315. {
  316. BitmapEncoder encoder = new PngBitmapEncoder();
  317. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  318. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  319. path = System.IO.Path.Combine(path, name);
  320. using (FileStream stream = new FileStream(path, FileMode.Create))
  321. {
  322. encoder.Save(stream);
  323. }
  324. if (!string.IsNullOrEmpty(SaveToPath))
  325. {
  326. App.CachePath.AddToDeleteFiles(SaveToPath);
  327. }
  328. IsNewFile = true;
  329. SaveToPath = path;
  330. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  331. StampWidth = targetBitmap.PixelWidth;
  332. StampHeight = targetBitmap.PixelHeight;
  333. Type = StampType.IMAGE_STAMP;
  334. ShowImageButton = Visibility.Collapsed;
  335. }
  336. else
  337. {
  338. BitmapEncoder encoder = new JpegBitmapEncoder();
  339. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  340. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  341. path = System.IO.Path.Combine(path, name);
  342. using (FileStream stream = new FileStream(path, FileMode.Create))
  343. {
  344. encoder.Save(stream);
  345. }
  346. if (!string.IsNullOrEmpty(SaveToPath))
  347. {
  348. App.CachePath.AddToDeleteFiles(SaveToPath);
  349. }
  350. SaveToPath = path;
  351. IsNewFile = true;
  352. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  353. StampWidth = targetBitmap.PixelWidth;
  354. StampHeight = targetBitmap.PixelHeight;
  355. Type = StampType.IMAGE_STAMP;
  356. ShowImageButton = Visibility.Collapsed;
  357. }
  358. }
  359. else
  360. {
  361. SaveToPath = "";
  362. }
  363. }
  364. /// <summary>
  365. /// 去除白底
  366. /// </summary>
  367. public Bitmap KnockOutGzf()
  368. {
  369. System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
  370. Bitmap bitmapProxy = new Bitmap(image);
  371. image.Dispose();
  372. for (int i = 0; i < bitmapProxy.Width; i++)
  373. {
  374. for (int j = 0; j < bitmapProxy.Height; j++)
  375. {
  376. System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
  377. if (!(c.R < 240 || c.G < 240 || c.B < 240))
  378. {
  379. bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);
  380. }
  381. }
  382. }
  383. return bitmapProxy;
  384. }
  385. /// <summary>
  386. /// 将 Bitmap 转化为 BitmapSource
  387. /// </summary>
  388. /// <param name="bmp"/>要转换的 Bitmap
  389. /// <returns>转换后的 BitmapImage</returns>
  390. private BitmapImage ToBitmapImage(System.Drawing.Bitmap bmp)
  391. {
  392. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  393. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  394. BitmapImage image = new BitmapImage();
  395. image.BeginInit();
  396. image.StreamSource = ms;
  397. image.CacheOption = BitmapCacheOption.OnLoad;
  398. image.EndInit();
  399. //编码,缓存到本地
  400. string path = App.CachePath.CustomStampPath;
  401. string name = Guid.NewGuid().ToString();
  402. BitmapEncoder encoder = new PngBitmapEncoder();
  403. encoder.Frames.Add(BitmapFrame.Create(image));
  404. path = System.IO.Path.Combine(path, name);
  405. using (FileStream stream = new FileStream(path, FileMode.Create))
  406. {
  407. encoder.Save(stream);
  408. }
  409. if (!string.IsNullOrEmpty(RemoveBackgroundSaveToPath))
  410. {
  411. App.CachePath.AddToDeleteFiles(RemoveBackgroundSaveToPath);
  412. }
  413. RemoveBackgroundSaveToPath = path;
  414. IsNewFile = false;
  415. return image;
  416. }
  417. public void OnDialogOpened(IDialogParameters parameters)
  418. {
  419. if (parameters != null)
  420. {
  421. Stamp stamp = parameters.GetValue<Stamp>(ParameterNames.DataModel);
  422. if (stamp != null)
  423. {
  424. IsCheckedTime = stamp.IsCheckedTime;
  425. IsCheckedDate = stamp.IsCheckedDate;
  426. StampText = stamp.StampText;
  427. SaveToPath = stamp.SourcePath;
  428. Color = (C_TEXTSTAMP_COLOR)(int)stamp.TextColor;
  429. Shape = (C_TEXTSTAMP_SHAPE)(int)stamp.TextSharp;
  430. UpDataRadioButton(stamp.TextSharp, stamp.TextColor);
  431. }
  432. }
  433. UpDataStamp();
  434. return;
  435. }
  436. private void UpDataRadioButton(TextStampSharp textStampSharp, TextStampColor textStampColor)
  437. {
  438. int index = 1;
  439. switch (textStampColor)
  440. {
  441. case TextStampColor.TEXTSTAMP_WHITE:
  442. switch (textStampSharp)
  443. {
  444. case TextStampSharp.TEXTSTAMP_RECT:
  445. index = 2;
  446. break;
  447. case TextStampSharp.TEXTSTAMP_NONE:
  448. index = 1;
  449. break;
  450. }
  451. break;
  452. case TextStampColor.TEXTSTAMP_RED:
  453. switch (textStampSharp)
  454. {
  455. case TextStampSharp.TEXTSTAMP_RECT:
  456. index = 4;
  457. break;
  458. case TextStampSharp.TEXTSTAMP_LEFT_TRIANGLE:
  459. index = 7;
  460. break;
  461. case TextStampSharp.TEXTSTAMP_RIGHT_TRIANGLE:
  462. index = 10;
  463. break;
  464. }
  465. break;
  466. case TextStampColor.TEXTSTAMP_GREEN:
  467. switch (textStampSharp)
  468. {
  469. case TextStampSharp.TEXTSTAMP_RECT:
  470. index = 3;
  471. break;
  472. case TextStampSharp.TEXTSTAMP_LEFT_TRIANGLE:
  473. index = 6;
  474. break;
  475. case TextStampSharp.TEXTSTAMP_RIGHT_TRIANGLE:
  476. index = 9;
  477. break;
  478. }
  479. break;
  480. case TextStampColor.TEXTSTAMP_BLUE:
  481. switch (textStampSharp)
  482. {
  483. case TextStampSharp.TEXTSTAMP_RECT:
  484. index = 5;
  485. break;
  486. case TextStampSharp.TEXTSTAMP_LEFT_TRIANGLE:
  487. index = 8;
  488. break;
  489. case TextStampSharp.TEXTSTAMP_RIGHT_TRIANGLE:
  490. index = 11;
  491. break;
  492. }
  493. break;
  494. default:
  495. break;
  496. }
  497. RadioButtonIndex = index;
  498. }
  499. }
  500. }