StampAnnotHistory.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.UpdateAp();
  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. 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. stampAnnot.SetTransparency((byte)updateParam.Transparency);
  190. }
  191. if (!updateParam.ClientRect.Equals(checkParam.ClientRect))
  192. {
  193. stampAnnot.SetRect(updateParam.ClientRect);
  194. }
  195. if (updateParam.Author != checkParam.Author)
  196. {
  197. stampAnnot.SetAuthor(updateParam.Author);
  198. }
  199. if (updateParam.Content != checkParam.Content)
  200. {
  201. stampAnnot.SetContent(updateParam.Content);
  202. }
  203. if (updateParam.Locked != checkParam.Locked)
  204. {
  205. stampAnnot.SetIsLocked(updateParam.Locked);
  206. }
  207. stampAnnot.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  208. stampAnnot.UpdateAp();
  209. return true;
  210. }
  211. return false;
  212. }
  213. internal override bool Remove()
  214. {
  215. if (MakeAnnotValid(CurrentParam))
  216. {
  217. Annot.RemoveAnnot();
  218. return true;
  219. }
  220. return false;
  221. }
  222. private bool GetAnnotRotateImage(CPDFStampAnnotation annot, bool clockwise, double zoom, out string imagePath)
  223. {
  224. imagePath = string.Empty;
  225. if (annot == null || annot.IsValid() == false)
  226. {
  227. return false;
  228. }
  229. try
  230. {
  231. double opacity = annot.GetTransparency() / 255.0;
  232. CRect rawRect = annot.GetRect();
  233. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  234. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  235. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  236. int stride = ((int)drawRect.Width) * 4;
  237. for (int i = 0; i < (int)(drawRect.Height); i++)
  238. {
  239. for (int j = 0; j < (int)(drawRect.Width); j++)
  240. {
  241. byte b = imageData[i * stride + j * 4];
  242. byte g = imageData[i * stride + j * 4 + 1];
  243. byte r = imageData[i * stride + j * 4 + 2];
  244. byte a = imageData[i * stride + j * 4 + 3];
  245. if (a == 0 && b == 255 && g == 255 && r == 255)
  246. {
  247. continue;
  248. }
  249. if (a == 0 && opacity > 0)
  250. {
  251. imageData[i * stride + j * 4] = 255;
  252. imageData[i * stride + j * 4 + 1] = 255;
  253. imageData[i * stride + j * 4 + 2] = 255;
  254. continue;
  255. }
  256. if (opacity == 0)
  257. {
  258. imageData[i * stride + j * 4 + 3] = 255;
  259. continue;
  260. }
  261. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  262. }
  263. }
  264. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  265. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, ((int)drawRect.Width) * 4, 0);
  266. RotateTransform rotateTransform = new RotateTransform();
  267. rotateTransform.Angle = clockwise ? 90 : -90;
  268. rotateTransform.CenterX = drawRect.Width / 2;
  269. rotateTransform.CenterY = drawRect.Height / 2;
  270. TranslateTransform translateTransform = new TranslateTransform();
  271. if (clockwise == false)
  272. {
  273. translateTransform.X = -(drawRect.Width - drawRect.Height) / 2;
  274. translateTransform.Y = -(drawRect.Width - drawRect.Height) / 2;
  275. }
  276. else
  277. {
  278. translateTransform.X = (drawRect.Width - drawRect.Height) / 2;
  279. translateTransform.Y = (drawRect.Width - drawRect.Height) / 2;
  280. }
  281. DrawingVisual drawVisual = new DrawingVisual();
  282. DrawingContext drawDC = drawVisual.RenderOpen();
  283. drawDC.PushTransform(rotateTransform);
  284. drawDC.PushTransform(translateTransform);
  285. drawDC.DrawImage(writeImage, new Rect(0, 0, writeImage.Width, writeImage.Height));
  286. drawDC.Pop();
  287. drawDC.Pop();
  288. drawDC.Close();
  289. RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)drawRect.Height, (int)drawRect.Width, 96, 96, PixelFormats.Pbgra32);
  290. renderBitmap.Render(drawVisual);
  291. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  292. BitmapSource saveImageData = renderBitmap;
  293. if (opacity == 0)
  294. {
  295. stride = saveImageData.PixelWidth * 4;
  296. imageData = new byte[stride * saveImageData.PixelHeight];
  297. saveImageData.CopyPixels(imageData, stride, 0);
  298. for (int i = 0; i < saveImageData.PixelHeight; i++)
  299. {
  300. for (int j = 0; j < saveImageData.PixelWidth; j++)
  301. {
  302. byte b = imageData[i * stride + j * 4];
  303. byte g = imageData[i * stride + j * 4 + 1];
  304. byte r = imageData[i * stride + j * 4 + 2];
  305. byte a = imageData[i * stride + j * 4 + 3];
  306. if (a == 0 && b == 255 && g == 255 && r == 255)
  307. {
  308. continue;
  309. }
  310. if (a == 0 && b == 0 && g == 0 && r == 0)
  311. {
  312. imageData[i * stride + j * 4] = 255;
  313. imageData[i * stride + j * 4 + 1] = 255;
  314. imageData[i * stride + j * 4 + 2] = 255;
  315. continue;
  316. }
  317. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  318. }
  319. }
  320. writeImage = new WriteableBitmap(saveImageData.PixelWidth, saveImageData.PixelHeight, 96, 96, PixelFormats.Bgra32, null);
  321. writeImage.WritePixels(new Int32Rect(0, 0, saveImageData.PixelWidth, saveImageData.PixelHeight), imageData, stride, 0);
  322. saveImageData = writeImage;
  323. }
  324. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  325. using (FileStream fs = File.Create(tempPath))
  326. {
  327. pngEncoder.Frames.Add(BitmapFrame.Create(saveImageData));
  328. pngEncoder.Save(fs);
  329. }
  330. imagePath = tempPath;
  331. return true;
  332. }
  333. catch (Exception ex)
  334. {
  335. }
  336. return false;
  337. }
  338. private bool GetAnnotOpacityImage(CPDFStampAnnotation annot, double opacity, double zoom, out string imagePath)
  339. {
  340. imagePath = string.Empty;
  341. if (annot == null || annot.IsValid() == false)
  342. {
  343. return false;
  344. }
  345. try
  346. {
  347. CRect rawRect = annot.GetRect();
  348. Rect drawRect = new Rect(0, 0, (int)(rawRect.width() * 96.0 / 72.0 * zoom), (int)(rawRect.height() * 96.0 / 72.0 * zoom));
  349. byte[] imageData = new byte[(int)drawRect.Width * (int)drawRect.Height * 4];
  350. annot.RenderAnnot((int)drawRect.Width, (int)drawRect.Height, imageData);
  351. byte annotOpacity = annot.GetTransparency();
  352. int stride = ((int)drawRect.Width) * 4;
  353. for (int i = 0; i < (int)(drawRect.Height); i++)
  354. {
  355. for (int j = 0; j < (int)(drawRect.Width); j++)
  356. {
  357. byte b = imageData[i * stride + j * 4];
  358. byte g = imageData[i * stride + j * 4 + 1];
  359. byte r = imageData[i * stride + j * 4 + 2];
  360. byte a = imageData[i * stride + j * 4 + 3];
  361. if (a == 0 && b == 255 && g == 255 && r == 255)
  362. {
  363. continue;
  364. }
  365. if (a == 0 && annotOpacity > 0)
  366. {
  367. imageData[i * stride + j * 4] = 255;
  368. imageData[i * stride + j * 4 + 1] = 255;
  369. imageData[i * stride + j * 4 + 2] = 255;
  370. continue;
  371. }
  372. imageData[i * stride + j * 4 + 3] = (byte)(opacity * 255);
  373. }
  374. }
  375. WriteableBitmap writeImage = new WriteableBitmap((int)drawRect.Width, (int)drawRect.Height, 96, 96, PixelFormats.Bgra32, null);
  376. writeImage.WritePixels(new Int32Rect(0, 0, (int)drawRect.Width, (int)drawRect.Height), imageData, stride, 0);
  377. string tempPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), Guid.NewGuid().ToString());
  378. PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
  379. using (FileStream fs = File.Create(tempPath))
  380. {
  381. pngEncoder.Frames.Add(BitmapFrame.Create(writeImage));
  382. pngEncoder.Save(fs);
  383. }
  384. imagePath = tempPath;
  385. return true;
  386. }
  387. catch (Exception ex)
  388. {
  389. }
  390. return false;
  391. }
  392. }
  393. }