CommonHelper.cs 43 KB

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