CommonHelper.cs 39 KB

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