CommonHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Media;
  7. using System.Windows;
  8. using ComPDFKitViewer;
  9. using System.IO;
  10. using System.Runtime.Remoting.Messaging;
  11. using System.Windows.Media.Imaging;
  12. using System.ComponentModel;
  13. using ComPDFKit.PDFAnnotation;
  14. namespace ComPDFKit.Tool.Help
  15. {
  16. public static class CommonHelper
  17. {
  18. /// <summary>
  19. /// Find the parent control of the target type of the object
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="obj"></param>
  23. /// <returns></returns>
  24. public static T FindVisualParent<T>(DependencyObject obj) where T : class
  25. {
  26. try
  27. {
  28. while (obj != null)
  29. {
  30. if (obj is T)
  31. return obj as T;
  32. obj = VisualTreeHelper.GetParent(obj);
  33. }
  34. return null;
  35. }
  36. catch { return null; }
  37. }
  38. /// <summary>
  39. /// Find the child control of the target type of the object
  40. /// </summary>
  41. /// <typeparam name="childItem">
  42. /// The type of the child control to find
  43. /// </typeparam>
  44. /// <param name="obj">
  45. /// The object to find
  46. /// </param>
  47. /// <returns>
  48. /// The child control of the target type of the object
  49. /// </returns>
  50. public static childItem FindVisualChild<childItem>(DependencyObject obj)
  51. where childItem : DependencyObject
  52. {
  53. try
  54. {
  55. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  56. {
  57. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  58. if (child != null && child is childItem)
  59. return (childItem)child;
  60. else
  61. {
  62. childItem childOfChild = FindVisualChild<childItem>(child);
  63. if (childOfChild != null)
  64. return childOfChild;
  65. }
  66. }
  67. return null;
  68. }
  69. catch { return null; }
  70. }
  71. /// <summary>
  72. /// Find the child control of the target type of the object
  73. /// </summary>
  74. /// <typeparam name="childItem">
  75. /// The type of the child control to find
  76. /// </typeparam>
  77. /// <param name="obj">
  78. /// The object to find
  79. /// </param>
  80. /// <returns>
  81. /// The child control of the target type of the object
  82. /// </returns>
  83. public static List<childItem> FindVisualChildList<childItem>(DependencyObject obj)
  84. where childItem : DependencyObject
  85. {
  86. List <childItem> children = new List <childItem>();
  87. try
  88. {
  89. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  90. {
  91. DependencyObject child = VisualTreeHelper.GetChild(obj, i);
  92. if (child != null && child is childItem)
  93. {
  94. children.Add((childItem)child);
  95. }
  96. else
  97. {
  98. childItem childOfChild = FindVisualChild<childItem>(child);
  99. if (childOfChild != null)
  100. children.Add(childOfChild);
  101. }
  102. }
  103. return children;
  104. }
  105. catch { return children; }
  106. }
  107. public static PathGeometry GetPathIcon(string iconKey)
  108. {
  109. string pathIcon = "M18 3H2V15H5V18L10 15H18V3ZM5 6H11V7.5H5V6ZM5 9.5H15V11H5V9.5Z";
  110. try
  111. {
  112. TypeConverter typeCovert = TypeDescriptor.GetConverter(typeof(Geometry));
  113. if (CPDFViewer.StickyIconDict != null && CPDFViewer.StickyIconDict.ContainsKey(iconKey))
  114. {
  115. pathIcon = CPDFViewer.StickyIconDict[iconKey];
  116. }
  117. return PathGeometry.CreateFromGeometry((Geometry)typeCovert.ConvertFrom(pathIcon));
  118. }
  119. catch (Exception ex)
  120. {
  121. }
  122. return new PathGeometry();
  123. }
  124. private static bool GetIconData(string iconName, Brush fillBrush, out string tempImagePath)
  125. {
  126. tempImagePath = string.Empty;
  127. try
  128. {
  129. if (CPDFViewer.StickyIconDict != null && CPDFViewer.StickyIconDict.ContainsKey(iconName))
  130. {
  131. PathGeometry iconGeometry = GetPathIcon(iconName);
  132. DrawingVisual iconVisual = new DrawingVisual();
  133. DrawingContext iconContext = iconVisual.RenderOpen();
  134. iconContext.DrawGeometry(fillBrush, null, iconGeometry);
  135. iconContext.Close();
  136. RenderTargetBitmap renderBitmap = new RenderTargetBitmap(32, 32, 96, 96, PixelFormats.Pbgra32);
  137. renderBitmap.Render(iconVisual);
  138. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  139. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  140. using (FileStream fs = File.Create(tempPath))
  141. {
  142. pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  143. pngEncoder.Save(fs);
  144. }
  145. tempImagePath = tempPath;
  146. return true;
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. }
  152. return false;
  153. }
  154. public static void UpdateStickyAP(CPDFTextAnnotation textAnnotation)
  155. {
  156. if(textAnnotation==null || textAnnotation.IsValid()==false)
  157. {
  158. return;
  159. }
  160. try
  161. {
  162. string iconName = textAnnotation.GetIconName();
  163. byte opacity = textAnnotation.GetTransparency();
  164. SolidColorBrush fillBrush = new SolidColorBrush(Color.FromArgb(opacity, textAnnotation.Color[0], textAnnotation.Color[1], textAnnotation.Color[2]));
  165. if (GetIconData(iconName, fillBrush, out string apPath) && File.Exists(apPath))
  166. {
  167. textAnnotation.UpdateApWithImage(apPath, string.Empty, textAnnotation.GetRotation());
  168. File.Delete(apPath);
  169. }
  170. }
  171. catch (Exception ex)
  172. {
  173. }
  174. }
  175. }
  176. }