CommonHelper.cs 37 KB

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