StampAnnotHistory.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Tool.Help;
  4. using System.IO;
  5. using System.Windows.Media;
  6. using System.Windows.Media.Imaging;
  7. namespace ComPDFKit.Tool.UndoManger
  8. {
  9. public class StampAnnotHistory:AnnotHistory
  10. {
  11. public override int GetAnnotIndex()
  12. {
  13. if (CurrentParam != null)
  14. {
  15. return CurrentParam.AnnotIndex;
  16. }
  17. return base.GetAnnotIndex();
  18. }
  19. public override int GetPageIndex()
  20. {
  21. if (CurrentParam != null)
  22. {
  23. return CurrentParam.PageIndex;
  24. }
  25. return base.GetPageIndex();
  26. }
  27. public override void SetAnnotIndex(int newIndex)
  28. {
  29. if (CurrentParam != null)
  30. {
  31. CurrentParam.AnnotIndex = newIndex;
  32. }
  33. if (PreviousParam != null)
  34. {
  35. PreviousParam.AnnotIndex = newIndex;
  36. }
  37. }
  38. internal override bool Add()
  39. {
  40. StampParam currentParam = CurrentParam as StampParam;
  41. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  42. {
  43. return false;
  44. }
  45. if(currentParam.StampType==C_STAMP_TYPE.UNKNOWN_STAMP)
  46. {
  47. return false;
  48. }
  49. if(currentParam.StampType==C_STAMP_TYPE.IMAGE_STAMP && currentParam.ImageStream==null)
  50. {
  51. return false;
  52. }
  53. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  54. CPDFStampAnnotation stampAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  55. if (stampAnnot != null)
  56. {
  57. int annotIndex = pdfPage.GetAnnotCount() - 1;
  58. switch(currentParam.StampType)
  59. {
  60. case C_STAMP_TYPE.STANDARD_STAMP:
  61. {
  62. string stampText = currentParam.StampText;
  63. if (stampText == null)
  64. {
  65. stampText = string.Empty;
  66. }
  67. stampAnnot.SetStandardStamp(stampText, pdfPage.Rotation);
  68. stampAnnot.SetRect(currentParam.ClientRect);
  69. }
  70. break;
  71. case C_STAMP_TYPE.TEXT_STAMP:
  72. {
  73. string dateText = currentParam.DateText;
  74. string stampText = currentParam.StampText;
  75. if (dateText == null)
  76. {
  77. dateText = string.Empty;
  78. }
  79. if (stampText == null)
  80. {
  81. stampText = string.Empty;
  82. }
  83. stampAnnot.SetTextStamp(
  84. stampText,
  85. dateText,
  86. currentParam.TextStampShape,
  87. currentParam.TextStampColor,
  88. pdfPage.Rotation);
  89. stampAnnot.SetRect(currentParam.ClientRect);
  90. }
  91. break;
  92. case C_STAMP_TYPE.IMAGE_STAMP:
  93. {
  94. byte[] imageData = null;
  95. int imageWidth = 0;
  96. int imageHeight = 0;
  97. PDFHelp.ImageStreamToByte(currentParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  98. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  99. {
  100. stampAnnot.SetRect(currentParam.ClientRect);
  101. stampAnnot.SetImageStamp(
  102. imageData,
  103. imageWidth,
  104. imageHeight,
  105. pdfPage.Rotation);
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. stampAnnot.SetTransparency((byte)currentParam.Transparency);
  113. if (!string.IsNullOrEmpty(currentParam.Author))
  114. {
  115. stampAnnot.SetAuthor(currentParam.Author);
  116. }
  117. if (!string.IsNullOrEmpty(currentParam.Content))
  118. {
  119. stampAnnot.SetContent(currentParam.Content);
  120. }
  121. stampAnnot.SetIsLocked(currentParam.Locked);
  122. stampAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  123. stampAnnot.UpdateAp();
  124. stampAnnot.ReleaseAnnot();
  125. if (currentParam != null)
  126. {
  127. currentParam.AnnotIndex = annotIndex;
  128. }
  129. if (PreviousParam != null)
  130. {
  131. PreviousParam.AnnotIndex = annotIndex;
  132. }
  133. return true;
  134. }
  135. return false;
  136. }
  137. internal override bool Update(bool isUndo)
  138. {
  139. if (CurrentParam as StampParam == null || PreviousParam as StampParam == null)
  140. {
  141. return false;
  142. }
  143. if (MakeAnnotValid(CurrentParam))
  144. {
  145. CPDFStampAnnotation stampAnnot = Annot as CPDFStampAnnotation;
  146. if (stampAnnot == null || !stampAnnot.IsValid())
  147. {
  148. return false;
  149. }
  150. StampParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StampParam;
  151. StampParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StampParam;
  152. if (updateParam.Transparency != checkParam.Transparency)
  153. {
  154. stampAnnot.SetTransparency((byte)updateParam.Transparency);
  155. }
  156. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  157. {
  158. stampAnnot.SetRect(updateParam.ClientRect);
  159. }
  160. if (updateParam.Author != checkParam.Author)
  161. {
  162. stampAnnot.SetAuthor(updateParam.Author);
  163. }
  164. if (updateParam.Content != checkParam.Content)
  165. {
  166. stampAnnot.SetContent(updateParam.Content);
  167. }
  168. if (updateParam.Locked != checkParam.Locked)
  169. {
  170. stampAnnot.SetIsLocked(updateParam.Locked);
  171. }
  172. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  173. stampAnnot.UpdateAp();
  174. return true;
  175. }
  176. return false;
  177. }
  178. internal override bool Remove()
  179. {
  180. if (MakeAnnotValid(CurrentParam))
  181. {
  182. Annot.RemoveAnnot();
  183. return true;
  184. }
  185. return false;
  186. }
  187. }
  188. }