using compdfkit_tools.PDFControlUI;
using ComPDFKitViewer.PdfViewer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace compdfkit_tools.PDFControl
{
public partial class CPDFThumbnailControl : UserControl
{
///
/// PDFViewer
///
private CPDFViewer pdfView;
///
/// 缩略图是否加载过
///
public bool ThumbLoaded { get; set; }
///
/// 缩放比例集合
///
private int[] thumbnailSize = { 50, 100, 150, 200, 300, 500 };
///
/// 缩放系数
///
private int zoomLevel = 2;
///
/// 缩略图数据列表
///
private List thumbnailItemList { get; set; } = new List();
private List cachePageList = new List();
public CPDFThumbnailControl()
{
InitializeComponent();
Loaded += PdfThumbnail_Loaded;
}
///
/// 加载完成事件
///
private void PdfThumbnail_Loaded(object sender, RoutedEventArgs e)
{
ThumbControl.ViewChanged += ThumbControl_ViewChanged;
ThumbControl.SelectionChanged += ThumbControl_SelectionChanged;
}
///
/// 缩略图列表选中更改事件
///
private void ThumbControl_SelectionChanged(object sender, int e)
{
pdfView?.GoToPage(e);
}
///
/// 缩略图内容滚动事件
///
private void ThumbControl_ViewChanged(object sender, ScrollChangedEventArgs e)
{
LoadVisibleThumbs();
}
///
/// 加载缩略图
///
public void LoadThumb()
{
if (pdfView == null || pdfView.Document == null || ThumbLoaded)
{
return;
}
if (pdfView.Document.IsLocked)
{
return;
}
cachePageList.Clear();
pdfView.OnThumbnailGenerated -= OnThumbnailGenerated;
pdfView.OnThumbnailGenerated += OnThumbnailGenerated;
PopulateThumbnailList();
LoadVisibleThumbs();
}
///
/// 设置PDFViewer
///
public void InitWithPDFViewer(CPDFViewer newPDFView)
{
pdfView = newPDFView;
}
private void PopulateThumbnailList()
{
int thumbnailWidth = thumbnailSize[zoomLevel];
thumbnailItemList.Clear();
for (int i = 0; i < pdfView.Document.PageCount; i++)
{
Size pageSize = pdfView.Document.GetPageSize(i);
int imageWidth = pageSize.Width > pageSize.Height ? thumbnailWidth * 2 : (int)(pageSize.Width / pageSize.Height * thumbnailWidth * 2);
int imageHeight = pageSize.Height > pageSize.Width ? thumbnailWidth * 2 : (int)(pageSize.Height / pageSize.Width * thumbnailWidth * 2);
Image img = new Image()
{
Margin = new Thickness(0, 0, 5, 0),
Width = imageWidth,
Height = imageHeight,
Stretch = Stretch.Uniform,
};
ThumbnailItem addItem = new ThumbnailItem();
addItem.ImageHeight = imageHeight;
addItem.ImageWidth = imageWidth;
addItem.ThumbnailHeight = thumbnailWidth;
addItem.ThumbnailWidth = thumbnailWidth;
addItem.PageIndex = i;
addItem.ImageData = img;
thumbnailItemList.Add(addItem);
}
ThumbControl.SetThumbResult(thumbnailItemList);
}
private async void LoadVisibleThumbs()
{
try
{
foreach (ThumbnailItem item in thumbnailItemList)
{
if (ThumbControl.IsItemVisible(item) == false)
{
continue;
}
if (item.ImageData == null || item.ImageData.Source == null)
{
if (cachePageList.Contains(item.PageIndex) == false)
{
cachePageList.Add(item.PageIndex);
await pdfView.GetThumbnail(item.PageIndex, item.ImageWidth, item.ImageHeight);
}
}
}
}
catch (Exception ex)
{
}
}
private void OnThumbnailGenerated(int pageIndex, byte[] thumb, int w, int h)
{
try
{
if (thumbnailItemList != null && thumbnailItemList.Count > pageIndex)
{
PixelFormat fmt = PixelFormats.Bgra32;
BitmapSource bps = BitmapSource.Create(w, h, 96.0, 96.0, fmt, null, thumb, (w * fmt.BitsPerPixel + 7) / 8);
ThumbnailItem thumbItem = thumbnailItemList[pageIndex];
thumbItem.ImageData.Source = bps;
}
}
catch (Exception ex)
{
}
}
private void ThumbControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
LoadVisibleThumbs();
}
private void ThumbControl_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{
e.Handled = true;
if (e.Delta < 0)
{
zoomLevel = Math.Max(0, --zoomLevel);
}
else
{
zoomLevel = Math.Min(thumbnailSize.Length - 1, ++zoomLevel);
}
LoadThumb();
}
}
}
}