CommonHelper.cs 7.2 KB

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