CommonHelper.cs 37 KB

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