CommonHelper.cs 38 KB

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