CommonHelper.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Runtime.CompilerServices;
  13. using System.Text.RegularExpressions;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Interop;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Xml;
  20. using ComPDFKit.DigitalSign;
  21. using ComPDFKit.PDFAnnotation.Form;
  22. using Point = System.Windows.Point;
  23. using Size = System.Windows.Size;
  24. using System.Collections.ObjectModel;
  25. using Brush = System.Windows.Media.Brush;
  26. using Color = System.Windows.Media.Color;
  27. using System.Drawing.Drawing2D;
  28. using Matrix = System.Windows.Media.Matrix;
  29. using System.Drawing.Imaging;
  30. using System.Runtime.InteropServices;
  31. using ComPDFKit.NativeMethod;
  32. namespace Compdfkit_Tools.Helper
  33. {
  34. public class SDKLicenseHelper
  35. {
  36. public string key = string.Empty;
  37. public string secret = string.Empty;
  38. public SDKLicenseHelper()
  39. {
  40. string sdkLicensePath = "license_key_windows.xml";
  41. XmlDocument xmlDocument = new XmlDocument();
  42. xmlDocument.Load(sdkLicensePath);
  43. XmlNode keyNode = xmlDocument.SelectSingleNode("/license/key");
  44. XmlNode secretNode = xmlDocument.SelectSingleNode("/license/secret");
  45. if (keyNode != null && secretNode != null)
  46. {
  47. key = keyNode.InnerText;
  48. secret = secretNode.InnerText;
  49. }
  50. else
  51. {
  52. Console.WriteLine("Key or secret element not found in the XML.");
  53. }
  54. }
  55. }
  56. public static class CommonHelper
  57. {
  58. public static Bitmap ConvertTo32bppArgb(Bitmap source)
  59. {
  60. // Create a new Bitmap with 32bppArgb pixel format
  61. Bitmap newBitmap = new Bitmap(source.Width, source.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  62. // Create a Graphics object to draw the source image on the new Bitmap
  63. using (Graphics g = Graphics.FromImage(newBitmap))
  64. {
  65. // Set the interpolation mode and pixel offset mode for high-quality rendering
  66. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  67. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  68. g.SmoothingMode = SmoothingMode.HighQuality;
  69. // Draw the source image on the new Bitmap
  70. g.DrawImage(source, new System.Drawing.Rectangle(0, 0, source.Width, source.Height));
  71. }
  72. return newBitmap;
  73. }
  74. public static byte[] ConvertBrushToByteArray(Brush brush)
  75. {
  76. if (brush is SolidColorBrush solidColorBrush)
  77. {
  78. Color color = solidColorBrush.Color;
  79. byte[] colorBytes = new byte[3];
  80. colorBytes[0] = color.R;
  81. colorBytes[1] = color.G;
  82. colorBytes[2] = color.B;
  83. return colorBytes;
  84. }
  85. else
  86. {
  87. throw new ArgumentException("The provided brush is not a SolidColorBrush.");
  88. }
  89. }
  90. public static int GetBitmapPointer(Bitmap bitmap)
  91. {
  92. IntPtr hBitmap = bitmap.GetHbitmap();
  93. int bitmapPointer = hBitmap.ToInt32();
  94. return bitmapPointer;
  95. }
  96. public static string EmailPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
  97. public static bool IsValidEmail(string email)
  98. {
  99. string emailPattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
  100. return Regex.IsMatch(email, emailPattern);
  101. }
  102. public static string GetExactDateFromString(string dateString)
  103. {
  104. DateTime dateTime = GetDateTimeFromString(dateString);
  105. return dateTime.ToString("yyyy/MM/dd HH:mm:ss");
  106. }
  107. public static DateTime GetDateTimeFromString(string dateString)
  108. {
  109. int start = 0;
  110. for (int i = 0; i < dateString.Length; i++)
  111. {
  112. if (char.IsNumber(dateString[i]))
  113. {
  114. start = i;
  115. break;
  116. }
  117. }
  118. string date = dateString.Substring(start, 14);
  119. string year = date.Substring(0, 4);
  120. string month = date.Substring(4, 2);
  121. string day = date.Substring(6, 2);
  122. string hour = date.Substring(8, 2);
  123. string minute = date.Substring(10, 2);
  124. string second = date.Substring(12, 2);
  125. return new DateTime(int.Parse(year), int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute), int.Parse(second));
  126. }
  127. /// <summary>
  128. /// Returns the file size based on the specified file path, with the smallest unit being bytes (B).
  129. /// </summary>
  130. /// <param name="filePath">The path to the file.</param>
  131. /// <returns>
  132. /// The calculated file size, with units in bytes (B), kilobytes (KB), megabytes (MB), or gigabytes (GB).
  133. /// </returns>
  134. public static string GetFileSize(string filePath)
  135. {
  136. try
  137. {
  138. long fileSize = new FileInfo(filePath).Length;
  139. string[] sizes = { "B", "KB", "MB", "GB" };
  140. int order = 0;
  141. while (fileSize >= 1024 && order < sizes.Length - 1)
  142. {
  143. fileSize /= 1024;
  144. order++;
  145. }
  146. return $"{fileSize} {sizes[order]}";
  147. }
  148. catch
  149. {
  150. return "0B";
  151. }
  152. }
  153. public static string GetExistedPathOrEmpty(string filter = "PDF files (*.pdf)|*.pdf")
  154. {
  155. string selectedFilePath = string.Empty;
  156. OpenFileDialog openFileDialog;
  157. try
  158. {
  159. openFileDialog = new OpenFileDialog
  160. {
  161. Filter = filter
  162. };
  163. }
  164. catch
  165. {
  166. return string.Empty;
  167. };
  168. if (openFileDialog.ShowDialog() == true)
  169. {
  170. selectedFilePath = openFileDialog.FileName;
  171. }
  172. return selectedFilePath;
  173. }
  174. public static string GetGeneratePathOrEmpty(string filter, string defaultFileName = "")
  175. {
  176. string selectedFilePath = string.Empty;
  177. SaveFileDialog saveFileDialog = new SaveFileDialog
  178. {
  179. Filter = filter,
  180. FileName = defaultFileName
  181. };
  182. if (saveFileDialog.ShowDialog() == true)
  183. {
  184. selectedFilePath = saveFileDialog.FileName;
  185. }
  186. return selectedFilePath;
  187. }
  188. public static string GetPageParmFromList(List<int> pagesList)
  189. {
  190. string pageParam = "";
  191. if (pagesList.Count != 0)
  192. {
  193. pagesList.Sort();
  194. for (int i = 0; i < pagesList.Count; i++)
  195. {
  196. if (i == 0)
  197. {
  198. pageParam += pagesList[0].ToString();
  199. }
  200. else
  201. {
  202. if (pagesList[i] == pagesList[i - 1] + 1)
  203. {
  204. if (i >= 2)
  205. {
  206. if (pagesList[i - 1] != pagesList[i - 2] + 1)
  207. pageParam += "-";
  208. }
  209. else
  210. pageParam += "-";
  211. if (i == pagesList.Count - 1)
  212. {
  213. pageParam += pagesList[i].ToString();
  214. }
  215. }
  216. else
  217. {
  218. if (i >= 2)
  219. {
  220. if (pagesList[i - 1] == pagesList[i - 2] + 1)
  221. pageParam += pagesList[i - 1].ToString();
  222. }
  223. pageParam += "," + pagesList[i].ToString();
  224. }
  225. }
  226. }
  227. }
  228. return pageParam;
  229. }
  230. public static List<int> GetDefaultPageList(CPDFDocument document)
  231. {
  232. List<int> pageRangeList = new List<int>();
  233. for (int i = 0; i < document.PageCount; i++)
  234. {
  235. pageRangeList.Add(i + 1);
  236. }
  237. return pageRangeList;
  238. }
  239. public static bool GetPagesInRange(ref List<int> pageList, string pageRange, int count, char[] enumerationSeparator, char[] rangeSeparator, bool inittag = false)
  240. {
  241. if (pageRange == null || pageList == null)
  242. {
  243. return false;
  244. }
  245. pageList.Clear();
  246. int starttag = inittag ? 0 : 1;
  247. string[] rangeSplit = pageRange.Split(enumerationSeparator);
  248. foreach (string range in rangeSplit)
  249. {
  250. if (range.Contains("-"))
  251. {
  252. string[] limits = range.Split(rangeSeparator);
  253. if (limits.Length == 2 && int.TryParse(limits[0], out int start) && int.TryParse(limits[1], out int end))
  254. {
  255. if (start < starttag || end > count || start > end)
  256. {
  257. return false;
  258. }
  259. for (int i = start; i <= end; i++)
  260. {
  261. if (pageList.Contains(i - 1))
  262. {
  263. return false;
  264. }
  265. pageList.Add(i - 1);
  266. }
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. }
  273. else if (int.TryParse(range, out int pageNr))
  274. {
  275. if (pageNr < starttag || pageNr > count)
  276. {
  277. return false;
  278. }
  279. if (pageList.Contains(pageNr - 1))
  280. {
  281. return false;
  282. }
  283. pageList.Add(pageNr - 1);
  284. }
  285. else
  286. {
  287. return false;
  288. }
  289. }
  290. return true;
  291. }
  292. internal static byte[] ConvertBitmapToByteArray(Bitmap bitmap)
  293. {
  294. BitmapData bmpdata = null;
  295. try
  296. {
  297. bmpdata = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
  298. int numbytes = bmpdata.Stride * bitmap.Height;
  299. byte[] bytedata = new byte[numbytes];
  300. IntPtr ptr = bmpdata.Scan0;
  301. Marshal.Copy(ptr, bytedata, 0, numbytes);
  302. return bytedata;
  303. }
  304. finally
  305. {
  306. if (bmpdata != null)
  307. bitmap.UnlockBits(bmpdata);
  308. }
  309. }
  310. internal static class PageEditHelper
  311. {
  312. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  313. {
  314. while (obj != null)
  315. {
  316. if (obj is T)
  317. return obj as T;
  318. obj = VisualTreeHelper.GetParent(obj);
  319. }
  320. return null;
  321. }
  322. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  323. where childItem : DependencyObject
  324. {
  325. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  326. {
  327. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  328. if (child != null && child is childItem)
  329. return (childItem)child;
  330. else
  331. {
  332. childItem childOfChild = FindVisualChild<childItem>(child);
  333. if (childOfChild != null)
  334. return childOfChild;
  335. }
  336. }
  337. return null;
  338. }
  339. }
  340. public static class ViewportHelper
  341. {
  342. public static CPDFDocument CopyDoc;
  343. public static bool IsInViewport(ScrollViewer sv, Control item)
  344. {
  345. if (item == null) return false;
  346. ItemsControl itemsControl = null;
  347. if (item is ListBoxItem)
  348. itemsControl = ItemsControl.ItemsControlFromItemContainer(item) as ListBox;
  349. else
  350. throw new NotSupportedException(item.GetType().Name);
  351. ScrollContentPresenter scrollContentPresenter = (ScrollContentPresenter)sv.Template.FindName("PART_ScrollContentPresenter", sv);
  352. MethodInfo isInViewportMethod = sv.GetType().GetMethod("IsInViewport", BindingFlags.NonPublic | BindingFlags.Instance);
  353. return (bool)isInViewportMethod.Invoke(sv, new object[] { scrollContentPresenter, item });
  354. }
  355. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  356. {
  357. while (obj != null)
  358. {
  359. if (obj is T)
  360. return obj as T;
  361. obj = VisualTreeHelper.GetParent(obj);
  362. }
  363. return null;
  364. }
  365. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  366. where childItem : DependencyObject
  367. {
  368. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  369. {
  370. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  371. if (child != null && child is childItem)
  372. return (childItem)child;
  373. else
  374. {
  375. childItem childOfChild = FindVisualChild<childItem>(child);
  376. if (childOfChild != null)
  377. return childOfChild;
  378. }
  379. }
  380. return null;
  381. }
  382. }
  383. public class ArrowHelper
  384. {
  385. public bool HasStartArrow
  386. {
  387. get
  388. {
  389. if (StartSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && StartSharp != C_LINE_TYPE.LINETYPE_NONE)
  390. {
  391. return true;
  392. }
  393. return false;
  394. }
  395. }
  396. public bool IsStartClosed
  397. {
  398. get
  399. {
  400. if (StartSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || StartSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  401. {
  402. return true;
  403. }
  404. return false;
  405. }
  406. }
  407. public bool HasEndArrow
  408. {
  409. get
  410. {
  411. if (EndSharp != C_LINE_TYPE.LINETYPE_UNKNOWN && EndSharp != C_LINE_TYPE.LINETYPE_NONE)
  412. {
  413. return true;
  414. }
  415. return false;
  416. }
  417. }
  418. public bool IsEndClosed
  419. {
  420. get
  421. {
  422. if (EndSharp == C_LINE_TYPE.LINETYPE_CLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_RCLOSEDARROW || EndSharp == C_LINE_TYPE.LINETYPE_DIAMOND)
  423. {
  424. return true;
  425. }
  426. return false;
  427. }
  428. }
  429. public uint ArrowAngle { get; set; }
  430. public uint ArrowLength { get; set; }
  431. public Point? LineStart { get; set; }
  432. public Point? LineEnd { get; set; }
  433. public PathGeometry Body { get; set; }
  434. public C_LINE_TYPE StartSharp { get; set; }
  435. public C_LINE_TYPE EndSharp { get; set; }
  436. public ArrowHelper()
  437. {
  438. Body = new PathGeometry();
  439. ArrowLength = 12;
  440. ArrowAngle = 60;
  441. }
  442. protected PathFigure CreateLineBody()
  443. {
  444. if (LineStart != null && LineEnd != null)
  445. {
  446. PathFigure lineFigure = new PathFigure();
  447. lineFigure.StartPoint = (Point)LineStart;
  448. LineSegment linePath = new LineSegment();
  449. linePath.Point = (Point)LineEnd;
  450. lineFigure.Segments.Add(linePath);
  451. return lineFigure;
  452. }
  453. return null;
  454. }
  455. protected PathFigure CreateStartArrow()
  456. {
  457. switch (StartSharp)
  458. {
  459. case C_LINE_TYPE.LINETYPE_NONE:
  460. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  461. break;
  462. case C_LINE_TYPE.LINETYPE_ARROW:
  463. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  464. return CreateStartOpenArrow();
  465. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  466. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  467. return CreateStartReverseArrow();
  468. case C_LINE_TYPE.LINETYPE_BUTT:
  469. return CreateStartButtArrow();
  470. case C_LINE_TYPE.LINETYPE_DIAMOND:
  471. return CreateStartDiamondArrow();
  472. case C_LINE_TYPE.LINETYPE_CIRCLE:
  473. return CreateStartRoundArrow();
  474. case C_LINE_TYPE.LINETYPE_SQUARE:
  475. return CreateStartSquareArrow();
  476. case C_LINE_TYPE.LINETYPE_SLASH:
  477. return CreateStartSlashArrow();
  478. default:
  479. break;
  480. }
  481. return null;
  482. }
  483. protected virtual PathFigure CreateEndArrow()
  484. {
  485. switch (EndSharp)
  486. {
  487. case C_LINE_TYPE.LINETYPE_NONE:
  488. case C_LINE_TYPE.LINETYPE_UNKNOWN:
  489. break;
  490. case C_LINE_TYPE.LINETYPE_ARROW:
  491. case C_LINE_TYPE.LINETYPE_CLOSEDARROW:
  492. return CreateEndOpenArrow();
  493. case C_LINE_TYPE.LINETYPE_ROPENARROW:
  494. case C_LINE_TYPE.LINETYPE_RCLOSEDARROW:
  495. return CreateEndReverseArrow();
  496. case C_LINE_TYPE.LINETYPE_BUTT:
  497. return CreateEndButtArrow();
  498. case C_LINE_TYPE.LINETYPE_DIAMOND:
  499. return CreateEndDiamondArrow();
  500. case C_LINE_TYPE.LINETYPE_CIRCLE:
  501. return CreateEndRoundArrow();
  502. case C_LINE_TYPE.LINETYPE_SQUARE:
  503. return CreateEndSquareArrow();
  504. case C_LINE_TYPE.LINETYPE_SLASH:
  505. return CreateEndSlashArrow();
  506. default:
  507. break;
  508. }
  509. return null;
  510. }
  511. public PathGeometry BuildArrowBody()
  512. {
  513. Body.Figures.Clear();
  514. PathFigure lineBody = CreateLineBody();
  515. if (lineBody != null)
  516. {
  517. Body.Figures.Add(lineBody);
  518. PathFigure arrowFigure = CreateStartArrow();
  519. if (arrowFigure != null)
  520. {
  521. Body.Figures.Add(arrowFigure);
  522. }
  523. arrowFigure = CreateEndArrow();
  524. if (arrowFigure != null)
  525. {
  526. Body.Figures.Add(arrowFigure);
  527. }
  528. }
  529. return Body;
  530. }
  531. private PathFigure CreateStartOpenArrow()
  532. {
  533. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  534. {
  535. return null;
  536. }
  537. PathFigure arrowFigure = new PathFigure();
  538. PolyLineSegment arrowSegment = new PolyLineSegment();
  539. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  540. lineVector.Normalize();
  541. lineVector *= ArrowLength;
  542. Matrix rotateMatrix = new Matrix();
  543. rotateMatrix.Rotate(ArrowAngle / 2);
  544. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  545. arrowSegment.Points.Add((Point)LineStart);
  546. rotateMatrix.Rotate(-ArrowAngle);
  547. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  548. arrowFigure.Segments.Add(arrowSegment);
  549. arrowFigure.IsClosed = IsStartClosed;
  550. arrowFigure.IsFilled = IsStartClosed;
  551. return arrowFigure;
  552. }
  553. private PathFigure CreateEndOpenArrow()
  554. {
  555. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  556. {
  557. return null;
  558. }
  559. PathFigure arrowFigure = new PathFigure();
  560. PolyLineSegment arrowSegment = new PolyLineSegment();
  561. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  562. lineVector.Normalize();
  563. lineVector *= ArrowLength;
  564. Matrix rotateMatrix = new Matrix();
  565. rotateMatrix.Rotate(ArrowAngle / 2);
  566. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  567. arrowSegment.Points.Add((Point)LineEnd);
  568. rotateMatrix.Rotate(-ArrowAngle);
  569. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  570. arrowFigure.Segments.Add(arrowSegment);
  571. arrowFigure.IsClosed = IsEndClosed;
  572. arrowFigure.IsFilled = IsEndClosed;
  573. return arrowFigure;
  574. }
  575. private PathFigure CreateStartReverseArrow()
  576. {
  577. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  578. {
  579. return null;
  580. }
  581. PathFigure arrowFigure = new PathFigure();
  582. PolyLineSegment arrowSegment = new PolyLineSegment();
  583. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  584. lineVector.Normalize();
  585. lineVector *= ArrowLength;
  586. Matrix rotateMatrix = new Matrix();
  587. rotateMatrix.Rotate(ArrowAngle / 2);
  588. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  589. arrowSegment.Points.Add((Point)LineStart);
  590. rotateMatrix.Rotate(-ArrowAngle);
  591. arrowSegment.Points.Add((Point)LineStart + (lineVector * rotateMatrix));
  592. arrowFigure.Segments.Add(arrowSegment);
  593. arrowFigure.IsClosed = IsStartClosed;
  594. arrowFigure.IsFilled = IsStartClosed;
  595. return arrowFigure;
  596. }
  597. private PathFigure CreateEndReverseArrow()
  598. {
  599. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null || ArrowAngle == 0)
  600. {
  601. return null;
  602. }
  603. PathFigure arrowFigure = new PathFigure();
  604. PolyLineSegment arrowSegment = new PolyLineSegment();
  605. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  606. lineVector.Normalize();
  607. lineVector *= ArrowLength;
  608. Matrix rotateMatrix = new Matrix();
  609. rotateMatrix.Rotate(ArrowAngle / 2);
  610. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  611. arrowSegment.Points.Add((Point)LineEnd);
  612. rotateMatrix.Rotate(-ArrowAngle);
  613. arrowSegment.Points.Add((Point)LineEnd + (lineVector * rotateMatrix));
  614. arrowFigure.Segments.Add(arrowSegment);
  615. arrowFigure.IsClosed = IsEndClosed;
  616. arrowFigure.IsFilled = IsEndClosed;
  617. return arrowFigure;
  618. }
  619. private PathFigure CreateStartButtArrow()
  620. {
  621. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  622. {
  623. return null;
  624. }
  625. PathFigure arrowFigure = new PathFigure();
  626. LineSegment buttSegment = new LineSegment();
  627. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  628. lineVector.Normalize();
  629. lineVector *= ArrowLength;
  630. Matrix rotateMatrix = new Matrix();
  631. rotateMatrix.Rotate(90);
  632. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  633. rotateMatrix.Rotate(-180);
  634. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  635. arrowFigure.Segments.Add(buttSegment);
  636. return arrowFigure;
  637. }
  638. private PathFigure CreateEndButtArrow()
  639. {
  640. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  641. {
  642. return null;
  643. }
  644. PathFigure arrowFigure = new PathFigure();
  645. LineSegment buttSegment = new LineSegment();
  646. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  647. lineVector.Normalize();
  648. lineVector *= ArrowLength;
  649. Matrix rotateMatrix = new Matrix();
  650. rotateMatrix.Rotate(90);
  651. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  652. rotateMatrix.Rotate(-180);
  653. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  654. arrowFigure.Segments.Add(buttSegment);
  655. return arrowFigure;
  656. }
  657. private PathFigure CreateStartDiamondArrow()
  658. {
  659. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  660. {
  661. return null;
  662. }
  663. PathFigure arrowFigure = new PathFigure();
  664. PolyLineSegment arrowSegment = new PolyLineSegment();
  665. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  666. lineVector.Normalize();
  667. lineVector *= ArrowLength;
  668. Matrix rotateMatrix = new Matrix();
  669. rotateMatrix.Rotate(45);
  670. Point cornerTop = (Point)LineStart + (lineVector * rotateMatrix);
  671. Vector turnVector = cornerTop - (Point)LineStart;
  672. turnVector.Normalize();
  673. turnVector *= ArrowLength;
  674. Matrix turnMatrix = new Matrix();
  675. turnMatrix.Rotate(-90);
  676. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  677. rotateMatrix = new Matrix();
  678. rotateMatrix.Rotate(-45);
  679. Point cornerDown = (Point)LineStart + (lineVector * rotateMatrix);
  680. arrowFigure.StartPoint = (Point)LineStart;
  681. arrowSegment.Points.Add(cornerTop);
  682. arrowSegment.Points.Add(awayPoint);
  683. arrowSegment.Points.Add(cornerDown);
  684. arrowSegment.Points.Add((Point)LineStart);
  685. arrowFigure.Segments.Add(arrowSegment);
  686. arrowFigure.IsClosed = IsStartClosed;
  687. arrowFigure.IsFilled = IsStartClosed;
  688. return arrowFigure;
  689. }
  690. private PathFigure CreateEndDiamondArrow()
  691. {
  692. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  693. {
  694. return null;
  695. }
  696. PathFigure arrowFigure = new PathFigure();
  697. PolyLineSegment arrowSegment = new PolyLineSegment();
  698. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  699. lineVector.Normalize();
  700. lineVector *= ArrowLength;
  701. Matrix rotateMatrix = new Matrix();
  702. rotateMatrix.Rotate(45);
  703. Point cornerTop = (Point)LineEnd + (lineVector * rotateMatrix);
  704. Vector turnVector = cornerTop - (Point)LineEnd;
  705. turnVector.Normalize();
  706. turnVector *= ArrowLength;
  707. Matrix turnMatrix = new Matrix();
  708. turnMatrix.Rotate(-90);
  709. Point awayPoint = cornerTop + (turnVector * turnMatrix);
  710. rotateMatrix = new Matrix();
  711. rotateMatrix.Rotate(-45);
  712. Point cornerDown = (Point)LineEnd + (lineVector * rotateMatrix);
  713. arrowFigure.StartPoint = (Point)LineEnd;
  714. arrowSegment.Points.Add(cornerTop);
  715. arrowSegment.Points.Add(awayPoint);
  716. arrowSegment.Points.Add(cornerDown);
  717. arrowSegment.Points.Add((Point)LineEnd);
  718. arrowFigure.Segments.Add(arrowSegment);
  719. arrowFigure.IsClosed = IsEndClosed;
  720. arrowFigure.IsFilled = IsEndClosed;
  721. return arrowFigure;
  722. }
  723. private PathFigure CreateStartRoundArrow()
  724. {
  725. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  726. {
  727. return null;
  728. }
  729. PathFigure arrowFigure = new PathFigure();
  730. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  731. lineVector.Normalize();
  732. lineVector *= ArrowLength;
  733. Matrix rotateMatrix = new Matrix();
  734. rotateMatrix.Rotate(180);
  735. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  736. ArcSegment circleSegment = new ArcSegment();
  737. circleSegment.Point = (Point)LineStart;
  738. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  739. arrowFigure.Segments.Add(circleSegment);
  740. circleSegment = new ArcSegment();
  741. circleSegment.Point = (Point)arrowFigure.StartPoint;
  742. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  743. arrowFigure.Segments.Add(circleSegment);
  744. return arrowFigure;
  745. }
  746. private PathFigure CreateEndRoundArrow()
  747. {
  748. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  749. {
  750. return null;
  751. }
  752. PathFigure arrowFigure = new PathFigure();
  753. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  754. lineVector.Normalize();
  755. lineVector *= ArrowLength;
  756. Matrix rotateMatrix = new Matrix();
  757. rotateMatrix.Rotate(180);
  758. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  759. ArcSegment circleSegment = new ArcSegment();
  760. circleSegment.Point = (Point)LineEnd;
  761. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  762. arrowFigure.Segments.Add(circleSegment);
  763. circleSegment = new ArcSegment();
  764. circleSegment.Point = (Point)arrowFigure.StartPoint;
  765. circleSegment.Size = new Size(ArrowLength / 2, ArrowLength / 2);
  766. arrowFigure.Segments.Add(circleSegment);
  767. return arrowFigure;
  768. }
  769. private PathFigure CreateStartSquareArrow()
  770. {
  771. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  772. {
  773. return null;
  774. }
  775. PathFigure arrowFigure = new PathFigure();
  776. PolyLineSegment squreSegment = new PolyLineSegment();
  777. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  778. lineVector.Normalize();
  779. lineVector *= (ArrowLength / 2);
  780. Matrix rotateMatrix = new Matrix();
  781. rotateMatrix.Rotate(90);
  782. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  783. rotateMatrix.Rotate(-180);
  784. Point pointCorner = (Point)LineStart + (lineVector * rotateMatrix);
  785. squreSegment.Points.Add(pointCorner);
  786. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  787. moveVector.Normalize();
  788. moveVector *= (ArrowLength);
  789. rotateMatrix = new Matrix();
  790. rotateMatrix.Rotate(90);
  791. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  792. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  793. squreSegment.Points.Add(arrowFigure.StartPoint);
  794. squreSegment.Points.Add((Point)LineStart);
  795. arrowFigure.Segments.Add(squreSegment);
  796. return arrowFigure;
  797. }
  798. private PathFigure CreateEndSquareArrow()
  799. {
  800. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  801. {
  802. return null;
  803. }
  804. PathFigure arrowFigure = new PathFigure();
  805. PolyLineSegment squreSegment = new PolyLineSegment();
  806. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  807. lineVector.Normalize();
  808. lineVector *= (ArrowLength / 2);
  809. Matrix rotateMatrix = new Matrix();
  810. rotateMatrix.Rotate(90);
  811. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  812. rotateMatrix.Rotate(-180);
  813. Point pointCorner = (Point)LineEnd + (lineVector * rotateMatrix);
  814. squreSegment.Points.Add(pointCorner);
  815. Vector moveVector = arrowFigure.StartPoint - pointCorner;
  816. moveVector.Normalize();
  817. moveVector *= (ArrowLength);
  818. rotateMatrix = new Matrix();
  819. rotateMatrix.Rotate(90);
  820. squreSegment.Points.Add(pointCorner + (moveVector * rotateMatrix));
  821. squreSegment.Points.Add(arrowFigure.StartPoint + (moveVector * rotateMatrix));
  822. squreSegment.Points.Add(arrowFigure.StartPoint);
  823. squreSegment.Points.Add((Point)LineEnd);
  824. arrowFigure.Segments.Add(squreSegment);
  825. return arrowFigure;
  826. }
  827. private PathFigure CreateStartSlashArrow()
  828. {
  829. if (ArrowLength == 0 || !HasStartArrow || LineStart == null || LineEnd == null)
  830. {
  831. return null;
  832. }
  833. PathFigure arrowFigure = new PathFigure();
  834. LineSegment buttSegment = new LineSegment();
  835. Vector lineVector = (Point)LineStart - (Point)LineEnd;
  836. lineVector.Normalize();
  837. lineVector *= ArrowLength;
  838. Matrix rotateMatrix = new Matrix();
  839. rotateMatrix.Rotate(45);
  840. arrowFigure.StartPoint = (Point)LineStart + (lineVector * rotateMatrix);
  841. rotateMatrix.Rotate(-180);
  842. buttSegment.Point = ((Point)LineStart + (lineVector * rotateMatrix));
  843. arrowFigure.Segments.Add(buttSegment);
  844. return arrowFigure;
  845. }
  846. private PathFigure CreateEndSlashArrow()
  847. {
  848. if (ArrowLength == 0 || !HasEndArrow || LineStart == null || LineEnd == null)
  849. {
  850. return null;
  851. }
  852. PathFigure arrowFigure = new PathFigure();
  853. LineSegment buttSegment = new LineSegment();
  854. Vector lineVector = (Point)LineEnd - (Point)LineStart;
  855. lineVector.Normalize();
  856. lineVector *= ArrowLength;
  857. Matrix rotateMatrix = new Matrix();
  858. rotateMatrix.Rotate(45);
  859. arrowFigure.StartPoint = (Point)LineEnd + (lineVector * rotateMatrix);
  860. rotateMatrix.Rotate(-180);
  861. buttSegment.Point = ((Point)LineEnd + (lineVector * rotateMatrix));
  862. arrowFigure.Segments.Add(buttSegment);
  863. return arrowFigure;
  864. }
  865. }
  866. }
  867. public class PanelState
  868. {
  869. private static PanelState instance;
  870. public enum RightPanelState
  871. {
  872. None,
  873. PropertyPanel,
  874. ViewSettings
  875. }
  876. private bool _isLeftPanelExpand;
  877. public bool IsLeftPanelExpand
  878. {
  879. get { return _isLeftPanelExpand; }
  880. set
  881. {
  882. if (_isLeftPanelExpand != value)
  883. {
  884. _isLeftPanelExpand = value;
  885. OnPropertyChanged();
  886. }
  887. }
  888. }
  889. private RightPanelState _rightPanel;
  890. public RightPanelState RightPanel
  891. {
  892. get { return _rightPanel; }
  893. set
  894. {
  895. if (_rightPanel != value)
  896. {
  897. _rightPanel = value;
  898. OnPropertyChanged();
  899. }
  900. }
  901. }
  902. private PanelState() { }
  903. public static PanelState GetInstance()
  904. {
  905. if (instance == null)
  906. {
  907. instance = new PanelState();
  908. }
  909. return instance;
  910. }
  911. public event PropertyChangedEventHandler PropertyChanged;
  912. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  913. {
  914. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  915. }
  916. }
  917. public class SaveHelper
  918. {
  919. private static SaveHelper instance;
  920. private bool _canSave;
  921. public bool CanSave
  922. {
  923. get { return _canSave; }
  924. set
  925. {
  926. if (_canSave != value)
  927. {
  928. _canSave = value;
  929. OnPropertyChanged();
  930. }
  931. }
  932. }
  933. public event PropertyChangedEventHandler PropertyChanged;
  934. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  935. {
  936. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  937. }
  938. }
  939. public class CommandHelper
  940. {
  941. public static void CopyImage_Click(Dictionary<int, List<Bitmap>> imageDict)
  942. {
  943. try
  944. {
  945. if (imageDict != null && imageDict.Count > 0)
  946. {
  947. foreach (int pageIndex in imageDict.Keys)
  948. {
  949. List<Bitmap> imageList = imageDict[pageIndex];
  950. foreach (Bitmap image in imageList)
  951. {
  952. MemoryStream ms = new MemoryStream();
  953. image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  954. BitmapImage imageData = new BitmapImage();
  955. imageData.BeginInit();
  956. imageData.StreamSource = ms;
  957. imageData.CacheOption = BitmapCacheOption.OnLoad;
  958. imageData.EndInit();
  959. imageData.Freeze();
  960. Clipboard.SetImage(imageData);
  961. break;
  962. }
  963. }
  964. }
  965. }
  966. catch (Exception ex)
  967. {
  968. }
  969. }
  970. public static void ExtraImage_Click(Dictionary<int, List<Bitmap>> imageDict)
  971. {
  972. System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
  973. if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  974. {
  975. string choosePath = folderDialog.SelectedPath;
  976. string openPath = choosePath;
  977. try
  978. {
  979. if (imageDict != null && imageDict.Count > 0)
  980. {
  981. foreach (int pageIndex in imageDict.Keys)
  982. {
  983. List<Bitmap> imageList = imageDict[pageIndex];
  984. foreach (Bitmap image in imageList)
  985. {
  986. string savePath = Path.Combine(choosePath, Guid.NewGuid() + ".jpg");
  987. image.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  988. openPath = savePath;
  989. }
  990. }
  991. }
  992. Process.Start("explorer", "/select,\"" + openPath + "\"");
  993. }
  994. catch (Exception ex)
  995. {
  996. }
  997. }
  998. }
  999. public static double CheckZoomLevel(double[] zoomLevelList, double zoom, bool IsGrowth)
  1000. {
  1001. double standardZoom = 100;
  1002. if (zoom <= 0.01)
  1003. {
  1004. return 0.01;
  1005. }
  1006. if (zoom >= 10)
  1007. {
  1008. return 10;
  1009. }
  1010. zoom *= 100;
  1011. for (int i = 0; i < zoomLevelList.Length - 1; i++)
  1012. {
  1013. if (zoom > zoomLevelList[i] && zoom <= zoomLevelList[i + 1] && IsGrowth)
  1014. {
  1015. standardZoom = zoomLevelList[i + 1];
  1016. break;
  1017. }
  1018. if (zoom >= zoomLevelList[i] && zoom < zoomLevelList[i + 1] && !IsGrowth)
  1019. {
  1020. standardZoom = zoomLevelList[i];
  1021. break;
  1022. }
  1023. }
  1024. return standardZoom / 100;
  1025. }
  1026. }
  1027. public class SignatureHelper
  1028. {
  1029. public static List<CPDFSignature> SignatureList;
  1030. public static void InitEffectiveSignatureList(CPDFDocument document)
  1031. {
  1032. SignatureList = document.GetSignatureList();
  1033. for (int index = SignatureList.Count - 1; index >= 0; index--)
  1034. {
  1035. if (SignatureList[index].SignerList.Count <= 0)
  1036. {
  1037. SignatureList.RemoveAt(index);
  1038. }
  1039. }
  1040. }
  1041. public static void VerifySignatureList(CPDFDocument document)
  1042. {
  1043. foreach (var sig in SignatureList)
  1044. {
  1045. sig.VerifySignatureWithDocument(document);
  1046. }
  1047. }
  1048. }
  1049. }