CommonHelper.cs 37 KB

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