PageTurningPreview.xaml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  50. {
  51. this.CurrentPage.Text = "1";
  52. }
  53. if (int.Parse(this.CurrentPage.Text) > 1 && int.Parse(this.CurrentPage.Text) <= document.PageCount)
  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. else if (int.Parse(this.CurrentPage.Text) <= 1) { this.CurrentPage.Text = "1"; }
  63. else { this.CurrentPage.Text = document.PageCount.ToString(); }
  64. NextPage.IsEnabled = true;
  65. if (this.CurrentPage.Text == "1")
  66. {
  67. PrePage.IsEnabled = false;
  68. }
  69. }
  70. private void NextPage_Click(object sender, RoutedEventArgs e)
  71. {
  72. if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text,out int _))
  73. {
  74. this.CurrentPage.Text = "1";
  75. }
  76. if (int.Parse(this.CurrentPage.Text) < document.PageCount)
  77. {
  78. int Page = int.Parse(this.CurrentPage.Text);
  79. this.CurrentPage.Text = (++Page).ToString();
  80. if (document != null)
  81. {
  82. AwaitRenderBitmap(document);
  83. }
  84. }
  85. PrePage.IsEnabled= true;
  86. if (this.CurrentPage.Text == document.PageCount.ToString())
  87. {
  88. NextPage.IsEnabled = false;
  89. }
  90. }
  91. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  92. {
  93. if (this.CurrentPage.Text == ""|| !int.TryParse(this.CurrentPage.Text, out int _))
  94. {
  95. this.CurrentPage.Text = "1";
  96. }
  97. if (e.Key == Key.Enter)
  98. {
  99. if (this.CurrentPage.Text == "" || int.Parse(this.CurrentPage.Text) < 1)
  100. {
  101. this.CurrentPage.Text = "1";
  102. }
  103. if (this.PageIndex != null)
  104. {
  105. if (int.Parse(this.CurrentPage.Text) > int.Parse(this.PageIndex.Text))
  106. {
  107. this.CurrentPage.Text = this.PageIndex.Text;
  108. }
  109. }
  110. if (int.Parse(this.CurrentPage.Text) <= document.PageCount && int.Parse(this.CurrentPage.Text) > 0)
  111. {
  112. AwaitRenderBitmap(document);
  113. }
  114. else
  115. {
  116. MessageBox.Show("超出页面范围");
  117. }
  118. }
  119. }
  120. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  121. {
  122. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  123. }
  124. }
  125. }