CPdfThumbnailControl.xaml.cs 9.2 KB

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