AnnotTransfer.cs 9.0 KB

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