AnnotTransfer.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKitViewer;
  3. using ComPDFKitViewer.AnnotEvent;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Media;
  10. namespace PDF_Master.ViewModels.Tools.AnnotManager
  11. {
  12. /// <summary>
  13. /// 注释面板与外部(如注释工具)关联
  14. /// </summary>
  15. public class AnnotTransfer
  16. {
  17. public event EventHandler<Dictionary<AnnotArgsType, object>> DataChanged;
  18. public event EventHandler<Dictionary<AnnotArgsType, object>> AnnotTypeChanged;
  19. public bool IsAddLink = false;
  20. public bool IsLocationLink = false;
  21. public bool IsSelectedTextAddLink = false;
  22. public bool IsSelectedTextAddShape = false;
  23. public AnnotAttribEvent AnnotEvent { get; set; }
  24. public List<AnnotAttribEvent> AnnotEvents = new List<AnnotAttribEvent>();
  25. public AnnotHandlerEventArgs annot;
  26. public List<AnnotHandlerEventArgs> annotlists;
  27. public Dictionary<AnnotArgsType, AnnotHandlerEventArgs> LastAnnotDict = new Dictionary<AnnotArgsType, AnnotHandlerEventArgs>();
  28. public AnnotHandlerEventArgs LastArrowAnnot = null;
  29. public string SharpsAnnot = "Rect";
  30. public string AnnotSelect = "";
  31. //是否为填写与签名的日期文本
  32. public bool IsTextFill { get; private set; }
  33. public void SetIsTextFill(bool isTextFill)
  34. {
  35. IsTextFill = isTextFill;
  36. }
  37. private bool isSharpAnnotSelected = true;
  38. public bool IsSharpAnnotSelected
  39. {
  40. get { return isSharpAnnotSelected; }
  41. set { isSharpAnnotSelected = value; }
  42. }
  43. public bool IsMultiSelected
  44. { get { return (annotlists != null && annotlists.Count > 1); } }
  45. public AnnotTransfer()
  46. {
  47. LastAnnotDict.Add(AnnotArgsType.AnnotHighlight, null);
  48. LastAnnotDict.Add(AnnotArgsType.AnnotUnderline, null);
  49. LastAnnotDict.Add(AnnotArgsType.AnnotStrikeout, null);
  50. LastAnnotDict.Add(AnnotArgsType.AnnotFreehand, null);
  51. LastAnnotDict.Add(AnnotArgsType.AnnotFreeText, null);
  52. LastAnnotDict.Add(AnnotArgsType.AnnotSticky, null);
  53. LastAnnotDict.Add(AnnotArgsType.AnnotSquare, null);
  54. LastAnnotDict.Add(AnnotArgsType.AnnotCircle, null);
  55. LastAnnotDict.Add(AnnotArgsType.AnnotLine, null);
  56. }
  57. public void SaveLastAnnot()
  58. {
  59. if (annot == null)
  60. {
  61. return;
  62. }
  63. if (annot.EventType == AnnotArgsType.AnnotLine)
  64. {
  65. var lineAnnotArgs = annot as LineAnnotArgs;
  66. if (lineAnnotArgs.HeadLineType >= (C_LINE_TYPE)1 || lineAnnotArgs.TailLineType >= (C_LINE_TYPE)1)
  67. {
  68. LastArrowAnnot = annot;
  69. return;
  70. }
  71. }
  72. if (LastAnnotDict.ContainsKey(annot.EventType))
  73. {
  74. LastAnnotDict[annot.EventType] = annot;
  75. }
  76. }
  77. #region 静态
  78. //是否为形状注释
  79. public static bool IsShapAnnot(AnnotHandlerEventArgs annot)
  80. {
  81. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  82. annot.EventType == AnnotArgsType.AnnotSquare ||
  83. annot.EventType == AnnotArgsType.AnnotLine
  84. )
  85. {
  86. return true;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93. //是否为高亮注释
  94. public static bool IsHightAnnot(AnnotHandlerEventArgs annot)
  95. {
  96. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  97. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  98. annot.EventType == AnnotArgsType.AnnotHighlight ||
  99. annot.EventType == AnnotArgsType.AnnotStrikeout
  100. )
  101. {
  102. return true;
  103. }
  104. else
  105. {
  106. return false;
  107. }
  108. }
  109. //判断注释列表是否有不同种类的注释
  110. public static bool IsDifferentTypeAnnots(List<AnnotHandlerEventArgs> annotlists)
  111. {
  112. //如高亮、下划线、删除线,是属于同一种类的注释
  113. bool isDifferentAnnotTyle = false;
  114. var annot = annotlists[0];
  115. var lastAnnot = annot;
  116. foreach (var item in annotlists)
  117. {
  118. if (lastAnnot.EventType != item.EventType)
  119. {
  120. if ((AnnotTransfer.IsShapAnnot(annot) == true && AnnotTransfer.IsShapAnnot(item) == true) || (AnnotTransfer.IsHightAnnot(annot) == true && AnnotTransfer.IsHightAnnot(item) == true))
  121. {
  122. lastAnnot = item;
  123. continue;
  124. }
  125. lastAnnot = item;
  126. isDifferentAnnotTyle = true;
  127. break;
  128. }
  129. }
  130. return isDifferentAnnotTyle;
  131. }
  132. //外部UI控件选中状态
  133. public static bool IsSolidStyle(DashStyle LineDash)
  134. {
  135. bool isSolid = true;
  136. if (LineDash == null || LineDash.Dashes.Count == 0)
  137. {
  138. return isSolid;
  139. }
  140. foreach (var item in LineDash.Dashes)
  141. {
  142. if (item > 0)
  143. {
  144. isSolid = false;
  145. break;
  146. }
  147. }
  148. return isSolid;
  149. }
  150. public static DashStyle GetLineDashStyle(bool isSolid)
  151. {
  152. DashStyle newDash = new DashStyle();
  153. if (isSolid == false)
  154. {
  155. newDash.Dashes.Add(2);
  156. newDash.Dashes.Add(2);
  157. }
  158. else
  159. {
  160. newDash = DashStyles.Solid;
  161. }
  162. return newDash;
  163. }
  164. #endregion 静态
  165. //单个属性更改
  166. public void UpdateAnnotAAttrib(AnnotAttrib annotAttrib, object obj)
  167. {
  168. //if(obj is Color)
  169. //{
  170. // if (AnnotEvents != null)
  171. // {
  172. // foreach (var itemevent in AnnotEvents)
  173. // {
  174. // itemevent?.UpdateAttrib(annotAttrib, obj);
  175. // itemevent?.UpdateAnnot();
  176. // }
  177. // }
  178. //}
  179. if (annotlists == null) return;
  180. if (annotlists != null && annotlists.Count > 1)
  181. {
  182. foreach (var itemevent in AnnotEvents)
  183. {
  184. itemevent?.UpdateAttrib(annotAttrib, obj);
  185. itemevent?.UpdateAnnot();
  186. }
  187. }
  188. else if (annotlists.Count == 1)
  189. {
  190. AnnotEvent?.UpdateAttrib(annotAttrib, obj);
  191. AnnotEvent?.UpdateAnnot();
  192. //this.annot = AnnotEvent.AnnotItemsList[0];
  193. }
  194. }
  195. //多个属性更改
  196. public void UpdateAnnotAllAttribs(Dictionary<AnnotAttrib, object> AnnotAttribDir)
  197. {
  198. if (annotlists == null) return;
  199. if (annotlists != null && annotlists.Count > 1)
  200. {
  201. foreach (var itemevent in AnnotEvents)
  202. {
  203. foreach (var item in AnnotAttribDir)
  204. {
  205. itemevent?.UpdateAttrib(item.Key, item.Value);
  206. }
  207. itemevent?.UpdateAnnot();
  208. }
  209. }
  210. else if (annotlists.Count == 1)
  211. {
  212. foreach (var item in AnnotAttribDir)
  213. {
  214. AnnotEvent?.UpdateAttrib(item.Key, item.Value);
  215. }
  216. AnnotEvent?.UpdateAnnot();
  217. //this.annot = AnnotEvent.AnnotItemsList[0];
  218. }
  219. }
  220. //是否为多选
  221. #region Invoke
  222. /// <summary>
  223. /// 更新多个属性,触发到工具栏注释工具,改变工具图标下的颜色值
  224. /// </summary>
  225. public void InvokeToMyTools(object sender, Dictionary<AnnotArgsType, object> keyValues)
  226. {
  227. DataChanged?.Invoke(sender, keyValues);
  228. }
  229. /// <summary>
  230. /// 更新单个属性
  231. /// </summary>
  232. public void InvokeToMyTools(AnnotArgsType argsType, object obj)
  233. {
  234. Dictionary<AnnotArgsType, object> changeData = new Dictionary<AnnotArgsType, object>();
  235. changeData[argsType] = obj;
  236. DataChanged?.Invoke(null, changeData);
  237. }
  238. //同一属性面板,切换注释工具
  239. public void AnnotTypeChangedInvoke(object sender, Dictionary<AnnotArgsType, object> keyValues)
  240. {
  241. AnnotTypeChanged?.Invoke(sender, keyValues);
  242. }
  243. #endregion Invoke
  244. }
  245. }