AnnotTransfer.cs 8.5 KB

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