PageTurningPreview.xaml.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Xml.Linq;
  19. namespace PDF_Office.CustomControl
  20. {
  21. /// <summary>
  22. /// PageTurningPreview.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class PageTurningPreview : UserControl
  25. {
  26. public static CPDFDocument document;
  27. public PageTurningPreview()
  28. {
  29. InitializeComponent();
  30. if (document!=null) {
  31. AwaitRenderBitmap(document);
  32. this.PageIndex.Text = document.PageCount.ToString();
  33. }
  34. }
  35. public async Task RenderBitmap(CPDFDocument doc)
  36. {
  37. CPDFPage page = new CPDFPage(doc, int.Parse(this.CurrentPage.Text) - 1, true);
  38. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  39. await Task.Run(delegate
  40. {
  41. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  42. });
  43. PixelFormat fmt = PixelFormats.Bgra32;
  44. 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);
  45. this.Image.Source = bps;
  46. }
  47. public async void AwaitRenderBitmap(CPDFDocument doc)
  48. {
  49. await RenderBitmap(doc);
  50. }
  51. private void PrePage_Click(object sender, RoutedEventArgs e)
  52. {
  53. if (int.Parse(this.CurrentPage.Text) > 1)
  54. {
  55. int Page = int.Parse(this.CurrentPage.Text);
  56. this.CurrentPage.Text = (--Page).ToString();
  57. if (document != null)
  58. {
  59. AwaitRenderBitmap(document);
  60. }
  61. }
  62. }
  63. private void NextPage_Click(object sender, RoutedEventArgs e)
  64. {
  65. if (int.Parse(this.CurrentPage.Text) < document.PageCount)
  66. {
  67. int Page = int.Parse(this.CurrentPage.Text);
  68. this.CurrentPage.Text = (++Page).ToString();
  69. if (document != null)
  70. {
  71. AwaitRenderBitmap(document);
  72. }
  73. }
  74. }
  75. private async void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  76. {
  77. if (e.Key == Key.Enter)
  78. {
  79. if (int.Parse(this.CurrentPage.Text) <= document.PageCount && int.Parse(this.CurrentPage.Text) > 0)
  80. {
  81. AwaitRenderBitmap(document);
  82. }
  83. else
  84. {
  85. MessageBox.Show("超出页面范围");
  86. }
  87. }
  88. }
  89. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  90. {
  91. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  92. }
  93. }
  94. }