SquareAnnotHistory.cs 7.2 KB

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