PageTurningPreview.xaml.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using PDF_Office.Helper;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. using System.Windows.Documents;
  15. using System.Windows.Input;
  16. using System.Windows.Media;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using System.Xml.Linq;
  21. namespace PDF_Office.CustomControl
  22. {
  23. /// <summary>
  24. /// PageTurningPreview.xaml 的交互逻辑
  25. /// </summary>
  26. public partial class PageTurningPreview : UserControl
  27. {
  28. public CPDFDocument document;
  29. public List<int> PageIndexLists = new List<int>();
  30. public int CurrentIndex=0;
  31. public PageTurningPreview()
  32. {
  33. InitializeComponent();
  34. }
  35. public async Task RenderBitmap(CPDFDocument doc)
  36. {
  37. CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]);
  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 (CurrentIndex > 0)
  54. {
  55. CurrentIndex--;
  56. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  57. if (document != null)
  58. {
  59. AwaitRenderBitmap(document);
  60. }
  61. }
  62. }
  63. private void NextPage_Click(object sender, RoutedEventArgs e)
  64. {
  65. if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0)
  66. {
  67. CurrentIndex++;
  68. this.CurrentPage.Text = (PageIndexLists[CurrentIndex]+1).ToString();
  69. if (document != null)
  70. {
  71. AwaitRenderBitmap(document);
  72. }
  73. }
  74. }
  75. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  76. {
  77. if (e.Key == Key.Enter)
  78. {
  79. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  80. {
  81. this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  82. }
  83. if (this.PageIndex != null)
  84. {
  85. if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1))
  86. {
  87. CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1);
  88. this.CurrentPage.Text = (PageIndexLists[CurrentIndex]+1).ToString();
  89. AwaitRenderBitmap(document);
  90. }
  91. else
  92. {
  93. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  94. MessageBox.Show("超出页面范围");
  95. }
  96. }
  97. }
  98. }
  99. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  100. {
  101. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  102. }
  103. }
  104. }