CustomCreateDialogViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. /// <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. private Bitmap bitmapProxy;
  144. public void SetStampStyle(int index)
  145. {
  146. RadioButtonIndex = index;
  147. switch (index)
  148. {
  149. case 1:
  150. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  151. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_NONE;
  152. break;
  153. case 2:
  154. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_WHITE;
  155. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  156. break;
  157. case 3:
  158. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  159. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  160. break;
  161. case 4:
  162. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  163. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  164. break;
  165. case 5:
  166. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  167. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RECT;
  168. break;
  169. case 6:
  170. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  171. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  172. break;
  173. case 7:
  174. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  175. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  176. break;
  177. case 8:
  178. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_BLUE;
  179. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_LEFT_TRIANGLE;
  180. break;
  181. case 9:
  182. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  183. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  184. break;
  185. case 10:
  186. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_RED;
  187. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  188. break;
  189. case 11:
  190. Color = C_TEXTSTAMP_COLOR.TEXTSTAMP_GREEN;
  191. Shape = C_TEXTSTAMP_SHAPE.TEXTSTAMP_RIGHT_TRIANGLE;
  192. break;
  193. default:
  194. break;
  195. }
  196. UpDataStamp();
  197. }
  198. public CustomCreateDialogViewModel()
  199. {
  200. CancelCommand = new DelegateCommand(Cancel);
  201. CreateCommnad = new DelegateCommand(Create);
  202. UpDataDynamicCommnad = new DelegateCommand(UpDataStamp);
  203. KeyDown = new DelegateCommand<object>(PreviewKeyDown);
  204. OpenImageCommnad = new DelegateCommand(OpenImage);
  205. }
  206. private void Cancel()
  207. {
  208. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  209. }
  210. private void Create()
  211. {
  212. if (Type == StampType.TEXT_STAMP)
  213. {
  214. SaveImageToPath();
  215. }
  216. DialogParameters valuePairs = new DialogParameters();
  217. valuePairs.Add(ParameterNames.DataModel, this);
  218. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  219. }
  220. private void SaveImageToPath()
  221. {
  222. string path = App.CachePath.CustomStampPath;
  223. string name = Guid.NewGuid().ToString();
  224. if (!string.IsNullOrEmpty(path))
  225. {
  226. BitmapEncoder encoder = new PngBitmapEncoder();
  227. encoder.Frames.Add(BitmapFrame.Create(TextImageSource));
  228. path = System.IO.Path.Combine(path, name);
  229. using (FileStream stream = new FileStream(path, FileMode.Create))
  230. {
  231. encoder.Save(stream);
  232. }
  233. SaveToPath = path;
  234. }
  235. else
  236. {
  237. SaveToPath = "";
  238. }
  239. }
  240. public bool CanCloseDialog()
  241. {
  242. return true;
  243. }
  244. public void OnDialogClosed()
  245. {
  246. return;
  247. }
  248. private void PreviewKeyDown(object e)
  249. {
  250. var args = e as KeyEventArgs;
  251. if (args == null)
  252. {
  253. return;
  254. }
  255. if (args.Key == Key.Enter)
  256. {
  257. StampText = (args.OriginalSource as TextBox).Text;
  258. UpDataStamp();
  259. }
  260. }
  261. public void UpDataStamp()
  262. {
  263. string date = "";
  264. string dateType = "";
  265. if (IsCheckedDate)
  266. {
  267. dateType = "yyyy-MM-dd";
  268. }
  269. if (IsCheckedTime)
  270. {
  271. dateType = dateType + " HH:mm:ss";
  272. }
  273. if (!String.IsNullOrEmpty(dateType))
  274. {
  275. date = DateTime.Now.ToString(dateType);
  276. }
  277. var bytes = CPDFStampAnnotation.GetTempTextStampImage(StampText, date,
  278. Shape, Color, out int stampWidth, out int stampHeight, out int width, out int height);
  279. if (bytes.Length > 0)
  280. {
  281. PixelFormat fmt = PixelFormats.Bgra32;
  282. BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, bytes, (width * fmt.BitsPerPixel + 7) / 8);
  283. TextImageSource = bps;
  284. StampTextDate = date;
  285. StampWidth = stampWidth;
  286. StampHeight = stampHeight;
  287. Type = StampType.TEXT_STAMP;
  288. }
  289. else
  290. {
  291. TextImageSource = null;
  292. }
  293. }
  294. public void OpenImage()
  295. {
  296. OpenFileDialog openFile = new OpenFileDialog();
  297. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  298. "(*.gif)|*.gif|" +
  299. "(*.jpeg)|*.jpeg|" +
  300. "(*.jpg)|*.jpg|" +
  301. "(*.png)|*.png|" +
  302. "(*.tiff)|*.tiff";
  303. if (openFile.ShowDialog() == false)
  304. {
  305. return;
  306. }
  307. string path = App.CachePath.CustomStampPath;
  308. string name = Guid.NewGuid().ToString();
  309. if (!string.IsNullOrEmpty(path))
  310. {
  311. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  312. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  313. scale = Math.Min(scale, 1);
  314. string ext = Path.GetExtension(openFile.FileName);
  315. if (ext.ToUpper() == ".PNG")
  316. {
  317. BitmapEncoder encoder = new PngBitmapEncoder();
  318. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  319. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  320. path = System.IO.Path.Combine(path, name);
  321. using (FileStream stream = new FileStream(path, FileMode.Create))
  322. {
  323. encoder.Save(stream);
  324. }
  325. if (!string.IsNullOrEmpty(SaveToPath))
  326. {
  327. App.CachePath.AddToDeleteFiles(SaveToPath);
  328. }
  329. IsNewFile = true;
  330. SaveToPath = path;
  331. ImagePreviewSource= originalimagePreviewSource = targetBitmap;
  332. StampWidth = targetBitmap.PixelWidth;
  333. StampHeight = targetBitmap.PixelHeight;
  334. Type = StampType.IMAGE_STAMP;
  335. ShowImageButton = Visibility.Collapsed;
  336. }
  337. else
  338. {
  339. BitmapEncoder encoder = new JpegBitmapEncoder();
  340. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  341. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  342. path = System.IO.Path.Combine(path, name);
  343. using (FileStream stream = new FileStream(path, FileMode.Create))
  344. {
  345. encoder.Save(stream);
  346. }
  347. if (!string.IsNullOrEmpty(SaveToPath))
  348. {
  349. App.CachePath.AddToDeleteFiles(SaveToPath);
  350. }
  351. SaveToPath = path;
  352. IsNewFile = true;
  353. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  354. StampWidth = targetBitmap.PixelWidth;
  355. StampHeight = targetBitmap.PixelHeight;
  356. Type = StampType.IMAGE_STAMP;
  357. ShowImageButton = Visibility.Collapsed;
  358. }
  359. }
  360. else
  361. {
  362. SaveToPath = "";
  363. }
  364. }
  365. /// <summary>
  366. /// 去除白底
  367. /// </summary>
  368. public Bitmap KnockOutGzf()
  369. {
  370. System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
  371. bitmapProxy = new Bitmap(image);
  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. }