CPdfThumbnailControl.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using Compdfkit_Tools.PDFControlUI;
  2. using ComPDFKitViewer.PdfViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. namespace Compdfkit_Tools.PDFControl
  11. {
  12. public partial class CPDFThumbnailControl : UserControl
  13. {
  14. /// <summary>
  15. /// PDFViewer
  16. /// </summary>
  17. private CPDFViewer pdfView;
  18. /// <summary>
  19. /// Whether the thumbnail has been loaded
  20. /// </summary>
  21. public bool ThumbLoaded { get; set; }
  22. /// <summary>
  23. /// A collection of scale factors
  24. /// </summary>
  25. private int[] thumbnailSize = { 50, 100, 150, 200, 300, 500 };
  26. /// <summary>
  27. /// Scale factor
  28. /// </summary>
  29. private int zoomLevel = 2;
  30. /// <summary>
  31. /// A list of thumbnail data
  32. /// </summary>
  33. private List<ThumbnailItem> thumbnailItemList { get; set; } = new List<ThumbnailItem>();
  34. private List<int> cachePageList = new List<int>();
  35. public CPDFThumbnailControl()
  36. {
  37. InitializeComponent();
  38. Loaded += PdfThumbnail_Loaded;
  39. }
  40. /// <summary>
  41. /// Load completion event
  42. /// </summary>
  43. private void PdfThumbnail_Loaded(object sender, RoutedEventArgs e)
  44. {
  45. ThumbControl.ViewChanged += ThumbControl_ViewChanged;
  46. ThumbControl.SelectionChanged += ThumbControl_SelectionChanged;
  47. }
  48. /// <summary>
  49. /// The thumbnail list selects the change event
  50. /// </summary>
  51. private void ThumbControl_SelectionChanged(object sender, int e)
  52. {
  53. pdfView?.GoToPage(e);
  54. }
  55. /// <summary>
  56. /// Thumbnail content scrolling events
  57. /// </summary>
  58. private void ThumbControl_ViewChanged(object sender, ScrollChangedEventArgs e)
  59. {
  60. LoadVisibleThumbs();
  61. }
  62. /// <summary>
  63. /// Load thumbnails
  64. /// </summary>
  65. public void LoadThumb()
  66. {
  67. if (pdfView == null || pdfView.Document == null || ThumbLoaded)
  68. {
  69. return;
  70. }
  71. if (pdfView.Document.IsLocked)
  72. {
  73. return;
  74. }
  75. cachePageList.Clear();
  76. pdfView.OnThumbnailGenerated -= OnThumbnailGenerated;
  77. pdfView.OnThumbnailGenerated += OnThumbnailGenerated;
  78. pdfView.RenderCompleted -= PdfView_RenderCompleted;
  79. pdfView.RenderCompleted += PdfView_RenderCompleted;
  80. PopulateThumbnailList();
  81. LoadVisibleThumbs();
  82. SelectThumbItem(pdfView.CurrentIndex);
  83. }
  84. private void PdfView_RenderCompleted(object sender, KeyValuePair<string, object> e)
  85. {
  86. if (e.Key == "RenderNum")
  87. {
  88. SelectThumbItem(pdfView.CurrentIndex);
  89. }
  90. }
  91. /// <summary>
  92. /// Set up PDFViewer
  93. /// </summary>
  94. public void InitWithPDFViewer(CPDFViewer newPDFView)
  95. {
  96. pdfView = newPDFView;
  97. }
  98. /// <summary>
  99. /// Set the selected thumbnail
  100. /// </summary>
  101. public void SelectThumbItem(int newIndex)
  102. {
  103. ThumbControl?.SelectItem(newIndex);
  104. }
  105. private void PopulateThumbnailList()
  106. {
  107. int thumbnailWidth = thumbnailSize[zoomLevel];
  108. thumbnailItemList.Clear();
  109. for (int i = 0; i < pdfView.Document.PageCount; i++)
  110. {
  111. Size pageSize = pdfView.Document.GetPageSize(i);
  112. int imageWidth = 0;
  113. int imageHeight = 0;
  114. if(pageSize.Width>0 && pageSize.Height>0)
  115. {
  116. imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
  117. imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
  118. Image img = new Image()
  119. {
  120. Margin = new Thickness(0, 0, 5, 0),
  121. Width = imageWidth,
  122. Height = imageHeight,
  123. Stretch = Stretch.Uniform,
  124. };
  125. ThumbnailItem addItem = new ThumbnailItem();
  126. addItem.ImageHeight = imageHeight;
  127. addItem.ImageWidth = imageWidth;
  128. addItem.ThumbnailHeight = thumbnailWidth;
  129. addItem.ThumbnailWidth = thumbnailWidth;
  130. addItem.PageIndex = i;
  131. addItem.ImageData = img;
  132. thumbnailItemList.Add(addItem);
  133. }
  134. }
  135. ThumbControl.SetThumbResult(thumbnailItemList);
  136. }
  137. private async void LoadVisibleThumbs()
  138. {
  139. try
  140. {
  141. foreach (ThumbnailItem item in thumbnailItemList)
  142. {
  143. if (ThumbControl.IsItemVisible(item) == false)
  144. {
  145. continue;
  146. }
  147. if (item.ImageData == null || item.ImageData.Source == null)
  148. {
  149. if (cachePageList.Contains(item.PageIndex) == false)
  150. {
  151. cachePageList.Add(item.PageIndex);
  152. await pdfView.GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
  153. }
  154. }
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. }
  160. }
  161. private void OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  162. {
  163. try
  164. {
  165. if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
  166. {
  167. PixelFormat fmt = PixelFormats.Bgra32;
  168. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  169. ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
  170. thumbItem.ImageData.Source = bps;
  171. }
  172. }
  173. catch (Exception ex)
  174. {
  175. }
  176. }
  177. private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
  178. {
  179. LoadVisibleThumbs();
  180. }
  181. private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
  182. {
  183. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  184. {
  185. e.Handled = true;
  186. if (e.Delta < 0)
  187. {
  188. zoomLevel = Math.Max(0, --zoomLevel);
  189. }
  190. else
  191. {
  192. zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
  193. }
  194. LoadThumb();
  195. }
  196. }
  197. }
  198. }