CPdfThumbnailControl.xaml.cs 9.5 KB

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