StampAnnotHistory.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if (pdfPage == null)
  58. return false;
  59. CPDFStampAnnotation stampAnnot;
  60. if (currentParam.StampType == C_STAMP_TYPE.IMAGE_STAMP && currentParam.CopyImageAnnot != null)
  61. {
  62. stampAnnot = CPDFAnnotation.AddCopyAnnotToPage(PDFDoc, pdfPage, currentParam.CopyImageAnnot) as CPDFStampAnnotation;
  63. }
  64. else
  65. {
  66. stampAnnot = pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  67. }
  68. if (stampAnnot != null)
  69. {
  70. int annotIndex = pdfPage.GetAnnotCount() - 1;
  71. switch (currentParam.StampType)
  72. {
  73. case C_STAMP_TYPE.STANDARD_STAMP:
  74. {
  75. string stampText = currentParam.StampText;
  76. if (stampText == null)
  77. {
  78. stampText = string.Empty;
  79. }
  80. stampAnnot.SetStandardStamp(stampText, pdfPage.Rotation);
  81. }
  82. break;
  83. case C_STAMP_TYPE.TEXT_STAMP:
  84. {
  85. string dateText = currentParam.DateText;
  86. string stampText = currentParam.StampText;
  87. if (dateText == null)
  88. {
  89. dateText = string.Empty;
  90. }
  91. if (stampText == null)
  92. {
  93. stampText = string.Empty;
  94. }
  95. stampAnnot.SetTextStamp(
  96. stampText,
  97. dateText,
  98. currentParam.TextStampShape,
  99. currentParam.TextStampColor,
  100. pdfPage.Rotation);
  101. }
  102. break;
  103. case C_STAMP_TYPE.IMAGE_STAMP:
  104. {
  105. if(currentParam.CopyImageAnnot == null)
  106. {
  107. byte[] imageData = null;
  108. int imageWidth = 0;
  109. int imageHeight = 0;
  110. PDFHelp.ImageStreamToByte(currentParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  111. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  112. {
  113. stampAnnot.SetSourceRect(currentParam.SourceRect);
  114. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  115. stampAnnot.SetImageStamp(
  116. imageData,
  117. imageWidth,
  118. imageHeight,
  119. pdfPage.Rotation);
  120. }
  121. }
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. if (currentParam.StampType != C_STAMP_TYPE.IMAGE_STAMP || currentParam.CopyImageAnnot != null)
  128. {
  129. stampAnnot.SetSourceRect(currentParam.SourceRect);
  130. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  131. }
  132. stampAnnot.SetTransparency(currentParam.Transparency);
  133. string imagePath = string.Empty;
  134. if (currentParam.Transparency <255 && GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
  135. {
  136. stampAnnot.SetImageStamp(imagePath, string.Empty);
  137. File.Delete(imagePath);
  138. }
  139. if (!string.IsNullOrEmpty(currentParam.Author))
  140. {
  141. stampAnnot.SetAuthor(currentParam.Author);
  142. }
  143. if (!string.IsNullOrEmpty(currentParam.Content))
  144. {
  145. stampAnnot.SetContent(currentParam.Content);
  146. }
  147. stampAnnot.SetIsLocked(currentParam.Locked);
  148. stampAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  149. stampAnnot.UpdateAp();
  150. stampAnnot.ReleaseAnnot();
  151. if (currentParam != null)
  152. {
  153. currentParam.AnnotIndex = annotIndex;
  154. }
  155. if (PreviousParam != null)
  156. {
  157. PreviousParam.AnnotIndex = annotIndex;
  158. }
  159. return true;
  160. }
  161. return false;
  162. }
  163. internal override bool Update(bool isUndo)
  164. {
  165. if (CurrentParam as StampParam == null || PreviousParam as StampParam == null)
  166. {
  167. return false;
  168. }
  169. if (MakeAnnotValid(CurrentParam))
  170. {
  171. CPDFStampAnnotation stampAnnot = Annot as CPDFStampAnnotation;
  172. if (stampAnnot == null || !stampAnnot.IsValid())
  173. {
  174. return false;
  175. }
  176. StampParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StampParam;
  177. StampParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StampParam;
  178. if (updateParam.Transparency != checkParam.Transparency)
  179. {
  180. stampAnnot.SetTransparency(updateParam.Transparency);
  181. string imagePath = string.Empty;
  182. if (GetAnnotOpacityImage(stampAnnot, updateParam.Transparency/255.0, 5, out imagePath) && File.Exists(imagePath))
  183. {
  184. stampAnnot.SetImageStamp(imagePath, string.Empty);
  185. File.Delete(imagePath);
  186. }
  187. }
  188. if (!updateParam.SourceRect.Equals(checkParam.SourceRect))
  189. {
  190. stampAnnot.SetSourceRect(updateParam.SourceRect);
  191. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  192. }
  193. if (updateParam.Rotation != checkParam.Rotation)
  194. {
  195. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  196. }
  197. if (updateParam.Author != checkParam.Author)
  198. {
  199. stampAnnot.SetAuthor(updateParam.Author);
  200. }
  201. if (updateParam.Content != checkParam.Content)
  202. {
  203. stampAnnot.SetContent(updateParam.Content);
  204. }
  205. if (updateParam.Locked != checkParam.Locked)
  206. {
  207. stampAnnot.SetIsLocked(updateParam.Locked);
  208. }
  209. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  210. stampAnnot.UpdateAp();
  211. return true;
  212. }
  213. return false;
  214. }
  215. internal override bool Remove()
  216. {
  217. if (MakeAnnotValid(CurrentParam))
  218. {
  219. Annot.RemoveAnnot();
  220. return true;
  221. }
  222. return false;
  223. }
  224. private bool GetAnnotOpacityImage(CPDFStampAnnotation annot, double opacity, double zoom, out string imagePath)
  225. {
  226. imagePath = string.Empty;
  227. if (annot == null || annot.IsValid() == false)
  228. {
  229. return false;
  230. }
  231. try
  232. {
  233. CRect rawRect = annot.GetRect();
  234. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  235. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  236. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  237. int stride = ((int)drawRect.Width) * 4;
  238. for (int i = 0; i < (int)(drawRect.Height); i++)
  239. {
  240. for (int j = 0; j < (int)(drawRect.Width); j++)
  241. {
  242. byte b = imageData[i * stride + j * 4];
  243. byte g = imageData[i * stride + j * 4 + 1];
  244. byte r = imageData[i * stride + j * 4 + 2];
  245. byte a = imageData[i * stride + j * 4 + 3];
  246. if (a == 0 && b == 255 && g == 255 && r == 255)
  247. {
  248. continue;
  249. }
  250. if (a == 0 && b == 0 && g == 0 && r == 0)
  251. {
  252. continue;
  253. }
  254. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  255. }
  256. }
  257. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  258. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, stride, 0);
  259. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  260. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  261. using (FileStream fs = File.Create(tempPath))
  262. {
  263. pngEncoder.Frames.Add(BitmapFrame.Create(writeImage));
  264. pngEncoder.Save(fs);
  265. }
  266. imagePath = tempPath;
  267. return true;
  268. }
  269. catch (Exception ex)
  270. {
  271. }
  272. return false;
  273. }
  274. }
  275. }