LineAnnotHistory.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 LineAnnotHistory: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. LineParam currentParam = CurrentParam as LineParam;
  39. if (currentParam==null || PDFDoc==null || !PDFDoc.IsValid())
  40. {
  41. return false;
  42. }
  43. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  44. CPDFLineAnnotation lineAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE) as CPDFLineAnnotation;
  45. if (lineAnnot != null)
  46. {
  47. int annotIndex = pdfPage.GetAnnotCount() - 1;
  48. if(currentParam.HeadLineType!=C_LINE_TYPE.LINETYPE_NONE || currentParam.TailLineType!=C_LINE_TYPE.LINETYPE_NONE)
  49. {
  50. lineAnnot.SetLineType(currentParam.HeadLineType, currentParam.TailLineType);
  51. }
  52. if(currentParam.LineColor!=null && currentParam.LineColor.Length==3)
  53. {
  54. lineAnnot.SetLineColor(currentParam.LineColor);
  55. }
  56. if(currentParam.HasBgColor)
  57. {
  58. if (currentParam.BgColor != null && currentParam.BgColor.Length == 3)
  59. {
  60. lineAnnot.SetBgColor(currentParam.BgColor);
  61. }
  62. }
  63. lineAnnot.SetTransparency((byte)currentParam.Transparency);
  64. lineAnnot.SetLineWidth((byte)currentParam.LineWidth);
  65. lineAnnot.SetLinePoints(currentParam.HeadPoint, currentParam.TailPoint);
  66. lineAnnot.SetRect(currentParam.ClientRect);
  67. List<float> floatArray = new List<float>();
  68. if (currentParam.LineDash != null)
  69. {
  70. foreach (float num in currentParam.LineDash)
  71. {
  72. floatArray.Add(num);
  73. }
  74. }
  75. lineAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED, floatArray.ToArray());
  76. if (!string.IsNullOrEmpty(currentParam.Author))
  77. {
  78. lineAnnot.SetAuthor(currentParam.Author);
  79. }
  80. if(!string.IsNullOrEmpty(currentParam.Content))
  81. {
  82. lineAnnot.SetContent(currentParam.Content);
  83. }
  84. lineAnnot.SetIsLocked(currentParam.Locked);
  85. lineAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  86. lineAnnot.UpdateAp();
  87. lineAnnot.ReleaseAnnot();
  88. if (CurrentParam != null)
  89. {
  90. currentParam.AnnotIndex = annotIndex;
  91. }
  92. if (PreviousParam != null)
  93. {
  94. PreviousParam.AnnotIndex = annotIndex;
  95. }
  96. return true;
  97. }
  98. return false;
  99. }
  100. internal override bool Update(bool isUndo)
  101. {
  102. if (CurrentParam as LineParam == null || PreviousParam as LineParam == null)
  103. {
  104. return false;
  105. }
  106. if (MakeAnnotValid(CurrentParam))
  107. {
  108. CPDFLineAnnotation lineAnnot = Annot as CPDFLineAnnotation;
  109. if (lineAnnot == null || !lineAnnot.IsValid())
  110. {
  111. return false;
  112. }
  113. LineParam updateParam = (isUndo ? PreviousParam : CurrentParam)as LineParam;
  114. LineParam checkParam = (isUndo ? CurrentParam : PreviousParam) as LineParam;
  115. if(updateParam.HeadLineType!=checkParam.HeadLineType || updateParam.TailLineType!=checkParam.TailLineType)
  116. {
  117. lineAnnot.SetLineType(updateParam.HeadLineType, updateParam.TailLineType);
  118. }
  119. if (!CheckArrayEqual(updateParam.LineColor,checkParam.LineColor))
  120. {
  121. if(updateParam.LineColor != null && updateParam.LineColor.Length==3)
  122. {
  123. lineAnnot.SetLineColor(updateParam.LineColor);
  124. }
  125. }
  126. if(!CheckArrayEqual(updateParam.BgColor, checkParam.BgColor))
  127. {
  128. if (updateParam.HasBgColor)
  129. {
  130. if (updateParam.BgColor != null && updateParam.BgColor.Length == 3)
  131. {
  132. lineAnnot.SetBgColor(updateParam.BgColor);
  133. }
  134. }
  135. else
  136. {
  137. lineAnnot.ClearBgColor();
  138. }
  139. }
  140. if(updateParam.Transparency!=checkParam.Transparency)
  141. {
  142. lineAnnot.SetTransparency((byte)updateParam.Transparency);
  143. }
  144. if(updateParam.LineWidth!=checkParam.LineWidth)
  145. {
  146. lineAnnot.SetLineWidth((byte)updateParam.LineWidth);
  147. }
  148. if(!updateParam.HeadPoint.Equals(checkParam.HeadPoint) || !updateParam.TailPoint.Equals(checkParam.TailPoint))
  149. {
  150. lineAnnot.SetLinePoints(updateParam.HeadPoint,updateParam.TailPoint);
  151. }
  152. if(!updateParam.ClientRect.Equals(checkParam.ClientRect))
  153. {
  154. lineAnnot.SetRect(updateParam.ClientRect);
  155. }
  156. if (updateParam.LineDash != null)
  157. {
  158. List<float> floatArray = new List<float>();
  159. if (updateParam.LineDash != null && updateParam.LineDash.Length > 0)
  160. {
  161. foreach (double num in updateParam.LineDash)
  162. {
  163. floatArray.Add((float)num);
  164. }
  165. }
  166. lineAnnot.SetBorderStyle(updateParam.BorderStyle, floatArray.ToArray());
  167. }
  168. else
  169. {
  170. float[] dashArray = new float[0];
  171. lineAnnot.SetBorderStyle(updateParam.BorderStyle, dashArray);
  172. }
  173. if (updateParam.Author != checkParam.Author)
  174. {
  175. lineAnnot.SetAuthor(updateParam.Author);
  176. }
  177. if(updateParam.Content != checkParam.Content)
  178. {
  179. lineAnnot.SetContent(updateParam.Content);
  180. }
  181. if(updateParam.Locked != checkParam.Locked)
  182. {
  183. lineAnnot.SetIsLocked(updateParam.Locked);
  184. }
  185. lineAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  186. lineAnnot.UpdateAp();
  187. return true;
  188. }
  189. return false;
  190. }
  191. internal override bool Remove()
  192. {
  193. if (MakeAnnotValid(CurrentParam))
  194. {
  195. Annot.RemoveAnnot();
  196. return true;
  197. }
  198. return false;
  199. }
  200. }
  201. }