CommonHelper.cs 44 KB

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