CircleAnnotHistory.cs 7.2 KB

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