StampAnnotHistory.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.Help;
  5. using System;
  6. using System.IO;
  7. using System.Windows;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. namespace ComPDFKit.Tool.UndoManger
  11. {
  12. public class StampAnnotHistory:AnnotHistory
  13. {
  14. public override int GetAnnotIndex()
  15. {
  16. if (CurrentParam != null)
  17. {
  18. return CurrentParam.AnnotIndex;
  19. }
  20. return base.GetAnnotIndex();
  21. }
  22. public override int GetPageIndex()
  23. {
  24. if (CurrentParam != null)
  25. {
  26. return CurrentParam.PageIndex;
  27. }
  28. return base.GetPageIndex();
  29. }
  30. public override void SetAnnotIndex(int newIndex)
  31. {
  32. if (CurrentParam != null)
  33. {
  34. CurrentParam.AnnotIndex = newIndex;
  35. }
  36. if (PreviousParam != null)
  37. {
  38. PreviousParam.AnnotIndex = newIndex;
  39. }
  40. }
  41. internal override bool Add()
  42. {
  43. StampParam currentParam = CurrentParam as StampParam;
  44. if (currentParam == null || PDFDoc == null || !PDFDoc.IsValid())
  45. {
  46. return false;
  47. }
  48. if(currentParam.StampType==C_STAMP_TYPE.UNKNOWN_STAMP)
  49. {
  50. return false;
  51. }
  52. if(currentParam.StampType==C_STAMP_TYPE.IMAGE_STAMP && currentParam.ImageStream==null)
  53. {
  54. return false;
  55. }
  56. CPDFPage pdfPage = PDFDoc.PageAtIndex(currentParam.PageIndex);
  57. CPDFStampAnnotation stampAnnot = pdfPage?.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  58. if (stampAnnot != null)
  59. {
  60. int annotIndex = pdfPage.GetAnnotCount() - 1;
  61. switch (currentParam.StampType)
  62. {
  63. case C_STAMP_TYPE.STANDARD_STAMP:
  64. {
  65. string stampText = currentParam.StampText;
  66. if (stampText == null)
  67. {
  68. stampText = string.Empty;
  69. }
  70. stampAnnot.SetStandardStamp(stampText, pdfPage.Rotation);
  71. }
  72. break;
  73. case C_STAMP_TYPE.TEXT_STAMP:
  74. {
  75. string dateText = currentParam.DateText;
  76. string stampText = currentParam.StampText;
  77. if (dateText == null)
  78. {
  79. dateText = string.Empty;
  80. }
  81. if (stampText == null)
  82. {
  83. stampText = string.Empty;
  84. }
  85. stampAnnot.SetTextStamp(
  86. stampText,
  87. dateText,
  88. currentParam.TextStampShape,
  89. currentParam.TextStampColor,
  90. pdfPage.Rotation);
  91. }
  92. break;
  93. case C_STAMP_TYPE.IMAGE_STAMP:
  94. {
  95. byte[] imageData = null;
  96. int imageWidth = 0;
  97. int imageHeight = 0;
  98. PDFHelp.ImageStreamToByte(currentParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  99. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  100. {
  101. stampAnnot.SetSourceRect(currentParam.SourceRect);
  102. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  103. stampAnnot.SetImageStamp(
  104. imageData,
  105. imageWidth,
  106. imageHeight,
  107. pdfPage.Rotation);
  108. }
  109. }
  110. break;
  111. default:
  112. break;
  113. }
  114. if(currentParam.StampType != C_STAMP_TYPE.IMAGE_STAMP)
  115. {
  116. stampAnnot.SetSourceRect(currentParam.SourceRect);
  117. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  118. }
  119. stampAnnot.SetTransparency(currentParam.Transparency);
  120. string imagePath = string.Empty;
  121. if (currentParam.Transparency <255 && GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
  122. {
  123. stampAnnot.SetImageStamp(imagePath, string.Empty);
  124. File.Delete(imagePath);
  125. }
  126. if (!string.IsNullOrEmpty(currentParam.Author))
  127. {
  128. stampAnnot.SetAuthor(currentParam.Author);
  129. }
  130. if (!string.IsNullOrEmpty(currentParam.Content))
  131. {
  132. stampAnnot.SetContent(currentParam.Content);
  133. }
  134. stampAnnot.SetIsLocked(currentParam.Locked);
  135. stampAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  136. stampAnnot.UpdateAp();
  137. stampAnnot.ReleaseAnnot();
  138. if (currentParam != null)
  139. {
  140. currentParam.AnnotIndex = annotIndex;
  141. }
  142. if (PreviousParam != null)
  143. {
  144. PreviousParam.AnnotIndex = annotIndex;
  145. }
  146. return true;
  147. }
  148. return false;
  149. }
  150. internal override bool Update(bool isUndo)
  151. {
  152. if (CurrentParam as StampParam == null || PreviousParam as StampParam == null)
  153. {
  154. return false;
  155. }
  156. if (MakeAnnotValid(CurrentParam))
  157. {
  158. CPDFStampAnnotation stampAnnot = Annot as CPDFStampAnnotation;
  159. if (stampAnnot == null || !stampAnnot.IsValid())
  160. {
  161. return false;
  162. }
  163. StampParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StampParam;
  164. StampParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StampParam;
  165. if (updateParam.Transparency != checkParam.Transparency)
  166. {
  167. stampAnnot.SetTransparency(updateParam.Transparency);
  168. string imagePath = string.Empty;
  169. if (GetAnnotOpacityImage(stampAnnot, updateParam.Transparency/255.0, 5, out imagePath) && File.Exists(imagePath))
  170. {
  171. stampAnnot.SetImageStamp(imagePath, string.Empty);
  172. File.Delete(imagePath);
  173. }
  174. }
  175. if (!updateParam.SourceRect.Equals(checkParam.SourceRect))
  176. {
  177. stampAnnot.SetSourceRect(updateParam.SourceRect);
  178. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  179. }
  180. if (updateParam.Rotation != checkParam.Rotation)
  181. {
  182. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  183. }
  184. if (updateParam.Author != checkParam.Author)
  185. {
  186. stampAnnot.SetAuthor(updateParam.Author);
  187. }
  188. if (updateParam.Content != checkParam.Content)
  189. {
  190. stampAnnot.SetContent(updateParam.Content);
  191. }
  192. if (updateParam.Locked != checkParam.Locked)
  193. {
  194. stampAnnot.SetIsLocked(updateParam.Locked);
  195. }
  196. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  197. stampAnnot.UpdateAp();
  198. return true;
  199. }
  200. return false;
  201. }
  202. internal override bool Remove()
  203. {
  204. if (MakeAnnotValid(CurrentParam))
  205. {
  206. Annot.RemoveAnnot();
  207. return true;
  208. }
  209. return false;
  210. }
  211. private bool GetAnnotOpacityImage(CPDFStampAnnotation annot, double opacity, double zoom, out string imagePath)
  212. {
  213. imagePath = string.Empty;
  214. if (annot == null || annot.IsValid() == false)
  215. {
  216. return false;
  217. }
  218. try
  219. {
  220. CRect rawRect = annot.GetRect();
  221. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  222. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  223. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  224. byte annotOpacity = annot.GetTransparency();
  225. int stride = ((int)drawRect.Width) * 4;
  226. for (int i = 0; i < (int)(drawRect.Height); i++)
  227. {
  228. for (int j = 0; j < (int)(drawRect.Width); j++)
  229. {
  230. byte b = imageData[i * stride + j * 4];
  231. byte g = imageData[i * stride + j * 4 + 1];
  232. byte r = imageData[i * stride + j * 4 + 2];
  233. byte a = imageData[i * stride + j * 4 + 3];
  234. if (a == 0 && b == 255 && g == 255 && r == 255)
  235. {
  236. continue;
  237. }
  238. if (a == 0 && annotOpacity > 0)
  239. {
  240. imageData[i * stride + j * 4] = 255;
  241. imageData[i * stride + j * 4 + 1] = 255;
  242. imageData[i * stride + j * 4 + 2] = 255;
  243. continue;
  244. }
  245. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  246. }
  247. }
  248. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  249. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, stride, 0);
  250. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  251. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  252. using (FileStream fs = File.Create(tempPath))
  253. {
  254. pngEncoder.Frames.Add(BitmapFrame.Create(writeImage));
  255. pngEncoder.Save(fs);
  256. }
  257. imagePath = tempPath;
  258. return true;
  259. }
  260. catch (Exception ex)
  261. {
  262. }
  263. return false;
  264. }
  265. }
  266. }