123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- 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
- {
- /// <summary>
- /// PageTurningPreview.xaml 的交互逻辑
- /// </summary>
- public partial class PageTurningPreview : UserControl
- {
- public CPDFDocument document;
- public List<int> PageIndexLists = new List<int>();
- 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();
-
- }
- /// <summary>
- /// 预览是否显示Form表单
- /// </summary>
- 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));
- /// <summary>
- /// 预览是否显示Annot
- /// </summary>
- 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();
- }
- }
- }
- }
- }
|