SignatureCreateDialogViewModel.cs 19 KB

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