AnnotTransfer.cs 8.1 KB

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