CPdfThumbnailControl.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. }
  54. /// <summary>
  55. /// The thumbnail list selects the change event
  56. /// </summary>
  57. private void ThumbControl_SelectionChanged(object sender, int e)
  58. {
  59. if(ViewControl!=null &&ViewControl.PDFViewTool!=null)
  60. {
  61. CPDFViewer pdfViewer=ViewControl.PDFViewTool.GetCPDFViewer();
  62. pdfViewer?.GoToPage(e, new Point(0, 0));
  63. }
  64. }
  65. /// <summary>
  66. /// Thumbnail content scrolling events
  67. /// </summary>
  68. private void ThumbControl_ViewChanged(object sender, ScrollChangedEventArgs e)
  69. {
  70. LoadVisibleThumbs();
  71. }
  72. /// <summary>
  73. /// Load thumbnails
  74. /// </summary>
  75. public void LoadThumb()
  76. {
  77. if (ViewControl == null || ViewControl.PDFViewTool == null || ThumbLoaded)
  78. {
  79. return;
  80. }
  81. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  82. CPDFDocument document= pdfViewer.GetDocument();
  83. if (document==null || document.IsLocked)
  84. {
  85. return;
  86. };
  87. pdfViewer.UpdataVirtualNodes();
  88. cachePageList.Clear();
  89. OnThumbnailGenerated -= ThumbnailGenerated;
  90. OnThumbnailGenerated += ThumbnailGenerated;
  91. pdfViewer.DrawChanged-= PdfView_DrawChanged;
  92. pdfViewer.DrawChanged += PdfView_DrawChanged;
  93. PopulateThumbnailList();
  94. LoadVisibleThumbs();
  95. }
  96. private void PdfView_DrawChanged(object sender, EventArgs e)
  97. {
  98. if (ViewControl != null && ViewControl.PDFViewTool != null)
  99. {
  100. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  101. if (pdfViewer != null && pdfViewer.CurrentRenderFrame != null)
  102. {
  103. SelectThumbItemWithoutGoTo(pdfViewer.CurrentRenderFrame.PageIndex);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Set up PDFViewer
  109. /// </summary>
  110. public void InitWithPDFViewer(PDFViewControl viewControl)
  111. {
  112. ViewControl = viewControl;
  113. }
  114. /// <summary>
  115. /// Set the selected thumbnail
  116. /// </summary>
  117. public void SelectThumbItem(int newIndex)
  118. {
  119. ThumbControl?.SelectItem(newIndex);
  120. }
  121. public void SelectThumbItemWithoutGoTo(int newIndex)
  122. {
  123. ThumbControl?.SelectItemWithoutGoTo(newIndex);
  124. }
  125. private void PopulateThumbnailList()
  126. {
  127. int thumbnailWidth = thumbnailSize[zoomLevel];
  128. thumbnailItemList.Clear();
  129. if (ViewControl == null || ViewControl.PDFViewTool == null)
  130. {
  131. return;
  132. }
  133. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  134. CPDFDocument pdfdoc= pdfViewer?.GetDocument();
  135. if (pdfdoc == null)
  136. {
  137. return;
  138. }
  139. for (int i = 0; i < pdfdoc.PageCount; i++)
  140. {
  141. Size pageSize = DataConversionForWPF.CSizeConversionForSize(pdfdoc.GetPageSize(i));
  142. int imageWidth = 0;
  143. int imageHeight = 0;
  144. if(pageSize.Width>0 && pageSize.Height>0)
  145. {
  146. imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
  147. imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
  148. Image img = new Image()
  149. {
  150. Margin = new Thickness(0, 0, 5, 0),
  151. Width = imageWidth,
  152. Height = imageHeight,
  153. Stretch = Stretch.Uniform,
  154. };
  155. ThumbnailItem addItem = new ThumbnailItem();
  156. addItem.ImageHeight = imageHeight;
  157. addItem.ImageWidth = imageWidth;
  158. addItem.ThumbnailHeight = thumbnailWidth;
  159. addItem.ThumbnailWidth = thumbnailWidth;
  160. addItem.PageIndex = i;
  161. addItem.ImageData = img;
  162. thumbnailItemList.Add(addItem);
  163. }
  164. }
  165. ThumbControl.SetThumbResult(thumbnailItemList);
  166. }
  167. private async void LoadVisibleThumbs()
  168. {
  169. try
  170. {
  171. foreach (ThumbnailItem item in thumbnailItemList)
  172. {
  173. if (ThumbControl.IsItemVisible(item) == false)
  174. {
  175. continue;
  176. }
  177. if (item.ImageData == null || item.ImageData.Source == null)
  178. {
  179. if (cachePageList.Contains(item.PageIndex) == false)
  180. {
  181. cachePageList.Add(item.PageIndex);
  182. await GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
  183. }
  184. }
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. }
  190. }
  191. private void ThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
  192. {
  193. try
  194. {
  195. if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
  196. {
  197. PixelFormat fmt = PixelFormats.Bgra32;
  198. BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
  199. ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
  200. thumbItem.ImageData.Source = bps;
  201. }
  202. }
  203. catch (Exception ex)
  204. {
  205. }
  206. }
  207. private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
  208. {
  209. LoadVisibleThumbs();
  210. }
  211. private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
  212. {
  213. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  214. {
  215. e.Handled = true;
  216. if (e.Delta < 0)
  217. {
  218. zoomLevel = Math.Max(0, --zoomLevel);
  219. }
  220. else
  221. {
  222. zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
  223. }
  224. LoadThumb();
  225. }
  226. }
  227. public async Task GetThumbnail(int pageIndex, int imageWidth, int imageHeight)
  228. {
  229. if (imageWidth <= 0 || imageHeight <= 0)
  230. {
  231. return;
  232. }
  233. if (ViewControl == null || ViewControl.PDFViewTool == null)
  234. {
  235. return;
  236. }
  237. CPDFViewer pdfViewer = ViewControl.PDFViewTool.GetCPDFViewer();
  238. CPDFDocument pdfdoc = pdfViewer?.GetDocument();
  239. if (pdfdoc == null)
  240. {
  241. return;
  242. }
  243. CPDFPage page = pdfdoc.PageAtIndex(pageIndex);
  244. byte[] bmpData = new byte[imageWidth * imageHeight * 4];
  245. await Task.Run(() => page.RenderPageBitmap(0, 0, imageWidth, imageHeight, 0xFFFFFFFF, bmpData, 1, true));
  246. if (OnThumbnailGenerated != null)
  247. {
  248. OnThumbnailGenerated(pageIndex, bmpData, imageWidth, imageHeight);
  249. }
  250. }
  251. }
  252. }