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