123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- 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 PDF_Office.CustomControl
- {
- /// <summary>
- /// PageTurningPreview.xaml 的交互逻辑
- /// </summary>
- public partial class PageTurningPreview : UserControl
- {
- public CPDFDocument document;
- public PageTurningPreview()
- {
- InitializeComponent();
- }
- public async Task RenderBitmap(CPDFDocument doc)
- {
- CPDFPage page = new CPDFPage(doc, int.Parse(this.CurrentPage.Text) - 1, true);
- byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
- await Task.Run(delegate
- {
- page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
- });
- PixelFormat fmt = PixelFormats.Bgra32;
- BitmapSource bps = BitmapSource.Create((int)page.PageSize.Width, (int)page.PageSize.Height, 96.0, 96.0, fmt, null, bmp_data, ((int)page.PageSize.Width * fmt.BitsPerPixel + 7) / 8);
- this.Image.Source = bps;
- }
- public async void AwaitRenderBitmap(CPDFDocument doc)
- {
- await RenderBitmap(doc);
- }
- private void PrePage_Click(object sender, RoutedEventArgs e)
- {
- if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
- {
- this.CurrentPage.Text = "1";
- }
- if (int.Parse(this.CurrentPage.Text) > 1 && int.Parse(this.CurrentPage.Text) <= document.PageCount)
- {
- int Page = int.Parse(this.CurrentPage.Text);
- this.CurrentPage.Text = (--Page).ToString();
- if (document != null)
- {
- AwaitRenderBitmap(document);
- }
- }
- else if (int.Parse(this.CurrentPage.Text) <= 1) { this.CurrentPage.Text = "1"; }
- else { this.CurrentPage.Text = document.PageCount.ToString(); }
- NextPage.IsEnabled = true;
- if (this.CurrentPage.Text == "1")
- {
- PrePage.IsEnabled = false;
- }
- }
- private void NextPage_Click(object sender, RoutedEventArgs e)
- {
- if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text,out int _))
- {
- this.CurrentPage.Text = "1";
- }
- if (int.Parse(this.CurrentPage.Text) < document.PageCount)
- {
- int Page = int.Parse(this.CurrentPage.Text);
- this.CurrentPage.Text = (++Page).ToString();
- if (document != null)
- {
- AwaitRenderBitmap(document);
- }
- }
- PrePage.IsEnabled= true;
- if (this.CurrentPage.Text == document.PageCount.ToString())
- {
- NextPage.IsEnabled = false;
- }
- }
- private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
- {
- if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text, out int _))
- {
- this.CurrentPage.Text = "1";
- }
- if (e.Key == Key.Enter)
- {
- if (this.CurrentPage.Text == "" || int.Parse(this.CurrentPage.Text) < 1)
- {
- this.CurrentPage.Text = "1";
- }
- if (this.PageIndex != null)
- {
- if (int.Parse(this.CurrentPage.Text) > int.Parse(this.PageIndex.Text))
- {
- this.CurrentPage.Text = this.PageIndex.Text;
- }
- }
- if (int.Parse(this.CurrentPage.Text) <= document.PageCount && int.Parse(this.CurrentPage.Text) > 0)
- {
- AwaitRenderBitmap(document);
- }
- else
- {
- MessageBox.Show("超出页面范围");
- }
- }
- }
- private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
- }
- }
- }
|