CommonHelper.cs 39 KB

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