StampAnnotHistory.cs 14 KB

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