CPDFCreateSignatureDialog.xaml.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 System.Windows.Ink;
  16. using Compdfkit_Tools.Common;
  17. using ComPDFKitViewer.Helper;
  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. List<List<System.Windows.Point>> RawPointList = new List<List<System.Windows.Point>>();
  196. for (int kk = 0; kk < DrawinkCanvas.Strokes.Count; kk++)
  197. {
  198. List<System.Windows.Point> p = new List<System.Windows.Point>();
  199. RawPointList.Add(p);
  200. for (int gg = 0; gg < DrawinkCanvas.Strokes[kk].StylusPoints.Count; gg++)
  201. {
  202. var point = DrawinkCanvas.Strokes[kk].StylusPoints[gg].ToPoint();
  203. if (point.X >= 0 && point.Y >= 0)
  204. RawPointList[kk].Add(point);
  205. }
  206. }
  207. DrawingSaveToPath = FreeHandpath;
  208. double inkThickness;
  209. inkThickness = StrokeWidth;
  210. WriteableBitmap writeStamp = RawPointListToBitmap(RawPointList, inkThickness, DrawinkCanvas.DefaultDrawingAttributes.Color);
  211. if (writeStamp==null)
  212. {
  213. return;
  214. }
  215. FreeHandpath = SignaturePath;
  216. string thumbnailName = Guid.NewGuid().ToString();
  217. FreeHandpath = System.IO.Path.Combine(FreeHandpath, thumbnailName);
  218. using (FileStream stream5 = new FileStream(FreeHandpath, FileMode.Create))
  219. {
  220. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  221. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  222. encoder5.Save(stream5);
  223. }
  224. SaveToPath = FreeHandpath;
  225. cPDFSignatureData = new CPDFSignatureData();
  226. cPDFSignatureData.AnnotationType = CPDFAnnotationType.Signature;
  227. cPDFSignatureData.SourcePath = SaveToPath;
  228. cPDFSignatureData.DrawingPath = DrawingSaveToPath;
  229. cPDFSignatureData.Type = SignatureType.Drawing;
  230. cPDFSignatureData.inkThickness = inkThickness;
  231. cPDFSignatureData.inkColor = DrawinkCanvas.DefaultDrawingAttributes.Color;
  232. }
  233. private WriteableBitmap RawPointListToBitmap(List<List<System.Windows.Point>> RawPointList, double InkThickness, System.Windows.Media.Color InkColor)
  234. {
  235. if (RawPointList != null && RawPointList.Count > 0)
  236. {
  237. GeometryGroup PaintGeomtry = new GeometryGroup();
  238. int minLeft = -1;
  239. int minTop = -1;
  240. int maxLeft = -1;
  241. int maxTop = -1;
  242. foreach (List<System.Windows.Point> Item in RawPointList)
  243. {
  244. for (int i = 0; i < Item.Count; i++)
  245. {
  246. System.Windows.Point paintPoint = Item[i];
  247. if (minLeft == -1)
  248. {
  249. minLeft = (int)paintPoint.X;
  250. maxLeft = (int)paintPoint.X;
  251. minTop = (int)paintPoint.Y;
  252. maxTop = (int)paintPoint.Y;
  253. }
  254. else
  255. {
  256. minLeft = (int)Math.Min(minLeft, paintPoint.X);
  257. maxLeft = (int)Math.Max(maxLeft, paintPoint.X);
  258. minTop = (int)Math.Min(minTop, paintPoint.Y);
  259. maxTop = (int)Math.Max(maxTop, paintPoint.Y);
  260. }
  261. }
  262. }
  263. if (minLeft >= 0 && maxLeft > minLeft && minTop >= 0 && maxTop > minTop)
  264. {
  265. List<List<System.Windows.Point>> points = new List<List<System.Windows.Point>>();
  266. foreach (List<System.Windows.Point> Item in RawPointList)
  267. {
  268. PathGeometry PaintPath = new PathGeometry();
  269. PathFigureCollection Figures = new PathFigureCollection();
  270. PathFigure AddFigure = new PathFigure();
  271. Figures.Add(AddFigure);
  272. PaintPath.Figures = Figures;
  273. PaintGeomtry.Children.Add(PaintPath);
  274. List<System.Windows.Point> changeList = new List<System.Windows.Point>();
  275. for (int i = 0; i < Item.Count; i++)
  276. {
  277. System.Windows.Point paintPoint = DpiHelper.CurrentPointToStandardPoint(new System.Windows.Point(Item[i].X - minLeft, Item[i].Y - minTop));
  278. changeList.Add(paintPoint);
  279. if (i == 0)
  280. {
  281. AddFigure.StartPoint = paintPoint;
  282. }
  283. else
  284. {
  285. LineSegment AddSegment = new LineSegment();
  286. AddSegment.Point = paintPoint;
  287. AddFigure.Segments.Add(AddSegment);
  288. }
  289. }
  290. if (changeList.Count > 0)
  291. {
  292. points.Add(changeList);
  293. }
  294. }
  295. int drawWidth = (int)DpiHelper.CurrentNumToStandardNum(maxLeft - minLeft);
  296. int drawHeight = (int)DpiHelper.CurrentNumToStandardNum(maxTop - minTop);
  297. RawPointList = points;
  298. DrawingVisual copyVisual = new DrawingVisual();
  299. DrawingContext copyContext = copyVisual.RenderOpen();
  300. System.Windows.Media.Pen drawPen = new System.Windows.Media.Pen(new SolidColorBrush(InkColor), InkThickness);
  301. copyContext?.DrawGeometry(null, drawPen, PaintGeomtry);
  302. copyContext.Close();
  303. RenderTargetBitmap targetBitmap = new RenderTargetBitmap(drawWidth, drawHeight, 96, 96, PixelFormats.Pbgra32);
  304. targetBitmap.Render(copyVisual);
  305. byte[] ImageArray = new byte[targetBitmap.PixelWidth * targetBitmap.PixelHeight * 4];
  306. targetBitmap.CopyPixels(new Int32Rect(0, 0, (int)targetBitmap.PixelWidth, (int)targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  307. WriteableBitmap writeBitmap = new WriteableBitmap(targetBitmap.PixelWidth, targetBitmap.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  308. writeBitmap.WritePixels(new Int32Rect(0, 0, targetBitmap.PixelWidth, targetBitmap.PixelHeight), ImageArray, targetBitmap.PixelWidth * 4, 0);
  309. return writeBitmap;
  310. }
  311. }
  312. return null;
  313. }
  314. private Bitmap TextToBitmap(string text, string FontFamily, double size, System.Drawing.Rectangle rect, System.Windows.Media.Brush fontcolor, System.Drawing.Color backColor)
  315. {
  316. FormattedText formatText = new FormattedText(
  317. text,
  318. CultureInfo.CurrentCulture,
  319. FlowDirection.LeftToRight,
  320. new Typeface(new System.Windows.Media.FontFamily(FontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
  321. size,
  322. fontcolor
  323. );
  324. DrawingVisual drawingVisual = new DrawingVisual();
  325. DrawingContext dc = drawingVisual.RenderOpen();
  326. dc.DrawText(formatText, new System.Windows.Point(2, 10));
  327. dc.Close();
  328. Rect x = drawingVisual.ContentBounds;
  329. Rect DrawRect = new Rect(0, 0, x.Width + (x.X / 2), x.Height + x.Y);
  330. RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)((int)DrawRect.Width + (x.X / 2)), (int)((int)DrawRect.Height + x.Y), 96F, 96F, PixelFormats.Pbgra32);
  331. renderTargetBitmap.Render(drawingVisual);
  332. MemoryStream stream = new MemoryStream();
  333. BitmapEncoder encoder = new PngBitmapEncoder();
  334. encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
  335. encoder.Save(stream);
  336. Bitmap bitmap = new Bitmap(stream);
  337. return bitmap;
  338. }
  339. private void TextName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  340. {
  341. if (TextName.SelectedIndex != -1 && IsPageLoaded)
  342. {
  343. InPutTextBox.FontFamily = new System.Windows.Media.FontFamily(TextName.SelectionBoxItem.ToString());
  344. }
  345. }
  346. private void Grid_Drop(object sender, DragEventArgs e)
  347. {
  348. string[] file = (string[])e.Data.GetData(DataFormats.FileDrop);
  349. foreach (string f in file)
  350. {
  351. string FileType = System.IO.Path.GetExtension(f).Trim().ToLower();
  352. string type = "*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff";
  353. if (!string.IsNullOrEmpty(FileType))
  354. {
  355. string imagetype = "*" + FileType;
  356. string[] x = type.ToLower().Split(';');
  357. List<string> list = x.ToList();
  358. int imageindex = list.IndexOf(imagetype);
  359. if (imageindex > 0)
  360. {
  361. SaveToImage(f);
  362. }
  363. }
  364. }
  365. }
  366. private void CreateSignatureControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  367. {
  368. TabControl tabControl = sender as TabControl;
  369. if (IsPageLoaded && tabControl != null)
  370. {
  371. switch (tabControl.SelectedIndex)
  372. {
  373. case 0:
  374. if (DrawinkCanvas.Strokes.Count <= 0)
  375. {
  376. SaveBtn.IsEnabled = false;
  377. }
  378. else
  379. {
  380. SaveBtn.IsEnabled = true;
  381. }
  382. break;
  383. case 1:
  384. if (string.IsNullOrEmpty(InPutTextBox.Text))
  385. {
  386. SaveBtn.IsEnabled = false;
  387. }
  388. else
  389. {
  390. SaveBtn.IsEnabled = true;
  391. }
  392. break;
  393. case 2:
  394. if (ImageImage.Source == null)
  395. {
  396. SaveBtn.IsEnabled = false;
  397. }
  398. else
  399. {
  400. SaveBtn.IsEnabled = true;
  401. }
  402. break;
  403. default:
  404. break;
  405. }
  406. }
  407. }
  408. private void InPutTextBox_TextChanged(object sender, TextChangedEventArgs e)
  409. {
  410. if (IsPageLoaded)
  411. {
  412. if (string.IsNullOrEmpty((sender as TextBox).Text))
  413. {
  414. SaveBtn.IsEnabled = false;
  415. }
  416. else
  417. {
  418. SaveBtn.IsEnabled = true;
  419. }
  420. }
  421. }
  422. private void ImageImage_SourceUpdated(object sender, DataTransferEventArgs e)
  423. {
  424. if (IsPageLoaded)
  425. {
  426. if (ImageImage.Source == null)
  427. {
  428. SaveBtn.IsEnabled = false;
  429. }
  430. else
  431. {
  432. SaveBtn.IsEnabled = true;
  433. }
  434. }
  435. }
  436. private void DrawinkCanvas_Unloaded(object sender, RoutedEventArgs e)
  437. {
  438. DrawinkCanvas.Strokes.StrokesChanged -= Strokes_StrokesChanged;
  439. }
  440. private void DrawinkCanvas_Loaded(object sender, RoutedEventArgs e)
  441. {
  442. DrawinkCanvas.Strokes.StrokesChanged += Strokes_StrokesChanged;
  443. }
  444. private void Strokes_StrokesChanged(object sender, StrokeCollectionChangedEventArgs e)
  445. {
  446. if (IsPageLoaded)
  447. {
  448. if (DrawinkCanvas.Strokes.Count <= 0)
  449. {
  450. SaveBtn.IsEnabled = false;
  451. }
  452. else
  453. {
  454. SaveBtn.IsEnabled = true;
  455. }
  456. }
  457. }
  458. private void StrokeWidth_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  459. {
  460. if (IsPageLoaded)
  461. {
  462. DrawinkCanvas.DefaultDrawingAttributes.Height = DrawinkCanvas.DefaultDrawingAttributes.Width = StrokeHigh = StrokeWidth = e.NewValue;
  463. UpDataToStrokesObject();
  464. }
  465. }
  466. private void ColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  467. {
  468. ColorPickerControl.SetIsChecked(1);
  469. }
  470. private void TextColorPickerControl_Loaded(object sender, RoutedEventArgs e)
  471. {
  472. TextColorPickerControl.SetIsChecked(1);
  473. }
  474. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  475. {
  476. if (!string.IsNullOrEmpty((sender as TextBox).Text))
  477. {
  478. int num;
  479. int.TryParse((sender as TextBox).Text, out num);
  480. if (num > StrokeWidthSlider.Maximum)
  481. {
  482. (sender as TextBox).Text = StrokeWidthSlider.Maximum.ToString();
  483. }
  484. if (num < StrokeWidthSlider.Minimum)
  485. {
  486. (sender as TextBox).Text = StrokeWidthSlider.Minimum.ToString();
  487. }
  488. }
  489. }
  490. }
  491. }