AnnotTransfer.cs 8.1 KB

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