AnnotTransfer.cs 7.9 KB

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