using ComPDFKit.PDFDocument; using ComPDFKit.PDFPage; using PDF_Master.Helper; using System; using System.Collections.Generic; using System.Diagnostics; 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_Master.CustomControl { /// /// PageTurningPreview.xaml 的交互逻辑 /// public partial class PageTurningPreview : UserControl { public CPDFDocument document; public List PageIndexLists = new List(); private int currentIndex = 0; public int CurrentIndex { get { return currentIndex; } set { currentIndex = value; //合理的使用set方法来进行一些操作,可以帮助理清很多思路,且极大减少代码量 //显示Doc页码 //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString(); //显示位置 this.CurrentPage.Text = (CurrentIndex + 1).ToString(); if (PageIndexLists != null) { if (CurrentIndex == PageIndexLists.Count-1) { NextPage.IsEnabled = false; } else { NextPage.IsEnabled = true; } if (CurrentIndex == 0) { PrePage.IsEnabled = false; } else { PrePage.IsEnabled = true; } AwaitRenderBitmap(document); } } } public PageTurningPreview() { InitializeComponent(); } /// /// 预览是否显示Form表单 /// public bool PageIsForm { get { return (bool)GetValue(PageIsFormProperty); } set { SetValue(PageIsFormProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty PageIsFormProperty = DependencyProperty.Register("PageIsForm", typeof(bool), typeof(PageTurningPreview), new PropertyMetadata(true)); /// /// 预览是否显示Annot /// public bool PageIsAnnot { get { return (bool)GetValue(PageIsAnnotProperty); } set { SetValue(PageIsAnnotProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty PageIsAnnotProperty = DependencyProperty.Register("PageIsAnnot", typeof(bool), typeof(PageTurningPreview), new PropertyMetadata(true)); public async Task RenderBitmap(CPDFDocument doc) { CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]); byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4]; bool IsAnnot = this.PageIsAnnot; bool IsForm=this.PageIsForm; await Task.Run(delegate { page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, IsAnnot ? 1:0, IsForm); }); 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 (CurrentIndex > 0) { CurrentIndex--; } } private void NextPage_Click(object sender, RoutedEventArgs e) { if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0) { CurrentIndex++; } } private void CurrentPage_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _)) { // this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString(); this.CurrentPage.Text = "1"; } if (this.PageIndex != null) { //if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1)) //{ // CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1); // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString(); // AwaitRenderBitmap(document); //} //else //{ // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString(); // MessageBox.Show("超出页面范围"); //} if (int.Parse(this.CurrentPage.Text)<=PageIndexLists.Count&&int.Parse(this.CurrentPage.Text) > 0) { CurrentIndex =int.Parse(this.CurrentPage.Text) - 1; this.CurrentPage.Text = (CurrentIndex + 1).ToString(); AwaitRenderBitmap(document); } else { this.CurrentPage.Text = (CurrentIndex + 1).ToString(); //MessageBox.Show("超出页面范围"); } } } } private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled = new Regex("[^0-9]+").IsMatch(e.Text); } private void CurrentPage_LostFocus(object sender, RoutedEventArgs e) { if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _)) { this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString(); } if (this.PageIndex != null) { if (PageIndexLists.Count>(int.Parse(this.CurrentPage.Text) - 1)) { CurrentIndex = int.Parse(this.CurrentPage.Text) - 1; //显示Doc页码 //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString(); //显示位置 this.CurrentPage.Text = (CurrentIndex +1).ToString(); AwaitRenderBitmap(document); } else { //显示Doc页码 //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString(); //显示位置 this.CurrentPage.Text = (CurrentIndex + 1).ToString(); } } } } }