123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- 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
- {
- /// <summary>
- /// Interaction logic for PreviewControl.xaml
- /// </summary>
- public partial class PreviewControl : UserControl, INotifyPropertyChanged
- {
- private List<int> _pageIndexList = new List<int>();
- public List<int> 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<int> 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<T>(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++;
- }
- }
- }
- }
- }
|