PolygonMeasureAnnotHistory.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using ComPDFKit.Measure;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using ComPDFKitViewer.Annot;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using static ComPDFKit.PDFAnnotation.CTextAttribute;
  12. namespace ComPDFKit.Tool.UndoManger
  13. {
  14. internal class PolygonMeasureAnnotHistory : AnnotHistory
  15. {
  16. public override int GetAnnotIndex()
  17. {
  18. if (CurrentParam != null)
  19. {
  20. return CurrentParam.AnnotIndex;
  21. }
  22. return base.GetAnnotIndex();
  23. }
  24. public override int GetPageIndex()
  25. {
  26. if (CurrentParam != null)
  27. {
  28. return CurrentParam.PageIndex;
  29. }
  30. return base.GetPageIndex();
  31. }
  32. public override void SetAnnotIndex(int newIndex)
  33. {
  34. if (CurrentParam != null)
  35. {
  36. CurrentParam.AnnotIndex = newIndex;
  37. }
  38. if (PreviousParam != null)
  39. {
  40. PreviousParam.AnnotIndex = newIndex;
  41. }
  42. }
  43. internal override bool Add()
  44. {
  45. PolygonMeasureParam currentParam = CurrentParam as PolygonMeasureParam;
  46. if (CurrentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  47. {
  48. return false;
  49. }
  50. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  51. CPDFPolygonAnnotation polygonAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON) as CPDFPolygonAnnotation;
  52. if (polygonAnnot != null)
  53. {
  54. int annotIndex = pdfPage.GetAnnotCount() - 1;
  55. if (currentParam.HasFillColor)
  56. {
  57. if (currentParam.FillColor != null && currentParam.FillColor.Length == 3)
  58. {
  59. polygonAnnot.SetBgColor(currentParam.FillColor);
  60. }
  61. }
  62. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  63. {
  64. polygonAnnot.SetLineColor(currentParam.LineColor);
  65. }
  66. polygonAnnot.SetTransparency((byte)currentParam.Transparency);
  67. polygonAnnot.SetLineWidth(currentParam.LineWidth);
  68. polygonAnnot.SetPoints(currentParam.SavePoints);
  69. polygonAnnot.SetRect(currentParam.ClientRect);
  70. if (currentParam.LineDash != null)
  71. {
  72. if (currentParam.LineDash.Length == 0)
  73. {
  74. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  75. }
  76. else
  77. {
  78. List<float> floatArray = new List<float>();
  79. foreach (float num in currentParam.LineDash)
  80. {
  81. floatArray.Add(num);
  82. }
  83. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  84. }
  85. }
  86. CTextAttribute textAttribute = new CTextAttribute();
  87. textAttribute.FontColor = currentParam.FontColor;
  88. textAttribute.FontSize = (float)currentParam.FontSize;
  89. textAttribute.FontName = CFontNameHelper.ObtainFontName(CFontNameHelper.GetFontType(currentParam.FontName),
  90. currentParam.IsBold,
  91. currentParam.IsItalic);
  92. polygonAnnot.SetTextAttribute(textAttribute);
  93. if (currentParam.measureInfo != null)
  94. {
  95. CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
  96. if (polygonMeasure != null)
  97. {
  98. polygonMeasure.SetMeasureInfo(currentParam.measureInfo);
  99. polygonMeasure.SetMeasureScale(currentParam.measureInfo.RulerBase, currentParam.measureInfo.RulerBaseUnit,
  100. currentParam.measureInfo.RulerTranslate, currentParam.measureInfo.RulerTranslateUnit);
  101. polygonMeasure.UpdateAnnotMeasure();
  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() || !polygonAnnot.IsMeasured())
  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.ClientRect.Equals(checkParam.ClientRect))
  193. {
  194. polygonAnnot.SetRect(updateParam.ClientRect);
  195. }
  196. if (updateParam.Author != checkParam.Author)
  197. {
  198. polygonAnnot.SetAuthor(updateParam.Author);
  199. }
  200. if (updateParam.Content != checkParam.Content)
  201. {
  202. polygonAnnot.SetContent(updateParam.Content);
  203. }
  204. if (updateParam.Locked != checkParam.Locked)
  205. {
  206. polygonAnnot.SetIsLocked(updateParam.Locked);
  207. }
  208. if (updateParam.measureInfo != checkParam.measureInfo)
  209. {
  210. CPDFAreaMeasure polygonMeasure = polygonAnnot.GetAreaMeasure();
  211. if (polygonMeasure != null)
  212. {
  213. polygonMeasure.SetMeasureInfo(updateParam.measureInfo);
  214. polygonMeasure.SetMeasureScale(updateParam.measureInfo.RulerBase, updateParam.measureInfo.RulerBaseUnit,
  215. updateParam.measureInfo.RulerTranslate, updateParam.measureInfo.RulerTranslateUnit);
  216. polygonMeasure.UpdateAnnotMeasure();
  217. }
  218. }
  219. polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  220. polygonAnnot.UpdateAp();
  221. return true;
  222. }
  223. return false;
  224. }
  225. internal override bool Remove()
  226. {
  227. if (MakeAnnotValid(CurrentParam))
  228. {
  229. Annot.RemoveAnnot();
  230. return true;
  231. }
  232. return false;
  233. }
  234. }
  235. }