PolygonMeasureAnnotHistory.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using static ComPDFKit.PDFAnnotation.CTextAttribute;
  8. namespace ComPDFKit.Tool.UndoManger
  9. {
  10. public class PolygonMeasureAnnotHistory : AnnotHistory
  11. {
  12. public override int GetAnnotIndex()
  13. {
  14. if (CurrentParam != null)
  15. {
  16. return CurrentParam.AnnotIndex;
  17. }
  18. return base.GetAnnotIndex();
  19. }
  20. public override int GetPageIndex()
  21. {
  22. if (CurrentParam != null)
  23. {
  24. return CurrentParam.PageIndex;
  25. }
  26. return base.GetPageIndex();
  27. }
  28. public override void SetAnnotIndex(int newIndex)
  29. {
  30. if (CurrentParam != null)
  31. {
  32. CurrentParam.AnnotIndex = newIndex;
  33. }
  34. if (PreviousParam != null)
  35. {
  36. PreviousParam.AnnotIndex = newIndex;
  37. }
  38. }
  39. internal override bool Add()
  40. {
  41. PolygonMeasureParam currentParam = CurrentParam as PolygonMeasureParam;
  42. if (CurrentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  43. {
  44. return false;
  45. }
  46. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  47. CPDFPolygonAnnotation polygonAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON) as CPDFPolygonAnnotation;
  48. if (polygonAnnot != null)
  49. {
  50. int annotIndex = pdfPage.GetAnnotCount() - 1;
  51. if (currentParam.HasFillColor)
  52. {
  53. if (currentParam.FillColor != null && currentParam.FillColor.Length == 3)
  54. {
  55. polygonAnnot.SetBgColor(currentParam.FillColor);
  56. }
  57. }
  58. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  59. {
  60. polygonAnnot.SetLineColor(currentParam.LineColor);
  61. }
  62. polygonAnnot.SetTransparency(currentParam.Transparency);
  63. polygonAnnot.SetLineWidth(currentParam.LineWidth);
  64. polygonAnnot.SetPoints(currentParam.SavePoints);
  65. polygonAnnot.SetAnnotBorderEffector(currentParam.BorderEffector);
  66. polygonAnnot.SetRect(currentParam.ClientRect);
  67. if (currentParam.LineDash != null)
  68. {
  69. if (currentParam.LineDash.Length == 0)
  70. {
  71. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  72. }
  73. else
  74. {
  75. List<float> floatArray = new List<float>();
  76. foreach (float num in currentParam.LineDash)
  77. {
  78. floatArray.Add(num);
  79. }
  80. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  81. }
  82. }
  83. if(polygonAnnot.IsMeasured())
  84. {
  85. CTextAttribute textAttribute = new CTextAttribute();
  86. textAttribute.FontColor = currentParam.FontColor;
  87. textAttribute.FontSize = (float)currentParam.FontSize;
  88. textAttribute.FontName = CFontNameHelper.ObtainFontName(CFontNameHelper.GetFontType(currentParam.FontName),
  89. currentParam.IsBold,
  90. currentParam.IsItalic);
  91. polygonAnnot.SetTextAttribute(textAttribute);
  92. if (currentParam.measureInfo != null)
  93. {
  94. CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
  95. if (polygonMeasure != null)
  96. {
  97. polygonMeasure.SetMeasureInfo(currentParam.measureInfo);
  98. polygonMeasure.SetMeasureScale(currentParam.measureInfo.RulerBase, currentParam.measureInfo.RulerBaseUnit,
  99. currentParam.measureInfo.RulerTranslate, currentParam.measureInfo.RulerTranslateUnit);
  100. polygonMeasure.UpdateAnnotMeasure();
  101. }
  102. }
  103. }
  104. if (!string.IsNullOrEmpty(currentParam.Author))
  105. {
  106. polygonAnnot.SetAuthor(currentParam.Author);
  107. }
  108. if (!string.IsNullOrEmpty(currentParam.Content))
  109. {
  110. polygonAnnot.SetContent(currentParam.Content);
  111. }
  112. polygonAnnot.SetIsLocked(currentParam.Locked);
  113. polygonAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  114. polygonAnnot.UpdateAp();
  115. polygonAnnot.ReleaseAnnot();
  116. if (currentParam != null)
  117. {
  118. currentParam.AnnotIndex = annotIndex;
  119. }
  120. if (PreviousParam != null)
  121. {
  122. PreviousParam.AnnotIndex = annotIndex;
  123. }
  124. return true;
  125. }
  126. return false;
  127. }
  128. internal override bool Update(bool isUndo)
  129. {
  130. if (CurrentParam as PolygonMeasureParam == null || PreviousParam as PolygonMeasureParam == null)
  131. {
  132. return false;
  133. }
  134. if (MakeAnnotValid(CurrentParam))
  135. {
  136. CPDFPolygonAnnotation polygonAnnot = Annot as CPDFPolygonAnnotation;
  137. if (polygonAnnot == null || !polygonAnnot.IsValid())
  138. {
  139. return false;
  140. }
  141. PolygonMeasureParam updateParam = (isUndo ? PreviousParam : CurrentParam) as PolygonMeasureParam;
  142. PolygonMeasureParam checkParam = (isUndo ? CurrentParam : PreviousParam) as PolygonMeasureParam;
  143. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  144. {
  145. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  146. {
  147. polygonAnnot.SetLineColor(updateParam.LineColor);
  148. }
  149. }
  150. if (!CheckArrayEqual(updateParam.FillColor, checkParam.FillColor))
  151. {
  152. if (updateParam.HasFillColor)
  153. {
  154. if (updateParam.FillColor != null && updateParam.FillColor.Length == 3)
  155. {
  156. polygonAnnot.SetBgColor(updateParam.FillColor);
  157. }
  158. }
  159. else
  160. {
  161. polygonAnnot.ClearBgColor();
  162. }
  163. }
  164. if (updateParam.Transparency != checkParam.Transparency)
  165. {
  166. polygonAnnot.SetTransparency((byte)updateParam.Transparency);
  167. }
  168. if (updateParam.LineWidth != checkParam.LineWidth)
  169. {
  170. polygonAnnot.SetLineWidth((byte)updateParam.LineWidth);
  171. }
  172. if (!CheckArrayEqual(updateParam.LineDash, checkParam.LineDash))
  173. {
  174. if (updateParam.LineDash.Length == 0)
  175. {
  176. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  177. }
  178. else
  179. {
  180. List<float> floatArray = new List<float>();
  181. foreach (float num in updateParam.LineDash)
  182. {
  183. floatArray.Add(num);
  184. }
  185. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  186. }
  187. }
  188. if (!updateParam.SavePoints.SequenceEqual(checkParam.SavePoints))
  189. {
  190. polygonAnnot.SetPoints(updateParam.SavePoints);
  191. }
  192. if(!updateParam.BorderEffector.Equals(checkParam.BorderEffector))
  193. {
  194. polygonAnnot.SetAnnotBorderEffector(updateParam.BorderEffector);
  195. }
  196. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  197. {
  198. polygonAnnot.SetRect(updateParam.ClientRect);
  199. }
  200. if (updateParam.Author != checkParam.Author)
  201. {
  202. polygonAnnot.SetAuthor(updateParam.Author);
  203. }
  204. if (updateParam.Content != checkParam.Content)
  205. {
  206. polygonAnnot.SetContent(updateParam.Content);
  207. }
  208. if (updateParam.Locked != checkParam.Locked)
  209. {
  210. polygonAnnot.SetIsLocked(updateParam.Locked);
  211. }
  212. if (polygonAnnot.IsMeasured())
  213. {
  214. if (updateParam.measureInfo != checkParam.measureInfo)
  215. {
  216. CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
  217. if (polygonMeasure != null)
  218. {
  219. polygonMeasure.SetMeasureInfo(updateParam.measureInfo);
  220. polygonMeasure.SetMeasureScale(updateParam.measureInfo.RulerBase, updateParam.measureInfo.RulerBaseUnit,
  221. updateParam.measureInfo.RulerTranslate, updateParam.measureInfo.RulerTranslateUnit);
  222. polygonMeasure.UpdateAnnotMeasure();
  223. }
  224. }
  225. }
  226. polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  227. polygonAnnot.UpdateAp();
  228. return true;
  229. }
  230. return false;
  231. }
  232. internal override bool Remove()
  233. {
  234. if (MakeAnnotValid(CurrentParam))
  235. {
  236. Annot.RemoveAnnot();
  237. return true;
  238. }
  239. return false;
  240. }
  241. }
  242. }