using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using System; using System.Collections.Generic; using System.ComponentModel; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; 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; using System.Xml.Linq; namespace Compdfkit_Tools.PDFControl { /// /// Interaction logic for PreviewControl.xaml /// public partial class PreviewControl : UserControl, INotifyPropertyChanged { private List _pageIndexList = new List(); public List PageRangeList { get => _pageIndexList; set { _pageIndexList = value; PageCount = _pageIndexList.Count; } } private int _pageCount = 1; public int PageCount { get => _pageCount; set { UpdateProper(ref _pageCount, value); } } private int _currentIndex = 1; public int CurrentIndex { get => _currentIndex; set { if (value < 1) { value = 1; } else if (value > PageCount) { value = PageCount; } if (UpdateProper(ref _currentIndex, value)) { ImageSource = LoadImage(Document.PageAtIndex(_currentIndex - 1)); } } } private double _scale = 1.0; public double Scale { get => _scale; set { UpdateProper(ref _scale, Math.Min((Math.Max(value, 0.1)), 10)); } } private WriteableBitmap _imageSource; public WriteableBitmap ImageSource { get => _imageSource; set { UpdateProper(ref _imageSource, value); } } private CPDFDocument _document; public CPDFDocument Document { get { return _document; } set { _document = value; } } private double aspectRatio; private double thumbnailWidth; private double thumbnailHeight; public PreviewControl() { InitializeComponent(); DataContext = this; } public void InitPreview(CPDFDocument document, List pageRangeList) { Document = document; this.PageRangeList = pageRangeList; } private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { } private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { } private void Image_MouseMove(object sender, MouseEventArgs e) { } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected bool UpdateProper(ref T properValue, T newValue, [CallerMemberName] string properName = "") { if (object.Equals(properValue, newValue)) return false; properValue = newValue; OnPropertyChanged(properName); return true; } private void CurrentIndexTxt_PreviewTextInput(object sender, TextCompositionEventArgs e) { TextBox textBox = sender as TextBox; if (textBox != null) { if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, @"^[0-9]$")) { e.Handled = true; } } } public async Task LoadImageAsync(CPDFPage pdfPage) { ImageSource = null; WriteableBitmap bitmap = await Task.Run(() => LoadImage(pdfPage)); Application.Current.Dispatcher.Invoke(new Action(() => { ImageSource = bitmap; })); } private WriteableBitmap LoadImage(CPDFPage pdfPage) { Size pageSize = pdfPage.PageSize; double ratio = CalculateThumbnailSize(pageSize); Rect pageRect = new Rect(0, 0, (int)(pageSize.Width / 72.0 * 96 * ratio), (int)(pageSize.Height / 72.0 * 96 * ratio)); byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * (96 / 72.0) * (96 / 72.0) * 4)]; pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0 * ratio), pageRect, 0xFFFFFFFF, bmpData, 0, true); WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.Width, (int)pageRect.Height, 96, 96, PixelFormats.Bgra32, null); writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.Width, (int)pageRect.Height), bmpData, writeableBitmap.BackBufferStride, 0); writeableBitmap.Freeze(); return writeableBitmap; } private double CalculateThumbnailSize(Size size) { if (size.Height / size.Width > aspectRatio) { return thumbnailWidth / size.Width; } else { return thumbnailHeight / size.Height; } } private async void UserControl_Loaded(object sender, RoutedEventArgs e) { aspectRatio = ImageGd.ActualHeight / ImageGd.ActualWidth; thumbnailWidth = ImageGd.ActualWidth; thumbnailHeight = ImageGd.ActualHeight; await LoadImageAsync(Document.PageAtIndex(0)); } private void CurrentIndexTxt_CanExecute(object sender, CanExecuteRoutedEventArgs e) { try { if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText()) { string checkText = Clipboard.GetText(); if (int.TryParse(checkText, out int value)) { e.CanExecute = true; } e.Handled = true; } } catch (Exception ex) { } } private void PageBtn_Click(object sender, RoutedEventArgs e) { var button = sender as Button; if (button != null) { if (button.Name == "PrePageBtn") { CurrentIndex--; } else { CurrentIndex++; } } } } }