CPdfThumbnailControl.xaml.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using compdfkit_tools.PDFControlUI;
  2. using ComPDFKitViewer.PdfViewer;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace compdfkit_tools.PDFControl
  18. {
  19. public partial class CPDFThumbnailControl : UserControl
  20. {
  21. /// <summary>
  22. /// PDFViewer
  23. /// </summary>
  24. private CPDFViewer pdfView;
  25. /// <summary>
  26. /// 缩略图是否加载过
  27. /// </summary>
  28. public bool ThumbLoaded { get; set; }
  29. /// <summary>
  30. /// 缩放比例集合
  31. /// </summary>
  32. private int[] thumbnailSize = { 50, 100, 150, 200, 300, 500 };
  33. /// <summary>
  34. /// 缩放系数
  35. /// </summary>
  36. private int zoomLevel = 2;
  37. /// <summary>
  38. /// 缩略图数据列表
  39. /// </summary>
  40. private List<ThumbnailItem> thumbnailItemList { get; set; } = new List<ThumbnailItem>();
  41. private List<int> cachePageList = new List<int>();
  42. public CPDFThumbnailControl()
  43. {
  44. InitializeComponent();
  45. Loaded += PdfThumbnail_Loaded;
  46. }
  47. /// <summary>
  48. /// 加载完成事件
  49. /// </summary>
  50. private void PdfThumbnail_Loaded(object sender, RoutedEventArgs e)
  51. {
  52. ThumbControl.ViewChanged += ThumbControl_ViewChanged;
  53. ThumbControl.SelectionChanged += ThumbControl_SelectionChanged;
  54. }
  55. /// <summary>
  56. /// 缩略图列表选中更改事件
  57. /// </summary>
  58. private void ThumbControl_SelectionChanged(object sender, int e)
  59. {
  60. pdfView?.GoToPage(e);
  61. }
  62. /// <summary>
  63. /// 缩略图内容滚动事件
  64. /// </summary>
  65. private void ThumbControl_ViewChanged(object sender, ScrollChangedEventArgs e)
  66. {
  67. LoadVisibleThumbs();
  68. }
  69. /// <summary>
  70. /// 加载缩略图
  71. /// </summary>
  72. public void LoadThumb()
  73. {
  74. if (pdfView == null || pdfView.Document == null || ThumbLoaded)
  75. {
  76. return;
  77. }
  78. if (pdfView.Document.IsLocked)
  79. {
  80. return;
  81. }
  82. cachePageList.Clear();
  83. pdfView.OnThumbnailGenerated -= OnThumbnailGenerated;
  84. pdfView.OnThumbnailGenerated += OnThumbnailGenerated;
  85. PopulateThumbnailList();
  86. LoadVisibleThumbs();
  87. }
  88. /// <summary>
  89. /// 设置PDFViewer
  90. /// </summary>
  91. public void InitWithPDFViewer(CPDFViewer newPDFView)
  92. {
  93. pdfView = newPDFView;
  94. }
  95. private void PopulateThumbnailList()
  96. {
  97. int thumbnailWidth = thumbnailSize[zoomLevel];
  98. thumbnailItemList.Clear();
  99. for (int i = 0; i < pdfView.Document.PageCount; i++)
  100. {
  101. Size pageSize = pdfView.Document.GetPageSize(i);
  102. int imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
  103. int imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
  104. Image img = new Image()
  105. {
  106. Margin = new Thickness(0, 0, 5, 0),
  107. Width = imageWidth,
  108. Height = imageHeight,
  109. Stretch = Stretch.Uniform,
  110. };
  111. ThumbnailItem addItem = new ThumbnailItem();
  112. addItem.ImageHeight = imageHeight;
  113. addItem.ImageWidth = imageWidth;
  114. addItem.ThumbnailHeight = thumbnailWidth;
  115. addItem.ThumbnailWidth = thumbnailWidth;
  116. addItem.PageIndex = i;
  117. addItem.ImageData = img;
  118. thumbnailItemList.Add(addItem);
  119. }
  120. ThumbControl.SetThumbResult(thumbnailItemList);
  121. }
  122. private async void LoadVisibleThumbs()
  123. {
  124. try
  125. {
  126. foreach (ThumbnailItem item in thumbnailItemList)
  127. {
  128. if (ThumbControl.IsItemVisible(item) == false)
  129. {
  130. continue;
  131. }
  132. if (item.ImageData == null || item.ImageData.Source == null)
  133. {
  134. if (cachePageList.Contains(item.PageIndex) == false)
  135. {
  136. cachePageList.Add(item.PageIndex);
  137. await pdfView.GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
  138. }
  139. }
  140. }
  141. }
  142. catch (Exception ex)
  143. {
  144. }
  145. }
  146. private void OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  147. {
  148. try
  149. {
  150. if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
  151. {
  152. PixelFormat fmt = PixelFormats.Bgra32;
  153. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  154. ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
  155. thumbItem.ImageData.Source = bps;
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. }
  161. }
  162. private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
  163. {
  164. LoadVisibleThumbs();
  165. }
  166. private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
  167. {
  168. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  169. {
  170. e.Handled = true;
  171. if (e.Delta < 0)
  172. {
  173. zoomLevel = Math.Max(0, --zoomLevel);
  174. }
  175. else
  176. {
  177. zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
  178. }
  179. LoadThumb();
  180. }
  181. }
  182. }
  183. }