PageTurningPreview.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 CPDFDocument document;
  27. public PageTurningPreview()
  28. {
  29. InitializeComponent();
  30. }
  31. public async Task RenderBitmap(CPDFDocument doc)
  32. {
  33. CPDFPage page = new CPDFPage(doc, int.Parse(this.CurrentPage.Text) - 1, true);
  34. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  35. await Task.Run(delegate
  36. {
  37. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  38. });
  39. PixelFormat fmt = PixelFormats.Bgra32;
  40. 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);
  41. this.Image.Source = bps;
  42. }
  43. public async void AwaitRenderBitmap(CPDFDocument doc)
  44. {
  45. await RenderBitmap(doc);
  46. }
  47. private void PrePage_Click(object sender, RoutedEventArgs e)
  48. {
  49. if (int.Parse(this.CurrentPage.Text) > 1)
  50. {
  51. int Page = int.Parse(this.CurrentPage.Text);
  52. this.CurrentPage.Text = (--Page).ToString();
  53. if (document != null)
  54. {
  55. AwaitRenderBitmap(document);
  56. }
  57. }
  58. }
  59. private void NextPage_Click(object sender, RoutedEventArgs e)
  60. {
  61. if (int.Parse(this.CurrentPage.Text) < document.PageCount)
  62. {
  63. int Page = int.Parse(this.CurrentPage.Text);
  64. this.CurrentPage.Text = (++Page).ToString();
  65. if (document != null)
  66. {
  67. AwaitRenderBitmap(document);
  68. }
  69. }
  70. }
  71. private async void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  72. {
  73. if (e.Key == Key.Enter)
  74. {
  75. if (this.CurrentPage.Text == "" || int.Parse(this.CurrentPage.Text) < 1)
  76. {
  77. this.CurrentPage.Text = "1";
  78. }
  79. if (this.PageIndex != null)
  80. {
  81. if (int.Parse(this.CurrentPage.Text) > int.Parse(this.PageIndex.Text))
  82. {
  83. this.CurrentPage.Text = this.PageIndex.Text;
  84. }
  85. }
  86. if (int.Parse(this.CurrentPage.Text) <= document.PageCount && int.Parse(this.CurrentPage.Text) > 0)
  87. {
  88. AwaitRenderBitmap(document);
  89. }
  90. else
  91. {
  92. MessageBox.Show("超出页面范围");
  93. }
  94. }
  95. }
  96. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  97. {
  98. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  99. }
  100. }
  101. }