InkAnnotHistory.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using System.Collections.Generic;
  6. namespace ComPDFKit.Tool.UndoManger
  7. {
  8. public class InkAnnotHistory: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. InkParam currentParam = CurrentParam as InkParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFInkAnnotation inkAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK) as CPDFInkAnnotation;
  46. if(inkAnnot != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. if(currentParam.InkColor!=null && currentParam.InkColor.Length==3)
  50. {
  51. inkAnnot.SetInkColor(currentParam.InkColor);
  52. }
  53. inkAnnot.SetThickness((float)currentParam.Thickness);
  54. inkAnnot.SetInkPath(currentParam.InkPath);
  55. inkAnnot.SetTransparency(currentParam.Transparency);
  56. if (!string.IsNullOrEmpty(currentParam.Author))
  57. {
  58. inkAnnot.SetAuthor(currentParam.Author);
  59. }
  60. if (!string.IsNullOrEmpty(currentParam.Content))
  61. {
  62. inkAnnot.SetContent(currentParam.Content);
  63. }
  64. if (currentParam.Dash != null && currentParam.Dash.Length > 0)
  65. {
  66. inkAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED,currentParam.Dash);
  67. }
  68. inkAnnot.SetIsLocked(currentParam.Locked);
  69. inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  70. inkAnnot.UpdateAp();
  71. inkAnnot.ReleaseAnnot();
  72. if (currentParam != null)
  73. {
  74. currentParam.AnnotIndex = annotIndex;
  75. }
  76. if (PreviousParam != null)
  77. {
  78. PreviousParam.AnnotIndex = annotIndex;
  79. }
  80. return true;
  81. }
  82. return false;
  83. }
  84. internal override bool Update(bool isUndo)
  85. {
  86. if (CurrentParam as InkParam == null || PreviousParam as InkParam == null)
  87. {
  88. return false;
  89. }
  90. if (MakeAnnotValid(CurrentParam))
  91. {
  92. CPDFInkAnnotation inkAnnot = Annot as CPDFInkAnnotation;
  93. if (inkAnnot == null || !inkAnnot.IsValid())
  94. {
  95. return false;
  96. }
  97. InkParam updateParam = (isUndo ? PreviousParam : CurrentParam) as InkParam;
  98. InkParam checkParam = (isUndo ? CurrentParam : PreviousParam)as InkParam;
  99. if (!CheckArrayEqual(updateParam.InkColor,checkParam.InkColor))
  100. {
  101. if(updateParam.InkColor != null && updateParam.InkColor.Length==3)
  102. {
  103. inkAnnot.SetInkColor(updateParam.InkColor);
  104. }
  105. }
  106. if (updateParam.Thickness != checkParam.Thickness)
  107. {
  108. inkAnnot.SetThickness((byte)updateParam.Thickness);
  109. }
  110. if (updateParam.InkPath!=null)
  111. {
  112. inkAnnot.SetInkPath(updateParam.InkPath);
  113. }
  114. if (updateParam.Dash!=null && updateParam.Dash.Length>0)
  115. {
  116. inkAnnot.SetBorderStyle(C_BORDER_STYLE.BS_DASHDED,updateParam.Dash);
  117. }
  118. else
  119. {
  120. inkAnnot.SetBorderStyle(C_BORDER_STYLE.BS_SOLID,new float[0]);
  121. }
  122. if (updateParam.Transparency != checkParam.Transparency)
  123. {
  124. inkAnnot.SetTransparency((byte)updateParam.Transparency);
  125. }
  126. if (updateParam.Author != checkParam.Author)
  127. {
  128. inkAnnot.SetAuthor(updateParam.Author);
  129. }
  130. if (updateParam.Content != checkParam.Content)
  131. {
  132. inkAnnot.SetContent(updateParam.Content);
  133. }
  134. if (updateParam.Locked != checkParam.Locked)
  135. {
  136. inkAnnot.SetIsLocked(updateParam.Locked);
  137. }
  138. inkAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  139. inkAnnot.UpdateAp();
  140. return true;
  141. }
  142. return false;
  143. }
  144. internal override bool Remove()
  145. {
  146. if (MakeAnnotValid(CurrentParam))
  147. {
  148. Annot.RemoveAnnot();
  149. return true;
  150. }
  151. return false;
  152. }
  153. }
  154. }