CommonHelper.cs 39 KB

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