StampAnnotHistory.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. //stampAnnot.SetRect(currentParam.ClientRect);
  72. }
  73. break;
  74. case C_STAMP_TYPE.TEXT_STAMP:
  75. {
  76. string dateText = currentParam.DateText;
  77. string stampText = currentParam.StampText;
  78. if (dateText == null)
  79. {
  80. dateText = string.Empty;
  81. }
  82. if (stampText == null)
  83. {
  84. stampText = string.Empty;
  85. }
  86. stampAnnot.SetTextStamp(
  87. stampText,
  88. dateText,
  89. currentParam.TextStampShape,
  90. currentParam.TextStampColor,
  91. pdfPage.Rotation);
  92. //stampAnnot.SetRect(currentParam.ClientRect);
  93. }
  94. break;
  95. case C_STAMP_TYPE.IMAGE_STAMP:
  96. {
  97. byte[] imageData = null;
  98. int imageWidth = 0;
  99. int imageHeight = 0;
  100. PDFHelp.ImageStreamToByte(currentParam.ImageStream, ref imageData, ref imageWidth, ref imageHeight);
  101. if (imageData != null && imageWidth > 0 && imageHeight > 0)
  102. {
  103. //stampAnnot.SetRect(currentParam.ClientRect);
  104. stampAnnot.SetImageStamp(
  105. imageData,
  106. imageWidth,
  107. imageHeight,
  108. pdfPage.Rotation);
  109. }
  110. }
  111. break;
  112. default:
  113. break;
  114. }
  115. stampAnnot.SetSourceRect(currentParam.SourceRect);
  116. stampAnnot.AnnotationRotator.SetRotation(currentParam.Rotation);
  117. stampAnnot.SetTransparency((byte)currentParam.Transparency);
  118. string imagePath = string.Empty;
  119. if (GetAnnotOpacityImage(stampAnnot, currentParam.Transparency / 255.0, 5, out imagePath) && File.Exists(imagePath))
  120. {
  121. stampAnnot.SetImageStamp(imagePath, string.Empty);
  122. File.Delete(imagePath);
  123. }
  124. if (!string.IsNullOrEmpty(currentParam.Author))
  125. {
  126. stampAnnot.SetAuthor(currentParam.Author);
  127. }
  128. if (!string.IsNullOrEmpty(currentParam.Content))
  129. {
  130. stampAnnot.SetContent(currentParam.Content);
  131. }
  132. stampAnnot.SetIsLocked(currentParam.Locked);
  133. stampAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  134. stampAnnot.UpdateAp();
  135. stampAnnot.ReleaseAnnot();
  136. if (currentParam != null)
  137. {
  138. currentParam.AnnotIndex = annotIndex;
  139. }
  140. if (PreviousParam != null)
  141. {
  142. PreviousParam.AnnotIndex = annotIndex;
  143. }
  144. return true;
  145. }
  146. return false;
  147. }
  148. internal override bool Update(bool isUndo)
  149. {
  150. if (CurrentParam as StampParam == null || PreviousParam as StampParam == null)
  151. {
  152. return false;
  153. }
  154. if (MakeAnnotValid(CurrentParam))
  155. {
  156. CPDFStampAnnotation stampAnnot = Annot as CPDFStampAnnotation;
  157. if (stampAnnot == null || !stampAnnot.IsValid())
  158. {
  159. return false;
  160. }
  161. StampParam updateParam = (isUndo ? PreviousParam : CurrentParam) as StampParam;
  162. StampParam checkParam = (isUndo ? CurrentParam : PreviousParam) as StampParam;
  163. if (updateParam.Clockwise!=0)
  164. {
  165. string imagePath = string.Empty;
  166. bool clockwise = updateParam.Clockwise== 1 ? true : false;
  167. if(isUndo)
  168. {
  169. clockwise=!clockwise;
  170. }
  171. if (GetAnnotRotateImage(stampAnnot, clockwise, 5, out imagePath) && File.Exists(imagePath))
  172. {
  173. CRect rawRect = stampAnnot.GetRect();
  174. Point centerPos = new Point(rawRect.left + rawRect.width() / 2, rawRect.top + rawRect.height() / 2);
  175. Rect saveRect = new Rect(centerPos.X - rawRect.height() / 2, centerPos.Y - rawRect.width() / 2, rawRect.height(), rawRect.width());
  176. stampAnnot.SetRect(new CRect((float)saveRect.Left, (float)saveRect.Bottom, (float)saveRect.Right, (float)saveRect.Top));
  177. stampAnnot.SetImageStamp(imagePath, string.Empty);
  178. File.Delete(imagePath);
  179. }
  180. }
  181. if (updateParam.Transparency != checkParam.Transparency)
  182. {
  183. stampAnnot.SetTransparency((byte)updateParam.Transparency);
  184. string imagePath = string.Empty;
  185. if (GetAnnotOpacityImage(stampAnnot, updateParam.Transparency/255.0, 5, out imagePath) && File.Exists(imagePath))
  186. {
  187. stampAnnot.SetImageStamp(imagePath, string.Empty);
  188. File.Delete(imagePath);
  189. }
  190. }
  191. if (!updateParam.SourceRect.Equals(checkParam.SourceRect))
  192. {
  193. stampAnnot.SetSourceRect(updateParam.SourceRect);
  194. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  195. }
  196. if (updateParam.Rotation != checkParam.Rotation)
  197. {
  198. stampAnnot.AnnotationRotator.SetRotation(updateParam.Rotation);
  199. }
  200. if (updateParam.Author != checkParam.Author)
  201. {
  202. stampAnnot.SetAuthor(updateParam.Author);
  203. }
  204. if (updateParam.Content != checkParam.Content)
  205. {
  206. stampAnnot.SetContent(updateParam.Content);
  207. }
  208. if (updateParam.Locked != checkParam.Locked)
  209. {
  210. stampAnnot.SetIsLocked(updateParam.Locked);
  211. }
  212. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  213. stampAnnot.UpdateAp();
  214. return true;
  215. }
  216. return false;
  217. }
  218. internal override bool Remove()
  219. {
  220. if (MakeAnnotValid(CurrentParam))
  221. {
  222. Annot.RemoveAnnot();
  223. return true;
  224. }
  225. return false;
  226. }
  227. private bool GetAnnotRotateImage(CPDFStampAnnotation annot, bool clockwise, double zoom, out string imagePath)
  228. {
  229. imagePath = string.Empty;
  230. if (annot == null || annot.IsValid() == false)
  231. {
  232. return false;
  233. }
  234. try
  235. {
  236. double opacity = annot.GetTransparency() / 255.0;
  237. CRect rawRect = annot.GetRect();
  238. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  239. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  240. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  241. int stride = ((int)drawRect.Width) * 4;
  242. for (int i = 0; i < (int)(drawRect.Height); i++)
  243. {
  244. for (int j = 0; j < (int)(drawRect.Width); j++)
  245. {
  246. byte b = imageData[i * stride + j * 4];
  247. byte g = imageData[i * stride + j * 4 + 1];
  248. byte r = imageData[i * stride + j * 4 + 2];
  249. byte a = imageData[i * stride + j * 4 + 3];
  250. if (a == 0 && b == 255 && g == 255 && r == 255)
  251. {
  252. continue;
  253. }
  254. if (a == 0 && b == 0 && g == 0 && r == 0)
  255. {
  256. continue;
  257. }
  258. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  259. }
  260. }
  261. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  262. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, ((int)drawRect.Width) * 4, 0);
  263. RotateTransform rotateTransform = new RotateTransform();
  264. rotateTransform.Angle = clockwise ? 90 : -90;
  265. rotateTransform.CenterX = drawRect.Width / 2;
  266. rotateTransform.CenterY = drawRect.Height / 2;
  267. TranslateTransform translateTransform = new TranslateTransform();
  268. if (clockwise == false)
  269. {
  270. translateTransform.X = -(drawRect.Width - drawRect.Height) / 2;
  271. translateTransform.Y = -(drawRect.Width - drawRect.Height) / 2;
  272. }
  273. else
  274. {
  275. translateTransform.X = (drawRect.Width - drawRect.Height) / 2;
  276. translateTransform.Y = (drawRect.Width - drawRect.Height) / 2;
  277. }
  278. DrawingVisual drawVisual = new DrawingVisual();
  279. DrawingContext drawDC = drawVisual.RenderOpen();
  280. drawDC.PushTransform(rotateTransform);
  281. drawDC.PushTransform(translateTransform);
  282. drawDC.DrawImage(writeImage, new Rect(0, 0, writeImage.Width, writeImage.Height));
  283. drawDC.Pop();
  284. drawDC.Pop();
  285. drawDC.Close();
  286. RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)drawRect.Height, (int)drawRect.Width, 96, 96, PixelFormats.Pbgra32);
  287. renderBitmap.Render(drawVisual);
  288. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  289. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  290. using (FileStream fs = File.Create(tempPath))
  291. {
  292. pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));
  293. pngEncoder.Save(fs);
  294. }
  295. imagePath = tempPath;
  296. return true;
  297. }
  298. catch (Exception ex)
  299. {
  300. }
  301. return false;
  302. }
  303. private bool GetAnnotOpacityImage(CPDFStampAnnotation annot, double opacity, double zoom, out string imagePath)
  304. {
  305. imagePath = string.Empty;
  306. if (annot == null || annot.IsValid() == false)
  307. {
  308. return false;
  309. }
  310. try
  311. {
  312. CRect rawRect = annot.GetRect();
  313. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  314. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  315. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  316. int stride = ((int)drawRect.Width) * 4;
  317. for (int i = 0; i < (int)(drawRect.Height); i++)
  318. {
  319. for (int j = 0; j < (int)(drawRect.Width); j++)
  320. {
  321. byte b = imageData[i * stride + j * 4];
  322. byte g = imageData[i * stride + j * 4 + 1];
  323. byte r = imageData[i * stride + j * 4 + 2];
  324. byte a = imageData[i * stride + j * 4 + 3];
  325. if (a == 0 && b == 255 && g == 255 && r == 255)
  326. {
  327. continue;
  328. }
  329. if (a == 0 && b == 0 && g == 0 && r == 0)
  330. {
  331. continue;
  332. }
  333. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  334. }
  335. }
  336. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  337. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, stride, 0);
  338. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  339. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  340. using (FileStream fs = File.Create(tempPath))
  341. {
  342. pngEncoder.Frames.Add(BitmapFrame.Create(writeImage));
  343. pngEncoder.Save(fs);
  344. }
  345. imagePath = tempPath;
  346. return true;
  347. }
  348. catch (Exception ex)
  349. {
  350. }
  351. return false;
  352. }
  353. }
  354. }