CPdfThumbnailControl.xaml.cs 9.4 KB

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