InkAnnotHistory.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. inkAnnot.SetIsLocked(currentParam.Locked);
  65. inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  66. inkAnnot.UpdateAp();
  67. inkAnnot.ReleaseAnnot();
  68. if (currentParam != null)
  69. {
  70. currentParam.AnnotIndex = annotIndex;
  71. }
  72. if (PreviousParam != null)
  73. {
  74. PreviousParam.AnnotIndex = annotIndex;
  75. }
  76. return true;
  77. }
  78. return false;
  79. }
  80. internal override bool Update(bool isUndo)
  81. {
  82. if (CurrentParam as InkParam == null || PreviousParam as InkParam == null)
  83. {
  84. return false;
  85. }
  86. if (MakeAnnotValid(CurrentParam))
  87. {
  88. CPDFInkAnnotation inkAnnot = Annot as CPDFInkAnnotation;
  89. if (inkAnnot == null || !inkAnnot.IsValid())
  90. {
  91. return false;
  92. }
  93. InkParam updateParam = (isUndo ? PreviousParam : CurrentParam) as InkParam;
  94. InkParam checkParam = (isUndo ? CurrentParam : PreviousParam)as InkParam;
  95. if (!CheckArrayEqual(updateParam.InkColor,checkParam.InkColor))
  96. {
  97. if(updateParam.InkColor != null && updateParam.InkColor.Length==3)
  98. {
  99. inkAnnot.SetInkColor(updateParam.InkColor);
  100. }
  101. }
  102. if (updateParam.Thickness != checkParam.Thickness)
  103. {
  104. inkAnnot.SetThickness((byte)updateParam.Thickness);
  105. }
  106. if (updateParam.InkPath!=null)
  107. {
  108. inkAnnot.SetInkPath(updateParam.InkPath);
  109. }
  110. if (updateParam.Transparency != checkParam.Transparency)
  111. {
  112. inkAnnot.SetTransparency((byte)updateParam.Transparency);
  113. }
  114. if (updateParam.Author != checkParam.Author)
  115. {
  116. inkAnnot.SetAuthor(updateParam.Author);
  117. }
  118. if (updateParam.Content != checkParam.Content)
  119. {
  120. inkAnnot.SetContent(updateParam.Content);
  121. }
  122. if (updateParam.Locked != checkParam.Locked)
  123. {
  124. inkAnnot.SetIsLocked(updateParam.Locked);
  125. }
  126. inkAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  127. inkAnnot.UpdateAp();
  128. return true;
  129. }
  130. return false;
  131. }
  132. internal override bool Remove()
  133. {
  134. if (MakeAnnotValid(CurrentParam))
  135. {
  136. Annot.RemoveAnnot();
  137. return true;
  138. }
  139. return false;
  140. }
  141. }
  142. }