StickyNoteAnnotHistory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Tool.Help;
  4. namespace ComPDFKit.Tool.UndoManger
  5. {
  6. public class StickyNoteAnnotHistory:AnnotHistory
  7. {
  8. public override int GetAnnotIndex()
  9. {
  10. if (CurrentParam != null)
  11. {
  12. return CurrentParam.AnnotIndex;
  13. }
  14. return base.GetAnnotIndex();
  15. }
  16. public override int GetPageIndex()
  17. {
  18. if (CurrentParam != null)
  19. {
  20. return CurrentParam.PageIndex;
  21. }
  22. return base.GetPageIndex();
  23. }
  24. public override void SetAnnotIndex(int newIndex)
  25. {
  26. if (CurrentParam != null)
  27. {
  28. CurrentParam.AnnotIndex = newIndex;
  29. }
  30. if (PreviousParam != null)
  31. {
  32. PreviousParam.AnnotIndex = newIndex;
  33. }
  34. }
  35. internal override bool Add()
  36. {
  37. StickyNoteParam currentParam = CurrentParam as StickyNoteParam;
  38. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  39. {
  40. return false;
  41. }
  42. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  43. CPDFTextAnnotation stickynoteAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_TEXT) as CPDFTextAnnotation;
  44. if(stickynoteAnnot != null)
  45. {
  46. int annotIndex = pdfPage.GetAnnotCount() - 1;
  47. if(currentParam.StickyNoteColor!=null && currentParam.StickyNoteColor.Length==3)
  48. {
  49. stickynoteAnnot.SetColor(currentParam.StickyNoteColor);
  50. }
  51. stickynoteAnnot.SetTransparency((byte)currentParam.Transparency);
  52. stickynoteAnnot.SetRect(currentParam.ClientRect);
  53. if (!string.IsNullOrEmpty(currentParam.Author))
  54. {
  55. stickynoteAnnot.SetAuthor(currentParam.Author);
  56. }
  57. if (!string.IsNullOrEmpty(currentParam.Content))
  58. {
  59. stickynoteAnnot.SetContent(currentParam.Content);
  60. }
  61. stickynoteAnnot.SetIsLocked(currentParam.Locked);
  62. stickynoteAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  63. stickynoteAnnot.UpdateAp();
  64. stickynoteAnnot.ReleaseAnnot();
  65. if (currentParam != null)
  66. {
  67. currentParam.AnnotIndex = annotIndex;
  68. }
  69. if (PreviousParam != null)
  70. {
  71. PreviousParam.AnnotIndex = annotIndex;
  72. }
  73. return true;
  74. }
  75. return false;
  76. }
  77. internal override bool Update(bool isUndo)
  78. {
  79. if (CurrentParam as StickyNoteParam == null || PreviousParam as StickyNoteParam == null)
  80. {
  81. return false;
  82. }
  83. if (MakeAnnotValid(CurrentParam))
  84. {
  85. CPDFTextAnnotation stickynoteAnnot = Annot as CPDFTextAnnotation;
  86. if (stickynoteAnnot == null || !stickynoteAnnot.IsValid())
  87. {
  88. return false;
  89. }
  90. StickyNoteParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StickyNoteParam;
  91. StickyNoteParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StickyNoteParam;
  92. if (!CheckArrayEqual(updateParam.StickyNoteColor, checkParam.StickyNoteColor))
  93. {
  94. if (updateParam.StickyNoteColor != null && updateParam.StickyNoteColor.Length == 3)
  95. {
  96. stickynoteAnnot.SetColor(updateParam.StickyNoteColor);
  97. }
  98. }
  99. if (updateParam.Transparency != checkParam.Transparency)
  100. {
  101. stickynoteAnnot.SetTransparency((byte)updateParam.Transparency);
  102. }
  103. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  104. {
  105. stickynoteAnnot.SetRect(updateParam.ClientRect);
  106. }
  107. if (updateParam.Author != checkParam.Author)
  108. {
  109. stickynoteAnnot.SetAuthor(updateParam.Author);
  110. }
  111. if (updateParam.Content != checkParam.Content)
  112. {
  113. stickynoteAnnot.SetContent(updateParam.Content);
  114. }
  115. if (updateParam.Locked != checkParam.Locked)
  116. {
  117. stickynoteAnnot.SetIsLocked(updateParam.Locked);
  118. }
  119. stickynoteAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  120. stickynoteAnnot.UpdateAp();
  121. return true;
  122. }
  123. return false;
  124. }
  125. internal override bool Remove()
  126. {
  127. if (MakeAnnotValid(CurrentParam))
  128. {
  129. Annot.RemoveAnnot();
  130. return true;
  131. }
  132. return false;
  133. }
  134. }
  135. }