StampAnnotHistory.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.SetImageStamp(
  102. imageData,
  103. imageWidth,
  104. imageHeight,
  105. pdfPage.Rotation);
  106. }
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. stampAnnot.SetSourceRect(currentParam.SourceRect);
  113. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  114. stampAnnot.SetTransparency(currentParam.Transparency);
  115. string imagePath = string.Empty;
  116. if (currentParam.Transparency <255 && GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
  117. {
  118. stampAnnot.SetImageStamp(imagePath, string.Empty);
  119. File.Delete(imagePath);
  120. }
  121. if (!string.IsNullOrEmpty(currentParam.Author))
  122. {
  123. stampAnnot.SetAuthor(currentParam.Author);
  124. }
  125. if (!string.IsNullOrEmpty(currentParam.Content))
  126. {
  127. stampAnnot.SetContent(currentParam.Content);
  128. }
  129. stampAnnot.SetIsLocked(currentParam.Locked);
  130. stampAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  131. stampAnnot.UpdateAp();
  132. stampAnnot.ReleaseAnnot();
  133. if (currentParam != null)
  134. {
  135. currentParam.AnnotIndex = annotIndex;
  136. }
  137. if (PreviousParam != null)
  138. {
  139. PreviousParam.AnnotIndex = annotIndex;
  140. }
  141. return true;
  142. }
  143. return false;
  144. }
  145. internal override bool Update(bool isUndo)
  146. {
  147. if (CurrentParam as StampParam == null || PreviousParam as StampParam == null)
  148. {
  149. return false;
  150. }
  151. if (MakeAnnotValid(CurrentParam))
  152. {
  153. CPDFStampAnnotation stampAnnot = Annot as CPDFStampAnnotation;
  154. if (stampAnnot == null || !stampAnnot.IsValid())
  155. {
  156. return false;
  157. }
  158. StampParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StampParam;
  159. StampParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StampParam;
  160. if (updateParam.Transparency != checkParam.Transparency)
  161. {
  162. stampAnnot.SetTransparency(updateParam.Transparency);
  163. string imagePath = string.Empty;
  164. if (GetAnnotOpacityImage(stampAnnot, updateParam.Transparency/255.0, 5, out imagePath) && File.Exists(imagePath))
  165. {
  166. stampAnnot.SetImageStamp(imagePath, string.Empty);
  167. File.Delete(imagePath);
  168. }
  169. }
  170. if (!updateParam.SourceRect.Equals(checkParam.SourceRect))
  171. {
  172. stampAnnot.SetSourceRect(updateParam.SourceRect);
  173. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  174. }
  175. if (updateParam.Rotation != checkParam.Rotation)
  176. {
  177. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  178. }
  179. if (updateParam.Author != checkParam.Author)
  180. {
  181. stampAnnot.SetAuthor(updateParam.Author);
  182. }
  183. if (updateParam.Content != checkParam.Content)
  184. {
  185. stampAnnot.SetContent(updateParam.Content);
  186. }
  187. if (updateParam.Locked != checkParam.Locked)
  188. {
  189. stampAnnot.SetIsLocked(updateParam.Locked);
  190. }
  191. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  192. stampAnnot.UpdateAp();
  193. return true;
  194. }
  195. return false;
  196. }
  197. internal override bool Remove()
  198. {
  199. if (MakeAnnotValid(CurrentParam))
  200. {
  201. Annot.RemoveAnnot();
  202. return true;
  203. }
  204. return false;
  205. }
  206. private bool GetAnnotOpacityImage(CPDFStampAnnotation annot, double opacity, double zoom, out string imagePath)
  207. {
  208. imagePath = string.Empty;
  209. if (annot == null || annot.IsValid() == false)
  210. {
  211. return false;
  212. }
  213. try
  214. {
  215. CRect rawRect = annot.GetRect();
  216. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  217. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  218. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  219. int stride = ((int)drawRect.Width) * 4;
  220. for (int i = 0; i < (int)(drawRect.Height); i++)
  221. {
  222. for (int j = 0; j < (int)(drawRect.Width); j++)
  223. {
  224. byte b = imageData[i * stride + j * 4];
  225. byte g = imageData[i * stride + j * 4 + 1];
  226. byte r = imageData[i * stride + j * 4 + 2];
  227. byte a = imageData[i * stride + j * 4 + 3];
  228. if (a == 0 && b == 255 && g == 255 && r == 255)
  229. {
  230. continue;
  231. }
  232. if (a == 0 && b == 0 && g == 0 && r == 0)
  233. {
  234. continue;
  235. }
  236. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  237. }
  238. }
  239. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  240. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, stride, 0);
  241. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  242. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  243. using (FileStream fs = File.Create(tempPath))
  244. {
  245. pngEncoder.Frames.Add(BitmapFrame.Create(writeImage));
  246. pngEncoder.Save(fs);
  247. }
  248. imagePath = tempPath;
  249. return true;
  250. }
  251. catch (Exception ex)
  252. {
  253. }
  254. return false;
  255. }
  256. }
  257. }