CommonHelper.cs 45 KB

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