SignatureCreateDialogViewModel.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using ComPDFKitViewer.PdfViewer;
  2. using Microsoft.Win32;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using Prism.Services.Dialogs;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
  20. {
  21. class SignatureCreateDialogViewModel : BindableBase, IDialogAware
  22. {
  23. public string Title => "";
  24. private CPDFViewer PDFViewer;
  25. public event Action<IDialogResult> RequestClose;
  26. public DelegateCommand CancelCommand { get; set; }
  27. public DelegateCommand CreateCommnad { get; set; }
  28. public DelegateCommand<object> CheckedCommnad { get; set; }
  29. public DelegateCommand OpenImageCommnad { get; set; }
  30. public ObservableCollection<string> FontNameList { get; set; }
  31. private int fontNameIndex = 0;
  32. public int FontNameIndex
  33. {
  34. get { return fontNameIndex; }
  35. set
  36. {
  37. SetProperty(ref fontNameIndex, value);
  38. }
  39. }
  40. private int radioButtonIndex;
  41. public int RadioButtonIndex
  42. {
  43. get { return radioButtonIndex; }
  44. set
  45. {
  46. SetProperty(ref radioButtonIndex, value);
  47. }
  48. }
  49. private int tabItemIndex;
  50. public int TabItemIndex
  51. {
  52. get { return tabItemIndex; }
  53. set
  54. {
  55. SetProperty(ref tabItemIndex, value);
  56. }
  57. }
  58. private string inputText = "";
  59. public string InputText
  60. {
  61. get { return inputText; }
  62. set
  63. {
  64. SetProperty(ref inputText, value);
  65. }
  66. }
  67. public string SaveToPath { get; private set; }
  68. public SignatureCreateDialogViewModel()
  69. {
  70. FontNameList = new ObservableCollection<string>();
  71. CancelCommand = new DelegateCommand(Cancel);
  72. CreateCommnad = new DelegateCommand(Create);
  73. CheckedCommnad = new DelegateCommand<object>(Checked);
  74. OpenImageCommnad = new DelegateCommand(OpenImage);
  75. InitFontNameList();
  76. }
  77. private void InitFontNameList()
  78. {
  79. System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
  80. foreach (var item in objFont.Families)
  81. {
  82. FontNameList.Add(item.Name);
  83. }
  84. }
  85. private void Checked(object index)
  86. {
  87. RadioButtonIndex = Convert.ToInt32(index);
  88. }
  89. public void OpenImage()
  90. {
  91. OpenFileDialog openFile = new OpenFileDialog();
  92. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  93. "(*.gif)|*.gif|" +
  94. "(*.jpeg)|*.jpeg|" +
  95. "(*.jpg)|*.jpg|" +
  96. "(*.png)|*.png|" +
  97. "(*.tiff)|*.tiff";
  98. if (openFile.ShowDialog() == false)
  99. {
  100. return;
  101. }
  102. string path = App.CachePath.CustomStampPath;
  103. string name = Guid.NewGuid().ToString();
  104. if (!string.IsNullOrEmpty(path))
  105. {
  106. BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
  107. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  108. scale = Math.Min(scale, 1);
  109. string ext = Path.GetExtension(openFile.FileName);
  110. if (ext.ToUpper() == ".PNG")
  111. {
  112. BitmapEncoder encoder = new PngBitmapEncoder();
  113. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  114. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  115. path = System.IO.Path.Combine(path, name);
  116. using (FileStream stream = new FileStream(path, FileMode.Create))
  117. {
  118. encoder.Save(stream);
  119. }
  120. if (!string.IsNullOrEmpty(SaveToPath))
  121. {
  122. App.CachePath.AddToDeleteFiles(SaveToPath);
  123. }
  124. SaveToPath = path;
  125. }
  126. else
  127. {
  128. BitmapEncoder encoder = new JpegBitmapEncoder();
  129. TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  130. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  131. path = System.IO.Path.Combine(path, name);
  132. using (FileStream stream = new FileStream(path, FileMode.Create))
  133. {
  134. encoder.Save(stream);
  135. }
  136. if (!string.IsNullOrEmpty(SaveToPath))
  137. {
  138. App.CachePath.AddToDeleteFiles(SaveToPath);
  139. }
  140. SaveToPath = path;
  141. }
  142. }
  143. else
  144. {
  145. SaveToPath = "";
  146. }
  147. }
  148. /// <summary>
  149. /// 把文字转换成Bitmap
  150. /// </summary>
  151. /// <param name="text"></param>
  152. /// <param name="font"></param>
  153. /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
  154. /// <param name="fontcolor">字体颜色</param>
  155. /// <param name="backColor">背景颜色</param>
  156. /// <returns></returns>
  157. private Bitmap TextToBitmap(string text, Font font, Rectangle rect, System.Drawing.Color fontcolor, System.Drawing.Color backColor)
  158. {
  159. Graphics g;
  160. Bitmap bmp;
  161. StringFormat format = new StringFormat(StringFormatFlags.NoClip);
  162. if (rect == Rectangle.Empty)
  163. {
  164. bmp = new Bitmap(1, 1);
  165. g = Graphics.FromImage(bmp);
  166. //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
  167. SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
  168. int width = (int)(sizef.Width + 1);
  169. int height = (int)(sizef.Height + 1);
  170. rect = new Rectangle(0, 0, width, height);
  171. bmp.Dispose();
  172. bmp = new Bitmap(width, height);
  173. }
  174. else
  175. {
  176. bmp = new Bitmap(rect.Width, rect.Height);
  177. }
  178. g = Graphics.FromImage(bmp);
  179. //使用ClearType字体功能
  180. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  181. g.FillRectangle(new SolidBrush(backColor), rect);
  182. g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
  183. return bmp;
  184. }
  185. private void Cancel()
  186. {
  187. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  188. }
  189. private void Create()
  190. {
  191. switch (TabItemIndex)
  192. {
  193. case 0:
  194. Bitmap bmp = TextToBitmap(InputText, new Font(FontNameList[fontNameIndex], 48), Rectangle.Empty, System.Drawing.Color.Red, System.Drawing.Color.Transparent);
  195. string guid = Guid.NewGuid().ToString();
  196. string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
  197. bmp.Save(path, ImageFormat.Png);
  198. SaveToPath = path;
  199. break;
  200. case 1:
  201. break;
  202. case 2:
  203. break;
  204. default:
  205. break;
  206. }
  207. DialogParameters valuePairs = new DialogParameters();
  208. valuePairs.Add(ParameterNames.DataModel, this);
  209. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  210. }
  211. public bool CanCloseDialog()
  212. {
  213. return true;
  214. }
  215. public void OnDialogClosed()
  216. {
  217. return;
  218. }
  219. public void OnDialogOpened(IDialogParameters parameters)
  220. {
  221. if (parameters != null)
  222. {
  223. PDFViewer = parameters.GetValue<CPDFViewer>(ParameterNames.PDFViewer);
  224. if (PDFViewer != null)
  225. {
  226. }
  227. }
  228. return;
  229. }
  230. }
  231. }