CPDFCreateSignatureDialog.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using Compdfkit_Tools.Data;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing.Imaging;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Globalization;
  15. using ComPDFKitViewer.AnnotEvent;
  16. using System.Windows.Ink;
  17. using Compdfkit_Tools.Common;
  18. namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
  19. {
  20. public partial class CPDFCreateSignatureDialog : Window
  21. {
  22. SolidColorBrush solidColorBrush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0xFF, 0xFF, 0x00, 0x00));
  23. public CPDFSignatureData cPDFSignatureData;
  24. private string SaveToPath;
  25. private string SignaturePath;
  26. private string DrawingSaveToPath;
  27. private double StrokeWidth = 3;
  28. private double StrokeHigh = 3;
  29. private bool IsPageLoaded = false;
  30. public CPDFCreateSignatureDialog()
  31. {
  32. InitializeComponent();
  33. }
  34. private void Window_Loaded(object sender, RoutedEventArgs e)
  35. {
  36. SignaturePath = System.IO.Path.Combine(Environment.CurrentDirectory, "ComPDFKit");
  37. SignaturePath = System.IO.Path.Combine(SignaturePath, "Signature");
  38. System.IO.DirectoryInfo directoryInfo = System.IO.Directory.CreateDirectory(SignaturePath);
  39. IsPageLoaded = true;
  40. DrawinkCanvas.DefaultDrawingAttributes.Width = StrokeWidth;
  41. DrawinkCanvas.DefaultDrawingAttributes.Height = StrokeHigh;
  42. DrawinkCanvas.DefaultDrawingAttributes.Color = solidColorBrush.Color;
  43. InPutTextBox.Foreground = solidColorBrush;
  44. }
  45. private void Cancel_Click(object sender, RoutedEventArgs e)
  46. {
  47. this.Close();
  48. }
  49. private void Save_Click(object sender, RoutedEventArgs e)
  50. {
  51. switch (CreateSignatureControl.SelectedIndex)
  52. {
  53. case 0:
  54. SaveDrawSignature();
  55. break;
  56. case 1:
  57. SaveTextSignature();
  58. break;
  59. case 2:
  60. SaveImageSignature();
  61. break;
  62. default:
  63. break;
  64. }
  65. this.Close();
  66. }
  67. private void Clear_Click(object sender, RoutedEventArgs e)
  68. {
  69. ColorPickerControl.SetIsChecked(1);
  70. TextColorPickerControl.SetIsChecked(1);
  71. DrawinkCanvas.Strokes.Clear();
  72. InPutTextBox.Text = "";
  73. ImageImage.Source = null;
  74. AddImageBackground.Visibility = Visibility.Visible;
  75. SaveBtn.IsEnabled = false;
  76. DrawinkCanvas.DefaultDrawingAttributes.Color = solidColorBrush.Color;
  77. InPutTextBox.Foreground = solidColorBrush;
  78. if (CreateSignatureControl.SelectedIndex==1)
  79. {
  80. InPutTextBox.Focus();
  81. }
  82. }
  83. private void UpDataToStrokesObject()
  84. {
  85. foreach (var item in DrawinkCanvas.Strokes)
  86. {
  87. item.DrawingAttributes = DrawinkCanvas.DefaultDrawingAttributes.Clone();
  88. }
  89. }
  90. private void SaveToImage(string FilePath)
  91. {
  92. string path = SignaturePath;
  93. string name = Guid.NewGuid().ToString();
  94. if (!string.IsNullOrEmpty(path))
  95. {
  96. try
  97. {
  98. BitmapImage image = new BitmapImage(new Uri(FilePath));
  99. double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
  100. scale = Math.Min(scale, 1);
  101. BitmapEncoder encoder = new PngBitmapEncoder();
  102. var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
  103. encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
  104. path = System.IO.Path.Combine(path, name);
  105. using (FileStream stream = new FileStream(path, FileMode.Create))
  106. {
  107. encoder.Save(stream);
  108. }
  109. if (!string.IsNullOrEmpty(SaveToPath))
  110. {
  111. DirectoryInfo CreatedFilePathFolder = new DirectoryInfo(SaveToPath);
  112. if (CreatedFilePathFolder.Exists)
  113. {
  114. Directory.Delete(SaveToPath, true);
  115. }
  116. }
  117. SaveToPath = path;
  118. AddImageBackground.Visibility = Visibility.Collapsed;
  119. ImageImage.Source = targetBitmap;
  120. SaveBtn.IsEnabled = true;
  121. }
  122. catch
  123. {
  124. }
  125. }
  126. else
  127. {
  128. SaveToPath = "";
  129. }
  130. }
  131. private void OpenImage_Click(object sender, RoutedEventArgs e)
  132. {
  133. OpenFileDialog openFile = new OpenFileDialog();
  134. openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
  135. "(*.gif)|*.gif|" +
  136. "(*.jpeg)|*.jpeg|" +
  137. "(*.jpg)|*.jpg|" +
  138. "(*.png)|*.png|" +
  139. "(*.tiff)|*.tiff";
  140. if (openFile.ShowDialog() == false)
  141. {
  142. return;
  143. }
  144. SaveToImage(openFile.FileName);
  145. }
  146. private void ColorPickerControl_ColorChanged(object sender, EventArgs e)
  147. {
  148. if (IsPageLoaded)
  149. {
  150. DrawinkCanvas.DefaultDrawingAttributes.Color = ((SolidColorBrush)ColorPickerControl.Brush).Color;
  151. UpDataToStrokesObject();
  152. }
  153. }
  154. private void TextColorPickerControl_ColorChanged(object sender, EventArgs e)
  155. {
  156. if (IsPageLoaded)
  157. {
  158. InPutTextBox.Foreground = TextColorPickerControl.Brush;
  159. }
  160. }
  161. private void SaveTextSignature()
  162. {
  163. if (string.IsNullOrEmpty(InPutTextBox.Text))
  164. {
  165. return;
  166. }
  167. System.Windows.Media.Brush fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
  168. fontcolor = TextColorPickerControl.Brush;
  169. Bitmap bmp = TextToBitmap(InPutTextBox.Text, TextName.SelectionBoxItem.ToString(), 50, System.Drawing.Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
  170. string guid = Guid.NewGuid().ToString();
  171. string path = System.IO.Path.Combine(SignaturePath, guid);
  172. bmp.Save(path, ImageFormat.Png);
  173. SaveToPath = path;
  174. cPDFSignatureData = new CPDFSignatureData();
  175. cPDFSignatureData.SourcePath = SaveToPath;
  176. cPDFSignatureData.AnnotationType = CPDFAnnotationType.Signature;
  177. cPDFSignatureData.Type = SignatureType.TextType;
  178. }
  179. private void SaveImageSignature()
  180. {
  181. cPDFSignatureData = new CPDFSignatureData();
  182. cPDFSignatureData.SourcePath = SaveToPath;
  183. cPDFSignatureData.AnnotationType = CPDFAnnotationType.Signature;
  184. cPDFSignatureData.Type = SignatureType.ImageType;
  185. }
  186. private void SaveDrawSignature()
  187. {
  188. var FreeHandpath = SignaturePath;
  189. string name = Guid.NewGuid().ToString();
  190. FreeHandpath = System.IO.Path.Combine(FreeHandpath, name);
  191. using (System.IO.FileStream stream = new System.IO.FileStream(FreeHandpath, System.IO.FileMode.Create))
  192. {
  193. DrawinkCanvas.Strokes.Save(stream);
  194. }
  195. StampAnnotArgs stampArgs = new StampAnnotArgs();
  196. List<List<System.Windows.Point>> RawPointList = new List<List<System.Windows.Point>>();
  197. for (int kk = 0; kk < DrawinkCanvas.Strokes.Count; kk++)
  198. {
  199. List<System.Windows.Point> p = new List<System.Windows.Point>();
  200. RawPointList.Add(p);
  201. for (int gg = 0; gg < DrawinkCanvas.Strokes[kk].StylusPoints.Count; gg++)
  202. {
  203. var point = DrawinkCanvas.Strokes[kk].StylusPoints[gg].ToPoint();
  204. if (point.X >= 0 && point.Y >= 0)
  205. RawPointList[kk].Add(point);
  206. }
  207. }
  208. DrawingSaveToPath = FreeHandpath;
  209. double inkThickness;
  210. inkThickness = StrokeWidth;
  211. stampArgs.SetInkData(RawPointList, inkThickness, DrawinkCanvas.DefaultDrawingAttributes.Color);
  212. var writeStamp = stampArgs.GetStampDrawing();
  213. FreeHandpath = SignaturePath;
  214. string thumbnailName = Guid.NewGuid().ToString();
  215. FreeHandpath = System.IO.Path.Combine(FreeHandpath, thumbnailName);
  216. using (FileStream stream5 = new FileStream(FreeHandpath, FileMode.Create))
  217. {
  218. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  219. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  220. encoder5.Save(stream5);
  221. }
  222. SaveToPath = FreeHandpath;
  223. cPDFSignatureData = new CPDFSignatureData();
  224. cPDFSignatureData.AnnotationType = CPDFAnnotationType.Signature;
  225. cPDFSignatureData.SourcePath = SaveToPath;
  226. cPDFSignatureData.DrawingPath = DrawingSaveToPath;
  227. cPDFSignatureData.Type = SignatureType.Drawing;
  228. cPDFSignatureData.inkThickness = inkThickness;
  229. cPDFSignatureData.inkColor = DrawinkCanvas.DefaultDrawingAttributes.Color;
  230. }
  231. private Bitmap TextToBitmap(string text, string FontFamily, double size, System.Drawing.Rectangle rect, System.Windows.Media.Brush fontcolor, System.Drawing.Color backColor)
  232. {
  233. FormattedText formatText = new FormattedText(
  234. text,
  235. CultureInfo.CurrentCulture,
  236. FlowDirection.LeftToRight,
  237. new Typeface(new System.Windows.Media.FontFamily(FontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
  238. size,
  239. fontcolor
  240. );
  241. DrawingVisual drawingVisual = new DrawingVisual();
  242. DrawingContext dc = drawingVisual.RenderOpen();
  243. dc.DrawText(formatText, new System.Windows.Point(2, 10));
  244. dc.Close();
  245. Rect x = drawingVisual.ContentBounds;
  246. Rect DrawRect = new Rect(0, 0, x.Width + (x.X / 2), x.Height + x.Y);
  247. RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)((int)DrawRect.Width + (x.X / 2)), (int)((int)DrawRect.Height + x.Y), 96F, 96F, PixelFormats.Pbgra32);
  248. renderTargetBitmap.Render(drawingVisual);
  249. MemoryStream stream = new MemoryStream();
  250. BitmapEncoder encoder = new PngBitmapEncoder();
  251. encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
  252. encoder.Save(stream);
  253. Bitmap bitmap = new Bitmap(stream);
  254. return bitmap;
  255. }
  256. private void TextName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  257. {
  258. if (TextName.SelectedIndex != -1 && IsPageLoaded)
  259. {
  260. InPutTextBox.FontFamily = new System.Windows.Media.FontFamily(TextName.SelectionBoxItem.ToString());
  261. }
  262. }
  263. private void Grid_Drop(object sender, DragEventArgs e)
  264. {
  265. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  266. foreach (string f in file)
  267. {
  268. string FileType = System.IO.Path.GetExtension(f).Trim().ToLower();
  269. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  270. if (!string.IsNullOrEmpty(FileType))
  271. {
  272. string imagetype = "*" + FileType;
  273. string[] x = type.ToLower().Split(';');
  274. List<string> list = x.ToList();
  275. int imageindex = list.IndexOf(imagetype);
  276. if (imageindex > 0)
  277. {
  278. SaveToImage(f);
  279. }
  280. }
  281. }
  282. }
  283. private void CreateSignatureControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  284. {
  285. TabControl tabControl = sender as TabControl;
  286. if (IsPageLoaded && tabControl != null)
  287. {
  288. switch (tabControl.SelectedIndex)
  289. {
  290. case 0:
  291. if (DrawinkCanvas.Strokes.Count <= 0)
  292. {
  293. SaveBtn.IsEnabled = false;
  294. }
  295. else
  296. {
  297. SaveBtn.IsEnabled = true;
  298. }
  299. break;
  300. case 1:
  301. if (string.IsNullOrEmpty(InPutTextBox.Text))
  302. {
  303. SaveBtn.IsEnabled = false;
  304. }
  305. else
  306. {
  307. SaveBtn.IsEnabled = true;
  308. }
  309. break;
  310. case 2:
  311. if (ImageImage.Source == null)
  312. {
  313. SaveBtn.IsEnabled = false;
  314. }
  315. else
  316. {
  317. SaveBtn.IsEnabled = true;
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. }
  325. private void InPutTextBox_TextChanged(object sender, TextChangedEventArgs e)
  326. {
  327. if (IsPageLoaded)
  328. {
  329. if (string.IsNullOrEmpty((sender as TextBox).Text))
  330. {
  331. SaveBtn.IsEnabled = false;
  332. }
  333. else
  334. {
  335. SaveBtn.IsEnabled = true;
  336. }
  337. }
  338. }
  339. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  340. {
  341. if (IsPageLoaded)
  342. {
  343. if (ImageImage.Source == null)
  344. {
  345. SaveBtn.IsEnabled = false;
  346. }
  347. else
  348. {
  349. SaveBtn.IsEnabled = true;
  350. }
  351. }
  352. }
  353. private void DrawinkCanvas_Unloaded(object sender, RoutedEventArgs e)
  354. {
  355. DrawinkCanvas.Strokes.StrokesChanged -= Strokes_StrokesChanged;
  356. }
  357. private void DrawinkCanvas_Loaded(object sender, RoutedEventArgs e)
  358. {
  359. DrawinkCanvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
  360. }
  361. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  362. {
  363. if (IsPageLoaded)
  364. {
  365. if (DrawinkCanvas.Strokes.Count <= 0)
  366. {
  367. SaveBtn.IsEnabled = false;
  368. }
  369. else
  370. {
  371. SaveBtn.IsEnabled = true;
  372. }
  373. }
  374. }
  375. private void StrokeWidth_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  376. {
  377. if (IsPageLoaded)
  378. {
  379. DrawinkCanvas.DefaultDrawingAttributes.Height = DrawinkCanvas.DefaultDrawingAttributes.Width = StrokeHigh = StrokeWidth = e.NewValue;
  380. UpDataToStrokesObject();
  381. }
  382. }
  383. private void ColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  384. {
  385. ColorPickerControl.SetIsChecked(1);
  386. }
  387. private void TextColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  388. {
  389. TextColorPickerControl.SetIsChecked(1);
  390. }
  391. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  392. {
  393. if (!string.IsNullOrEmpty((sender as TextBox).Text))
  394. {
  395. int num;
  396. int.TryParse((sender as TextBox).Text, out num);
  397. if (num > StrokeWidthSlider.Maximum)
  398. {
  399. (sender as TextBox).Text = StrokeWidthSlider.Maximum.ToString();
  400. }
  401. if (num < StrokeWidthSlider.Minimum)
  402. {
  403. (sender as TextBox).Text = StrokeWidthSlider.Minimum.ToString();
  404. }
  405. }
  406. }
  407. }
  408. }