PolygonAnnotHistory.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Tool.Help;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ComPDFKit.Tool.UndoManger
  10. {
  11. public class PolygonAnnotHistory : AnnotHistory
  12. {
  13. public override int GetAnnotIndex()
  14. {
  15. if (CurrentParam != null)
  16. {
  17. return CurrentParam.AnnotIndex;
  18. }
  19. return base.GetAnnotIndex();
  20. }
  21. public override int GetPageIndex()
  22. {
  23. if (CurrentParam != null)
  24. {
  25. return CurrentParam.PageIndex;
  26. }
  27. return base.GetPageIndex();
  28. }
  29. public override void SetAnnotIndex(int newIndex)
  30. {
  31. if (CurrentParam != null)
  32. {
  33. CurrentParam.AnnotIndex = newIndex;
  34. }
  35. if (PreviousParam != null)
  36. {
  37. PreviousParam.AnnotIndex = newIndex;
  38. }
  39. }
  40. internal override bool Add()
  41. {
  42. PolygonMeasureParam currentParam = CurrentParam as PolygonMeasureParam;
  43. if (CurrentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  44. {
  45. return false;
  46. }
  47. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  48. CPDFPolygonAnnotation polygonAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON) as CPDFPolygonAnnotation;
  49. if (polygonAnnot != null)
  50. {
  51. int annotIndex = pdfPage.GetAnnotCount() - 1;
  52. if (currentParam.HasFillColor)
  53. {
  54. if (currentParam.FillColor != null && currentParam.FillColor.Length == 3)
  55. {
  56. polygonAnnot.SetBgColor(currentParam.FillColor);
  57. }
  58. }
  59. if (currentParam.LineColor != null && currentParam.LineColor.Length == 3)
  60. {
  61. polygonAnnot.SetLineColor(currentParam.LineColor);
  62. }
  63. polygonAnnot.SetTransparency((byte)currentParam.Transparency);
  64. polygonAnnot.SetLineWidth(currentParam.LineWidth);
  65. polygonAnnot.SetPoints(currentParam.SavePoints);
  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 (!string.IsNullOrEmpty(currentParam.Author))
  84. {
  85. polygonAnnot.SetAuthor(currentParam.Author);
  86. }
  87. if (!string.IsNullOrEmpty(currentParam.Content))
  88. {
  89. polygonAnnot.SetContent(currentParam.Content);
  90. }
  91. polygonAnnot.SetIsLocked(currentParam.Locked);
  92. polygonAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  93. polygonAnnot.UpdateAp();
  94. polygonAnnot.ReleaseAnnot();
  95. if (currentParam != null)
  96. {
  97. currentParam.AnnotIndex = annotIndex;
  98. }
  99. if (PreviousParam != null)
  100. {
  101. PreviousParam.AnnotIndex = annotIndex;
  102. }
  103. return true;
  104. }
  105. return true;
  106. }
  107. internal override bool Update(bool isUndo)
  108. {
  109. if (CurrentParam as PolygonMeasureParam == null || PreviousParam as PolygonMeasureParam == null)
  110. {
  111. return false;
  112. }
  113. if (MakeAnnotValid(CurrentParam))
  114. {
  115. CPDFPolygonAnnotation polygonAnnot = Annot as CPDFPolygonAnnotation;
  116. if (polygonAnnot == null || !polygonAnnot.IsValid() || !polygonAnnot.IsMeasured())
  117. {
  118. return false;
  119. }
  120. PolygonMeasureParam updateParam = (isUndo ? PreviousParam : CurrentParam) as PolygonMeasureParam;
  121. PolygonMeasureParam checkParam = (isUndo ? CurrentParam : PreviousParam) as PolygonMeasureParam;
  122. if (!CheckArrayEqual(updateParam.LineColor, checkParam.LineColor))
  123. {
  124. if (updateParam.LineColor != null && updateParam.LineColor.Length == 3)
  125. {
  126. polygonAnnot.SetLineColor(updateParam.LineColor);
  127. }
  128. }
  129. if (!CheckArrayEqual(updateParam.FillColor, checkParam.FillColor))
  130. {
  131. if (updateParam.HasFillColor)
  132. {
  133. if (updateParam.FillColor != null && updateParam.FillColor.Length == 3)
  134. {
  135. polygonAnnot.SetBgColor(updateParam.FillColor);
  136. }
  137. }
  138. else
  139. {
  140. polygonAnnot.ClearBgColor();
  141. }
  142. if (updateParam.Transparency != checkParam.Transparency)
  143. {
  144. polygonAnnot.SetTransparency((byte)updateParam.Transparency);
  145. }
  146. if (updateParam.LineWidth != checkParam.LineWidth)
  147. {
  148. polygonAnnot.SetLineWidth((byte)updateParam.LineWidth);
  149. }
  150. if (!CheckArrayEqual(updateParam.LineDash, checkParam.LineDash))
  151. {
  152. if (updateParam.LineDash != null)
  153. {
  154. if (updateParam.LineDash.Length == 0)
  155. {
  156. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID, new float[0]);
  157. }
  158. else
  159. {
  160. List<float> floatArray = new List<float>();
  161. foreach (float num in updateParam.LineDash)
  162. {
  163. floatArray.Add(num);
  164. }
  165. polygonAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  166. }
  167. }
  168. }
  169. if (!updateParam.SavePoints.SequenceEqual(checkParam.SavePoints))
  170. {
  171. polygonAnnot.SetPoints(updateParam.SavePoints);
  172. }
  173. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  174. {
  175. polygonAnnot.SetRect(updateParam.ClientRect);
  176. }
  177. if (updateParam.Author != checkParam.Author)
  178. {
  179. polygonAnnot.SetAuthor(updateParam.Author);
  180. }
  181. if (updateParam.Content != checkParam.Content)
  182. {
  183. polygonAnnot.SetContent(updateParam.Content);
  184. }
  185. if (updateParam.Locked != checkParam.Locked)
  186. {
  187. polygonAnnot.SetIsLocked(updateParam.Locked);
  188. }
  189. }
  190. polygonAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  191. polygonAnnot.UpdateAp();
  192. return true;
  193. }
  194. return false;
  195. }
  196. internal override bool Remove()
  197. {
  198. if (MakeAnnotValid(CurrentParam))
  199. {
  200. Annot.RemoveAnnot();
  201. return true;
  202. }
  203. return false;
  204. }
  205. }
  206. }