PDFHelp.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.DrawTool;
  6. using ComPDFKit.Viewer.Helper;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Reflection;
  10. using System.Windows;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Media;
  13. using System.IO;
  14. namespace ComPDFKit.Tool.Help
  15. {
  16. public class PDFHelp
  17. {
  18. public static List<TextDrawRect> GetSelectTextRect(CPDFDocument document, int pageIndex, Point startPoint, Point endPoint, Point tolerance)
  19. {
  20. CPDFPage page = document.PageAtIndex(pageIndex);
  21. if (page != null)
  22. {
  23. CPDFTextPage textPage = page.GetTextPage();
  24. if (textPage != null)
  25. {
  26. List<CRect> selectList = textPage.GetCharsRectAtPos(
  27. DataConversionForWPF.PointConversionForCPoint(startPoint),
  28. DataConversionForWPF.PointConversionForCPoint(endPoint),
  29. new CPoint(10, 10)
  30. );
  31. List<TextDrawRect> drawList = new List<TextDrawRect>();
  32. foreach (CRect rawRect in selectList)
  33. {
  34. drawList.Add(new TextDrawRect() { DrawRect = DataConversionForWPF.CRectConversionForRect( rawRect), Text = "" });
  35. }
  36. return drawList;
  37. }
  38. }
  39. return new List<TextDrawRect>();
  40. }
  41. public static string GetSelectText(CPDFDocument document, int pageIndex, Point startPoint, Point endPoint,Point tolerance)
  42. {
  43. if (document != null)
  44. {
  45. CPDFPage page = document.PageAtIndex(pageIndex);
  46. if (page != null && page.IsValid())
  47. {
  48. CPDFTextPage textPage = page.GetTextPage();
  49. if (textPage != null && textPage.IsValid())
  50. {
  51. return textPage.GetSelectText(
  52. DataConversionForWPF.PointConversionForCPoint(startPoint),
  53. DataConversionForWPF.PointConversionForCPoint(endPoint),
  54. DataConversionForWPF.PointConversionForCPoint(tolerance)
  55. );
  56. }
  57. }
  58. }
  59. return string.Empty;
  60. }
  61. public static string GetDoubleClickText(CPDFDocument document, int pageIndex, Point startPoint, ref Rect uiRect)
  62. {
  63. CPDFPage page = document.PageAtIndex(pageIndex);
  64. if (page != null)
  65. {
  66. CPDFTextPage textPage = page.GetTextPage();
  67. if (textPage != null)
  68. {
  69. CRect wordRect = new CRect();
  70. string Word = textPage.GetSelectionForWordAtPos(
  71. DataConversionForWPF.PointConversionForCPoint(startPoint),
  72. new CPoint(5, 5), ref wordRect);
  73. uiRect= DataConversionForWPF.CRectConversionForRect(wordRect);
  74. return Word;
  75. }
  76. }
  77. return string.Empty;
  78. }
  79. public static string GetCurrentPdfTime()
  80. {
  81. DateTime localTime = DateTime.Now;
  82. DateTime utcTime = DateTime.UtcNow;
  83. int checkHour = (localTime - utcTime).Hours;
  84. if (checkHour > 0)
  85. {
  86. return "D:" + localTime.ToString("yyyyMMddHHmmss") + "+" + checkHour.ToString("D2") + "'00'";
  87. }
  88. if (checkHour < 0)
  89. {
  90. return "D:" + localTime.ToString("yyyyMMddHHmmss") + "-" + Math.Abs(checkHour).ToString("D2") + "'00'";
  91. }
  92. return "D:" + localTime.ToString("yyyyMMddHHmmss") + "Z00'00'";
  93. }
  94. public static bool IsTextAtPos(CPDFDocument document, int pageIndex, Point pagePoint)
  95. {
  96. if (document==null)
  97. {
  98. return false;
  99. }
  100. CPDFPage page = document.PageAtIndex(pageIndex);
  101. if (page == null)
  102. {
  103. return false;
  104. }
  105. CPDFTextPage textPage = page.GetTextPage();
  106. if (textPage == null)
  107. {
  108. return false;
  109. }
  110. var x = textPage.GetCharIndexAtPos(
  111. DataConversionForWPF.PointConversionForCPoint(pagePoint),
  112. new CPoint(10, 10)
  113. );
  114. return x != -1;
  115. }
  116. public static void ImagePathToByte(string imagePath, ref byte[] imageData, ref int imageWidth, ref int imageHeight)
  117. {
  118. if (!File.Exists(imagePath))
  119. return;
  120. imagePath = Path.GetFullPath(imagePath);
  121. BitmapFrame frame = null;
  122. BitmapDecoder decoder = BitmapDecoder.Create(new Uri(imagePath), BitmapCreateOptions.None, BitmapCacheOption.Default);
  123. if (decoder != null && decoder.Frames.Count > 0)
  124. {
  125. frame = decoder.Frames[0];
  126. }
  127. if (frame != null)
  128. {
  129. imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  130. if (frame.Format != PixelFormats.Bgra32)
  131. {
  132. FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  133. covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  134. }
  135. else
  136. {
  137. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  138. }
  139. imageWidth = frame.PixelWidth;
  140. imageHeight = frame.PixelHeight;
  141. }
  142. }
  143. public static void ImageStreamToByte(Stream imageStream, ref byte[] imageData, ref int imageWidth, ref int imageHeight)
  144. {
  145. imageStream.Seek(0, SeekOrigin.Begin);
  146. BitmapDecoder decoder = BitmapDecoder.Create(imageStream,BitmapCreateOptions.None,BitmapCacheOption.Default);
  147. BitmapFrame frame = null;
  148. if (decoder != null && decoder.Frames.Count > 0)
  149. {
  150. frame = decoder.Frames[0];
  151. }
  152. if (frame != null)
  153. {
  154. imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  155. if (frame.Format != PixelFormats.Bgra32)
  156. {
  157. FormatConvertedBitmap covert = new FormatConvertedBitmap(frame, PixelFormats.Bgra32, frame.Palette, 0);
  158. covert.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  159. }
  160. else
  161. {
  162. frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
  163. }
  164. int stride = ((int)frame.PixelWidth) * 4;
  165. for (int i = 0; i < (int)(frame.PixelHeight); i++)
  166. {
  167. for (int j = 0; j < (int)(frame.PixelWidth); j++)
  168. {
  169. byte b = imageData[i * stride + j * 4];
  170. byte g = imageData[i * stride + j * 4 + 1];
  171. byte r = imageData[i * stride + j * 4 + 2];
  172. byte a = imageData[i * stride + j * 4 + 3];
  173. if (a == 0 && b == 0 && g == 0 && r == 0)
  174. {
  175. imageData[i * stride + j * 4] = 255;
  176. imageData[i * stride + j * 4 + 1] = 255;
  177. imageData[i * stride + j * 4 + 2] = 255;
  178. continue;
  179. }
  180. }
  181. }
  182. imageWidth = frame.PixelWidth;
  183. imageHeight = frame.PixelHeight;
  184. }
  185. }
  186. }
  187. public static class PDFDateHelp
  188. {
  189. /// <summary>
  190. /// Gets image for the appearance stream of the text stamp.
  191. /// </summary>
  192. /// <param name="stampText">Text</param>
  193. /// <param name="date">Date</param>
  194. /// <param name="shape">Shape</param>
  195. /// <param name="color">Color</param>
  196. /// <param name="stampWidth">Width of stamp</param>
  197. /// <param name="stampHeight">Height of stamp</param>
  198. /// <param name="imageWidth">Width of image</param>
  199. /// <param name="imageHeight">Height of image</param>
  200. /// <param name="rotation">Rotation angle</param>
  201. /// <returns>Image data</returns>
  202. public static byte[] GetTempTextStampImage(string stampText, string date, C_TEXTSTAMP_SHAPE shape, C_TEXTSTAMP_COLOR color, out int stampWidth, out int stampHeight, out int imageWidth, out int imageHeight, int rotation = 0)
  203. {
  204. CPDFDocument doc = CPDFDocument.CreateDocument();
  205. string Path = null;
  206. doc.InsertPage(0, 800, 1200, Path);
  207. CPDFPage page = doc.PageAtIndex(0, false);
  208. CPDFStampAnnotation stampAnnot = page.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_STAMP) as CPDFStampAnnotation;
  209. stampAnnot.SetTextStamp(stampText, date, shape, color, rotation);
  210. stampAnnot.UpdateAp();
  211. var flags = BindingFlags.NonPublic | BindingFlags.Static;
  212. var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
  213. int dpi = (int)dpiProperty.GetValue(null, null);
  214. CRect rect = stampAnnot.GetRect();
  215. stampWidth = (int)rect.width();
  216. stampHeight = (int)rect.height();
  217. imageWidth = (int)(rect.width() * dpi / 72D * 2);
  218. imageHeight = (int)(rect.height() * dpi / 72D * 2);
  219. byte[] imageData = new byte[imageWidth * imageHeight * 4];
  220. stampAnnot.RenderAnnot(imageWidth, imageHeight, imageData);
  221. stampAnnot.ReleaseAnnot();
  222. doc.ReleasePages(0);
  223. doc.Release();
  224. return imageData;
  225. }
  226. }
  227. }