CommonHelper.cs 41 KB

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