AnnotTransfer.cs 9.1 KB

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