CPdfThumbnailControl.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. pdfView.RenderCompleted -= PdfView_RenderCompleted;
  86. pdfView.RenderCompleted += PdfView_RenderCompleted;
  87. PopulateThumbnailList();
  88. LoadVisibleThumbs();
  89. SelectThumbItem(pdfView.CurrentIndex);
  90. }
  91. private void PdfView_RenderCompleted(object sender, KeyValuePair<string, object> e)
  92. {
  93. if (e.Key == "RenderNum")
  94. {
  95. SelectThumbItem(pdfView.CurrentIndex);
  96. }
  97. }
  98. /// <summary>
  99. /// 设置PDFViewer
  100. /// </summary>
  101. public void InitWithPDFViewer(CPDFViewer newPDFView)
  102. {
  103. pdfView = newPDFView;
  104. }
  105. /// <summary>
  106. /// 设置选中缩略图
  107. /// </summary>
  108. public void SelectThumbItem(int newIndex)
  109. {
  110. ThumbControl?.SelectItem(newIndex);
  111. }
  112. private void PopulateThumbnailList()
  113. {
  114. int thumbnailWidth = thumbnailSize[zoomLevel];
  115. thumbnailItemList.Clear();
  116. for (int i = 0; i < pdfView.Document.PageCount; i++)
  117. {
  118. Size pageSize = pdfView.Document.GetPageSize(i);
  119. int imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
  120. int imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
  121. Image img = new Image()
  122. {
  123. Margin = new Thickness(0, 0, 5, 0),
  124. Width = imageWidth,
  125. Height = imageHeight,
  126. Stretch = Stretch.Uniform,
  127. };
  128. ThumbnailItem addItem = new ThumbnailItem();
  129. addItem.ImageHeight = imageHeight;
  130. addItem.ImageWidth = imageWidth;
  131. addItem.ThumbnailHeight = thumbnailWidth;
  132. addItem.ThumbnailWidth = thumbnailWidth;
  133. addItem.PageIndex = i;
  134. addItem.ImageData = img;
  135. thumbnailItemList.Add(addItem);
  136. }
  137. ThumbControl.SetThumbResult(thumbnailItemList);
  138. }
  139. private async void LoadVisibleThumbs()
  140. {
  141. try
  142. {
  143. foreach (ThumbnailItem item in thumbnailItemList)
  144. {
  145. if (ThumbControl.IsItemVisible(item) == false)
  146. {
  147. continue;
  148. }
  149. if (item.ImageData == null || item.ImageData.Source == null)
  150. {
  151. if (cachePageList.Contains(item.PageIndex) == false)
  152. {
  153. cachePageList.Add(item.PageIndex);
  154. await pdfView.GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
  155. }
  156. }
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. }
  162. }
  163. private void OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  164. {
  165. try
  166. {
  167. if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
  168. {
  169. PixelFormat fmt = PixelFormats.Bgra32;
  170. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  171. ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
  172. thumbItem.ImageData.Source = bps;
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. }
  178. }
  179. private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
  180. {
  181. LoadVisibleThumbs();
  182. }
  183. private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
  184. {
  185. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  186. {
  187. e.Handled = true;
  188. if (e.Delta < 0)
  189. {
  190. zoomLevel = Math.Max(0, --zoomLevel);
  191. }
  192. else
  193. {
  194. zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
  195. }
  196. LoadThumb();
  197. }
  198. }
  199. }
  200. }