CPdfThumbnailControl.xaml.cs 7.1 KB

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