SignatureCreateDialogViewModel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer.PdfViewer;
  3. using Microsoft.Win32;
  4. using PDF_Office.Model;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Prism.Services.Dialogs;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Collections.ObjectModel;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Ink;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  23. {
  24. class SignatureCreateDialogViewModel : BindableBase, IDialogAware
  25. {
  26. public string Title => "";
  27. private CPDFViewer PDFViewer;
  28. public event Action<IDialogResult> RequestClose;
  29. public DelegateCommand CancelCommand { get; set; }
  30. public DelegateCommand CreateCommnad { get; set; }
  31. public DelegateCommand<object> CheckedCommnad { get; set; }
  32. public DelegateCommand OpenImageCommnad { get; set; }
  33. public DelegateCommand ClearImageCommnad { get; set; }
  34. public DelegateCommand ClearInkCanvasCommnad { get; set; }
  35. public ObservableCollection<string> FontNameList { get; set; }
  36. public ObservableCollection<string> ThicknessList { get; set; }
  37. private int fontNameIndex = 0;
  38. public int FontNameIndex
  39. {
  40. get { return fontNameIndex; }
  41. set
  42. {
  43. SetProperty(ref fontNameIndex, value);
  44. }
  45. }
  46. private int radioButtonIndex = 1;
  47. public int RadioButtonIndex
  48. {
  49. get { return radioButtonIndex; }
  50. set
  51. {
  52. SetProperty(ref radioButtonIndex, value);
  53. }
  54. }
  55. private int tabItemIndex;
  56. public int TabItemIndex
  57. {
  58. get { return tabItemIndex; }
  59. set
  60. {
  61. SetProperty(ref tabItemIndex, value);
  62. }
  63. }
  64. private string inputText = "";
  65. public string InputText
  66. {
  67. get { return inputText; }
  68. set
  69. {
  70. SetProperty(ref inputText, value);
  71. }
  72. }
  73. private Visibility showImageButton = Visibility.Visible;
  74. public Visibility ShowImageButton
  75. {
  76. get { return showImageButton; }
  77. set
  78. {
  79. SetProperty(ref showImageButton, value);
  80. }
  81. }
  82. private BitmapSource imagePreviewSource;
  83. public BitmapSource ImagePreviewSource
  84. {
  85. get { return imagePreviewSource; }
  86. set
  87. {
  88. SetProperty(ref imagePreviewSource, value);
  89. }
  90. }
  91. private bool issRemoveBackground;
  92. public bool IsRemoveBackground
  93. {
  94. get { return issRemoveBackground; }
  95. set
  96. {
  97. SetProperty(ref issRemoveBackground, value);
  98. if (issRemoveBackground)
  99. {
  100. if (IsNewFile)
  101. {
  102. removeBackgroundImagePreviewSource = ToBitmapImage(KnockOutGzf());
  103. }
  104. ImagePreviewSource = removeBackgroundImagePreviewSource;
  105. }
  106. else
  107. {
  108. ImagePreviewSource = originalimagePreviewSource;
  109. }
  110. }
  111. }
  112. private DrawingAttributes drawingAttributes;
  113. public DrawingAttributes DrawingAttributeObject
  114. {
  115. get { return drawingAttributes; }
  116. set
  117. {
  118. SetProperty(ref drawingAttributes, value);
  119. }
  120. }
  121. private StrokeCollection strokes;
  122. public StrokeCollection StrokesObject
  123. {
  124. get { return strokes; }
  125. set
  126. {
  127. SetProperty(ref strokes, value);
  128. }
  129. }
  130. private int imageRadioButtonIndex = 1;
  131. public int ImageRadioButtonIndex
  132. {
  133. get { return imageRadioButtonIndex; }
  134. set
  135. {
  136. SetProperty(ref imageRadioButtonIndex, value);
  137. switch (imageRadioButtonIndex)
  138. {
  139. case 1:
  140. DrawingAttributeObject.Color = Colors.Black;
  141. break;
  142. case 2:
  143. DrawingAttributeObject.Color = Colors.Red;
  144. break;
  145. case 3:
  146. DrawingAttributeObject.Color = Colors.Green;
  147. break;
  148. case 4:
  149. DrawingAttributeObject.Color = Colors.Blue;
  150. break;
  151. default:
  152. break;
  153. }
  154. UpDataToStrokesObject();
  155. }
  156. }
  157. private int thicknessListIndex = 0;
  158. public int ThicknessListIndex
  159. {
  160. get { return thicknessListIndex; }
  161. set
  162. {
  163. SetProperty(ref thicknessListIndex, value);
  164. if (thicknessListIndex >= 0)
  165. {
  166. DrawingAttributeObject.Width = DrawingAttributeObject.Height = Convert.ToDouble(ThicknessList[thicknessListIndex].Substring(0, 3));
  167. UpDataToStrokesObject();
  168. }
  169. }
  170. }
  171. public string SaveToPath { get; private set; }
  172. public string RemoveBackgroundSaveToPath { get; private set; }
  173. public string DrawingSaveToPath { get; private set; }
  174. /// <summary>
  175. /// 用于判断是否更换了文件,更换了文件则需要重新计算去背功能
  176. /// </summary>
  177. private bool IsNewFile = false;
  178. //去背图片和原图的图片数据
  179. private BitmapSource originalimagePreviewSource = null;
  180. private BitmapSource removeBackgroundImagePreviewSource = null;
  181. public SignatureCreateDialogViewModel()
  182. {
  183. DrawingAttributeObject = new DrawingAttributes();
  184. StrokesObject = new StrokeCollection();
  185. FontNameList = new ObservableCollection<string>();
  186. ThicknessList = new ObservableCollection<string>() { "1.0pt", "2.0pt", "4.0pt", "6.0pt", "8.0pt" };//改了这里记得去改ThicknessListIndex的Set方法里面的取值
  187. CancelCommand = new DelegateCommand(Cancel);
  188. CreateCommnad = new DelegateCommand(Create);
  189. CheckedCommnad = new DelegateCommand<object>(Checked);
  190. OpenImageCommnad = new DelegateCommand(OpenImage);
  191. ClearImageCommnad = new DelegateCommand(ClearImage);
  192. ClearInkCanvasCommnad = new DelegateCommand(ClearInkCanvas);
  193. DrawingAttributeObject.Color = Colors.Black;
  194. DrawingAttributeObject.Width = 1;
  195. DrawingAttributeObject.Height = 1;
  196. WeakEventManager<StrokeCollection, StrokeCollectionChangedEventArgs>.AddHandler(StrokesObject, "StrokesChanged", StrokesObject_StrokesChanged);
  197. InitFontNameList();
  198. }
  199. private void StrokesObject_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  200. {
  201. int x1 = 1;
  202. }
  203. private void InitFontNameList()
  204. {
  205. System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
  206. foreach (var item in objFont.Families)
  207. {
  208. FontNameList.Add(item.Name);
  209. }
  210. }
  211. private void Checked(object index)
  212. {
  213. RadioButtonIndex = Convert.ToInt32(index);
  214. }
  215. public void UpDataImageRadioButtonIndex(int Index)
  216. {
  217. ImageRadioButtonIndex = Index;
  218. }
  219. public void UpDataRadioButtonIndex(int Index)
  220. {
  221. RadioButtonIndex = Index;
  222. }
  223. private void UpDataToStrokesObject()
  224. {
  225. foreach (var item in StrokesObject)
  226. {
  227. item.DrawingAttributes = DrawingAttributeObject;
  228. }
  229. }
  230. private void OpenImage()
  231. {
  232. OpenFileDialog openFile = new OpenFileDialog();
  233. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  234. "(*.gif)|*.gif|" +
  235. "(*.jpeg)|*.jpeg|" +
  236. "(*.jpg)|*.jpg|" +
  237. "(*.png)|*.png|" +
  238. "(*.tiff)|*.tiff";
  239. if (openFile.ShowDialog() == false)
  240. {
  241. return;
  242. }
  243. string path = App.CachePath.CustomStampPath;
  244. string name = Guid.NewGuid().ToString();
  245. if (!string.IsNullOrEmpty(path))
  246. {
  247. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  248. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  249. scale = Math.Min(scale, 1);
  250. string ext = Path.GetExtension(openFile.FileName);
  251. if (ext.ToUpper() == ".PNG")
  252. {
  253. BitmapEncoder encoder = new PngBitmapEncoder();
  254. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  255. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  256. path = System.IO.Path.Combine(path, name);
  257. using (FileStream stream = new FileStream(path, FileMode.Create))
  258. {
  259. encoder.Save(stream);
  260. }
  261. if (!string.IsNullOrEmpty(SaveToPath))
  262. {
  263. App.CachePath.AddToDeleteFiles(SaveToPath);
  264. }
  265. IsNewFile = true;
  266. SaveToPath = path;
  267. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  268. ShowImageButton = Visibility.Collapsed;
  269. }
  270. else
  271. {
  272. BitmapEncoder encoder = new JpegBitmapEncoder();
  273. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  274. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  275. path = System.IO.Path.Combine(path, name);
  276. using (FileStream stream = new FileStream(path, FileMode.Create))
  277. {
  278. encoder.Save(stream);
  279. }
  280. if (!string.IsNullOrEmpty(SaveToPath))
  281. {
  282. App.CachePath.AddToDeleteFiles(SaveToPath);
  283. }
  284. IsNewFile = true;
  285. SaveToPath = path;
  286. ImagePreviewSource = originalimagePreviewSource = targetBitmap;
  287. ShowImageButton = Visibility.Collapsed;
  288. }
  289. }
  290. else
  291. {
  292. SaveToPath = "";
  293. }
  294. }
  295. private void ClearImage()
  296. {
  297. ShowImageButton = Visibility.Visible;
  298. ImagePreviewSource = originalimagePreviewSource = null;
  299. }
  300. private void ClearInkCanvas()
  301. {
  302. StrokesObject.Clear();
  303. }
  304. /// <summary>
  305. /// 去除白底
  306. /// </summary>
  307. private Bitmap KnockOutGzf()
  308. {
  309. System.Drawing.Image image = System.Drawing.Image.FromFile(SaveToPath);
  310. Bitmap bitmapProxy = new Bitmap(image);
  311. image.Dispose();
  312. for (int i = 0; i < bitmapProxy.Width; i++)
  313. {
  314. for (int j = 0; j < bitmapProxy.Height; j++)
  315. {
  316. System.Drawing.Color c = bitmapProxy.GetPixel(i, j);
  317. if (!(c.R < 240 || c.G < 240 || c.B < 240))
  318. {
  319. bitmapProxy.SetPixel(i, j, System.Drawing.Color.Transparent);
  320. }
  321. }
  322. }
  323. return bitmapProxy;
  324. }
  325. /// <summary>
  326. /// 将 Bitmap 转化为 BitmapSource
  327. /// </summary>
  328. /// <param name="bmp"/>要转换的 Bitmap
  329. /// <returns>转换后的 BitmapImage</returns>
  330. private BitmapImage ToBitmapImage(System.Drawing.Bitmap bmp)
  331. {
  332. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  333. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  334. BitmapImage image = new BitmapImage();
  335. image.BeginInit();
  336. image.StreamSource = ms;
  337. image.CacheOption = BitmapCacheOption.OnLoad;
  338. image.EndInit();
  339. //编码,缓存到本地
  340. string path = App.CachePath.CustomStampPath;
  341. string name = Guid.NewGuid().ToString();
  342. BitmapEncoder encoder = new PngBitmapEncoder();
  343. encoder.Frames.Add(BitmapFrame.Create(image));
  344. path = System.IO.Path.Combine(path, name);
  345. using (FileStream stream = new FileStream(path, FileMode.Create))
  346. {
  347. encoder.Save(stream);
  348. }
  349. if (!string.IsNullOrEmpty(RemoveBackgroundSaveToPath))
  350. {
  351. App.CachePath.AddToDeleteFiles(RemoveBackgroundSaveToPath);
  352. }
  353. RemoveBackgroundSaveToPath = path;
  354. IsNewFile = false;
  355. return image;
  356. }
  357. /// <summary>
  358. /// 把文字转换成Bitmap
  359. /// </summary>
  360. /// <param name="text"></param>
  361. /// <param name="font"></param>
  362. /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
  363. /// <param name="fontcolor">字体颜色</param>
  364. /// <param name="backColor">背景颜色</param>
  365. /// <returns></returns>
  366. private Bitmap TextToBitmap(string text, Font font, Rectangle rect, System.Drawing.Color fontcolor, System.Drawing.Color backColor)
  367. {
  368. Graphics g;
  369. Bitmap bmp;
  370. StringFormat format = new StringFormat(StringFormatFlags.NoClip);
  371. if (rect == Rectangle.Empty)
  372. {
  373. bmp = new Bitmap(1, 1);
  374. g = Graphics.FromImage(bmp);
  375. //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
  376. SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
  377. int width = (int)(sizef.Width + 1);
  378. int height = (int)(sizef.Height + 1);
  379. rect = new Rectangle(0, 0, width, height);
  380. bmp.Dispose();
  381. bmp = new Bitmap(width, height);
  382. }
  383. else
  384. {
  385. bmp = new Bitmap(rect.Width, rect.Height);
  386. }
  387. g = Graphics.FromImage(bmp);
  388. //使用ClearType字体功能
  389. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  390. g.FillRectangle(new SolidBrush(backColor), rect);
  391. g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
  392. return bmp;
  393. }
  394. private void Cancel()
  395. {
  396. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  397. }
  398. private void Create()
  399. {
  400. switch (TabItemIndex)
  401. {
  402. case 0:
  403. {
  404. if (string.IsNullOrEmpty(InputText))
  405. {
  406. Cancel();
  407. return;
  408. }
  409. System.Drawing.Color fontcolor = System.Drawing.Color.Black;
  410. switch (RadioButtonIndex)
  411. {
  412. case 1:
  413. fontcolor = System.Drawing.Color.Black;
  414. break;
  415. case 2:
  416. fontcolor = System.Drawing.Color.Red;
  417. break;
  418. case 3:
  419. fontcolor = System.Drawing.Color.Green;
  420. break;
  421. case 4:
  422. fontcolor = System.Drawing.Color.Blue;
  423. break;
  424. default:
  425. break;
  426. }
  427. Bitmap bmp = TextToBitmap(InputText, new Font(FontNameList[fontNameIndex], 48), Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
  428. string guid = Guid.NewGuid().ToString();
  429. string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
  430. bmp.Save(path, ImageFormat.Png);
  431. SaveToPath = path;
  432. }
  433. break;
  434. case 1:
  435. {
  436. var FreeHandpath = App.CachePath.SignatureFreeHandPath;
  437. string name = Guid.NewGuid().ToString();
  438. FreeHandpath = System.IO.Path.Combine(FreeHandpath, name);
  439. using (System.IO.FileStream stream = new System.IO.FileStream(FreeHandpath, System.IO.FileMode.Create))
  440. {
  441. StrokesObject.Save(stream);
  442. }
  443. StampAnnotArgs stampArgs = new StampAnnotArgs();
  444. List<List<System.Windows.Point>> RawPointList = new List<List<System.Windows.Point>>();
  445. for (int kk = 0; kk < StrokesObject.Count; kk++)
  446. {
  447. List<System.Windows.Point> p = new List<System.Windows.Point>();
  448. RawPointList.Add(p);
  449. for (int gg = 0; gg < StrokesObject[kk].StylusPoints.Count; gg++)
  450. {
  451. var point = StrokesObject[kk].StylusPoints[gg].ToPoint();
  452. if (point.X >= 0 && point.Y >= 0)
  453. RawPointList[kk].Add(point);
  454. }
  455. }
  456. DrawingSaveToPath = FreeHandpath;
  457. stampArgs.SetInkData(RawPointList, drawingAttributes.Width, drawingAttributes.Color);
  458. var writeStamp = stampArgs.GetStampDrawing();
  459. FreeHandpath = App.CachePath.SignatureStampPath;
  460. string thumbnailName = Guid.NewGuid().ToString();
  461. FreeHandpath = System.IO.Path.Combine(FreeHandpath, thumbnailName);
  462. using (FileStream stream5 = new FileStream(FreeHandpath, FileMode.Create))
  463. {
  464. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  465. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  466. encoder5.Save(stream5);
  467. }
  468. SaveToPath = FreeHandpath;
  469. }
  470. break;
  471. case 2:
  472. break;
  473. default:
  474. break;
  475. }
  476. DialogParameters valuePairs = new DialogParameters();
  477. valuePairs.Add(ParameterNames.DataModel, this);
  478. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  479. }
  480. public bool CanCloseDialog()
  481. {
  482. return true;
  483. }
  484. public void OnDialogClosed()
  485. {
  486. return;
  487. }
  488. public void OnDialogOpened(IDialogParameters parameters)
  489. {
  490. if (parameters != null)
  491. {
  492. PDFViewer = parameters.GetValue<CPDFViewer>(ParameterNames.PDFViewer);
  493. if (PDFViewer != null)
  494. {
  495. }
  496. }
  497. return;
  498. }
  499. }
  500. }