HighlightAnnotHistory.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 HighlightAnnotHistory: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. HighlightParam currentParam = CurrentParam as HighlightParam;
  40. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  41. {
  42. return false;
  43. }
  44. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  45. CPDFHighlightAnnotation highlightAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT) as CPDFHighlightAnnotation;
  46. if (highlightAnnot != null)
  47. {
  48. int annotIndex = pdfPage.GetAnnotCount() - 1;
  49. highlightAnnot.SetTransparency((byte)currentParam.Transparency);
  50. if (currentParam.QuardRects != null)
  51. {
  52. highlightAnnot.SetQuardRects(currentParam.QuardRects);
  53. }
  54. if (currentParam.HighlightColor != null && currentParam.HighlightColor.Length==3)
  55. {
  56. highlightAnnot.SetColor(currentParam.HighlightColor);
  57. }
  58. if (!string.IsNullOrEmpty(currentParam.Author))
  59. {
  60. highlightAnnot.SetAuthor(currentParam.Author);
  61. }
  62. if (!string.IsNullOrEmpty(currentParam.Content))
  63. {
  64. highlightAnnot.SetContent(currentParam.Content);
  65. }
  66. highlightAnnot.SetRect(currentParam.ClientRect);
  67. highlightAnnot.SetIsLocked(currentParam.Locked);
  68. highlightAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  69. highlightAnnot.UpdateAp();
  70. highlightAnnot.ReleaseAnnot();
  71. if (currentParam != null)
  72. {
  73. currentParam.AnnotIndex = annotIndex;
  74. }
  75. if (PreviousParam != null)
  76. {
  77. PreviousParam.AnnotIndex = annotIndex;
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. internal override bool Update(bool isUndo)
  84. {
  85. if (CurrentParam as HighlightParam == null || PreviousParam as HighlightParam == null)
  86. {
  87. return false;
  88. }
  89. if (MakeAnnotValid(CurrentParam))
  90. {
  91. CPDFHighlightAnnotation highlightAnnot = Annot as CPDFHighlightAnnotation;
  92. if (highlightAnnot == null || !highlightAnnot.IsValid())
  93. {
  94. return false;
  95. }
  96. HighlightParam updateParam = (isUndo ? PreviousParam : CurrentParam) as HighlightParam;
  97. HighlightParam checkParam = (isUndo ? CurrentParam : PreviousParam) as HighlightParam;
  98. if (!CheckArrayEqual(updateParam.HighlightColor, checkParam.HighlightColor))
  99. {
  100. if(updateParam.HighlightColor != null && updateParam.HighlightColor.Length==3)
  101. {
  102. highlightAnnot.SetColor(updateParam.HighlightColor);
  103. }
  104. }
  105. if (updateParam.Transparency != checkParam.Transparency)
  106. {
  107. highlightAnnot.SetTransparency((byte)updateParam.Transparency);
  108. }
  109. if (updateParam.Author != checkParam.Author)
  110. {
  111. highlightAnnot.SetAuthor(updateParam.Author);
  112. }
  113. if (updateParam.Content != checkParam.Content)
  114. {
  115. highlightAnnot.SetContent(updateParam.Content);
  116. }
  117. if (updateParam.Locked != checkParam.Locked)
  118. {
  119. highlightAnnot.SetIsLocked(updateParam.Locked);
  120. }
  121. highlightAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  122. highlightAnnot.UpdateAp();
  123. return true;
  124. }
  125. return false;
  126. }
  127. internal override bool Remove()
  128. {
  129. if (MakeAnnotValid(CurrentParam))
  130. {
  131. Annot.RemoveAnnot();
  132. return true;
  133. }
  134. return false;
  135. }
  136. }
  137. }