CPdfThumbnailControl.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Viewer.Helper;
  4. using ComPDFKit.Controls.PDFControlUI;
  5. using ComPDFKitViewer;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using ComPDFKit.Import;
  15. using ComPDFKit.Controls.Printer;
  16. using System.Runtime.InteropServices;
  17. namespace ComPDFKit.Controls.PDFControl
  18. {
  19. public partial class CPDFThumbnailControl : UserControl
  20. {
  21. /// <summary>
  22. /// PDFViewer
  23. /// </summary>
  24. private PDFViewControl ViewControl;
  25. /// <summary>
  26. /// Whether the thumbnail has been loaded
  27. /// </summary>
  28. public bool ThumbLoaded { get; set; }
  29. /// <summary>
  30. /// A collection of scale factors
  31. /// </summary>
  32. private int[] thumbnailSize = { 50, 100, 150, 200, 300, 500 };
  33. /// <summary>
  34. /// Scale factor
  35. /// </summary>
  36. private int zoomLevel = 2;
  37. /// <summary>
  38. /// A list of thumbnail data
  39. /// </summary>
  40. private List<ThumbnailItem> thumbnailItemList { get; set; } = new List<ThumbnailItem>();
  41. private List<int> cachePageList = new List<int>();
  42. private delegate void OnThumbnailGeneratedEventHandler(int pageIndex, byte[] thumb, int w, int h);
  43. private OnThumbnailGeneratedEventHandler OnThumbnailGenerated;
  44. public CPDFThumbnailControl()
  45. {
  46. InitializeComponent();
  47. Loaded += PdfThumbnail_Loaded;
  48. }
  49. /// <summary>
  50. /// Load completion event
  51. /// </summary>
  52. private void PdfThumbnail_Loaded(object sender, RoutedEventArgs e)
  53. {
  54. ThumbControl.ViewChanged -= ThumbControl_ViewChanged;
  55. ThumbControl.SelectionChanged -= ThumbControl_SelectionChanged;
  56. ThumbControl.ViewChanged += ThumbControl_ViewChanged;
  57. ThumbControl.SelectionChanged += ThumbControl_SelectionChanged;
  58. }
  59. /// <summary>
  60. /// The thumbnail list selects the change event
  61. /// </summary>
  62. private void ThumbControl_SelectionChanged(object sender, int e)
  63. {
  64. if(ViewControl!=null &&ViewControl.PDFViewTool!=null)
  65. {
  66. CPDFViewer pdfViewer=ViewControl.PDFViewTool.GetCPDFViewer();
  67. pdfViewer?.GoToPage(e, new Point(0, 0));
  68. }
  69. }
  70. /// <summary>
  71. /// Thumbnail content scrolling events
  72. /// </summary>
  73. private void ThumbControl_ViewChanged(object sender, ScrollChangedEventArgs e)
  74. {
  75. LoadVisibleThumbs();
  76. }
  77. /// <summary>
  78. /// Load thumbnails
  79. /// </summary>
  80. public void LoadThumb()
  81. {
  82. if (ViewControl == null || ViewControl.PDFViewTool == null || ThumbLoaded)
  83. {
  84. return;
  85. }
  86. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  87. CPDFDocument document= pdfViewer.GetDocument();
  88. if (document==null || document.IsLocked)
  89. {
  90. return;
  91. };
  92. pdfViewer.UpdateVirtualNodes();
  93. pdfViewer.UpdateRenderFrame();
  94. cachePageList.Clear();
  95. OnThumbnailGenerated -= ThumbnailGenerated;
  96. OnThumbnailGenerated += ThumbnailGenerated;
  97. pdfViewer.DrawChanged-= PdfView_DrawChanged;
  98. pdfViewer.DrawChanged += PdfView_DrawChanged;
  99. PopulateThumbnailList();
  100. LoadVisibleThumbs();
  101. }
  102. private void PdfView_DrawChanged(object sender, EventArgs e)
  103. {
  104. if (ViewControl != null && ViewControl.PDFViewTool != null)
  105. {
  106. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  107. if (pdfViewer != null && pdfViewer.CurrentRenderFrame != null)
  108. {
  109. SelectThumbItemWithoutGoTo(pdfViewer.CurrentRenderFrame.PageIndex);
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Set up PDFViewer
  115. /// </summary>
  116. public void InitWithPDFViewer(PDFViewControl viewControl)
  117. {
  118. ViewControl = viewControl;
  119. }
  120. /// <summary>
  121. /// Set the selected thumbnail
  122. /// </summary>
  123. public void SelectThumbItem(int newIndex)
  124. {
  125. ThumbControl?.SelectItem(newIndex);
  126. }
  127. public void SelectThumbItemWithoutGoTo(int newIndex)
  128. {
  129. ThumbControl?.SelectItemWithoutGoTo(newIndex);
  130. }
  131. private void PopulateThumbnailList()
  132. {
  133. int thumbnailWidth = thumbnailSize[zoomLevel];
  134. thumbnailItemList.Clear();
  135. if (ViewControl == null || ViewControl.PDFViewTool == null)
  136. {
  137. return;
  138. }
  139. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  140. CPDFDocument pdfdoc= pdfViewer?.GetDocument();
  141. if (pdfdoc == null)
  142. {
  143. return;
  144. }
  145. for (int i = 0; i < pdfdoc.PageCount; i++)
  146. {
  147. Size pageSize = DataConversionForWPF.CSizeConversionForSize(pdfdoc.GetPageSize(i));
  148. int imageWidth = 0;
  149. int imageHeight = 0;
  150. if(pageSize.Width>0 && pageSize.Height>0)
  151. {
  152. imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
  153. imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
  154. Image img = new Image()
  155. {
  156. Margin = new Thickness(0, 0, 5, 0),
  157. Width = imageWidth,
  158. Height = imageHeight,
  159. Stretch = Stretch.Uniform,
  160. };
  161. ThumbnailItem addItem = new ThumbnailItem();
  162. addItem.ImageHeight = imageHeight;
  163. addItem.ImageWidth = imageWidth;
  164. addItem.ThumbnailHeight = thumbnailWidth;
  165. addItem.ThumbnailWidth = thumbnailWidth;
  166. addItem.PageIndex = i;
  167. addItem.ImageData = img;
  168. thumbnailItemList.Add(addItem);
  169. }
  170. }
  171. ThumbControl.SetThumbResult(thumbnailItemList);
  172. }
  173. private async void LoadVisibleThumbs()
  174. {
  175. try
  176. {
  177. foreach (ThumbnailItem item in thumbnailItemList)
  178. {
  179. if (ThumbControl.IsItemVisible(item) == false)
  180. {
  181. continue;
  182. }
  183. if (item.ImageData == null || item.ImageData.Source == null)
  184. {
  185. if (cachePageList.Contains(item.PageIndex) == false)
  186. {
  187. cachePageList.Add(item.PageIndex);
  188. await GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
  189. }
  190. }
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. }
  196. }
  197. private void ThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  198. {
  199. try
  200. {
  201. if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
  202. {
  203. PixelFormat fmt = PixelFormats.Bgra32;
  204. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  205. ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
  206. thumbItem.ImageData.Source = bps;
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. }
  212. }
  213. private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
  214. {
  215. LoadVisibleThumbs();
  216. }
  217. private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
  218. {
  219. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  220. {
  221. e.Handled = true;
  222. if (e.Delta < 0)
  223. {
  224. zoomLevel = Math.Max(0, --zoomLevel);
  225. }
  226. else
  227. {
  228. zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
  229. }
  230. LoadThumb();
  231. }
  232. }
  233. public async Task GetThumbnail(int pageIndex, int imageWidth, int imageHeight)
  234. {
  235. if (imageWidth <= 0 || imageHeight <= 0)
  236. {
  237. return;
  238. }
  239. if (ViewControl == null || ViewControl.PDFViewTool == null)
  240. {
  241. return;
  242. }
  243. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  244. CPDFDocument pdfdoc = pdfViewer?.GetDocument();
  245. if (pdfdoc == null)
  246. {
  247. return;
  248. }
  249. CPDFPage page = pdfdoc.PageAtIndex(pageIndex);
  250. byte[] bmpData = new byte[imageWidth * imageHeight * 4];
  251. await Task.Run(() =>
  252. {
  253. bool isgetSignAp = false;
  254. if (PrintHelper.IsPageHaveSignAP(page))
  255. {
  256. double widthDpiRatio = imageWidth / page.PageSize.width;
  257. double heightDpiRatio = imageHeight / page.PageSize.height;
  258. System.Drawing.Bitmap bitmap = PrintHelper.GetPageBitmapWithFormDynamicAP(pdfdoc, page, (float)widthDpiRatio, (float)heightDpiRatio, new CRect(0, imageHeight, imageWidth, 0), 0xFFFFFFFF, bmpData, 1, true);
  259. if (bitmap != null)
  260. {
  261. System.Drawing.Imaging.BitmapData imageData = bitmap.LockBits(
  262. new System.Drawing.Rectangle(0, 0, imageWidth, imageHeight),
  263. System.Drawing.Imaging.ImageLockMode.ReadOnly,
  264. System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  265. Marshal.Copy(imageData.Scan0, bmpData, 0, bmpData.Length);
  266. bitmap.UnlockBits(imageData);
  267. isgetSignAp=true;
  268. }
  269. }
  270. if(!isgetSignAp)
  271. {
  272. page.RenderPageBitmap(0, 0, imageWidth, imageHeight, 0xFFFFFFFF, bmpData, 1, true);
  273. }
  274. });
  275. if (OnThumbnailGenerated != null)
  276. {
  277. OnThumbnailGenerated(pageIndex, bmpData, imageWidth, imageHeight);
  278. }
  279. }
  280. }
  281. }