SoundAnnotHistory.cs 5.2 KB

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