PageTurningPreview.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. private int currentIndex = 0;
  31. public int CurrentIndex
  32. {
  33. get { return currentIndex; }
  34. set
  35. {
  36. currentIndex = value;
  37. //合理的使用set方法来进行一些操作,可以帮助理清很多思路,且极大减少代码量
  38. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  39. if (document != null)
  40. {
  41. AwaitRenderBitmap(document);
  42. }
  43. }
  44. }
  45. public PageTurningPreview()
  46. {
  47. InitializeComponent();
  48. }
  49. public async Task RenderBitmap(CPDFDocument doc)
  50. {
  51. CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]);
  52. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  53. await Task.Run(delegate
  54. {
  55. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  56. });
  57. PixelFormat fmt = PixelFormats.Bgra32;
  58. 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);
  59. this.Image.Source = bps;
  60. }
  61. public async void AwaitRenderBitmap(CPDFDocument doc)
  62. {
  63. await RenderBitmap(doc);
  64. }
  65. private void PrePage_Click(object sender, RoutedEventArgs e)
  66. {
  67. if (CurrentIndex > 0)
  68. {
  69. CurrentIndex--;
  70. }
  71. }
  72. private void NextPage_Click(object sender, RoutedEventArgs e)
  73. {
  74. if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0)
  75. {
  76. CurrentIndex++;
  77. }
  78. }
  79. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  80. {
  81. if (e.Key == Key.Enter)
  82. {
  83. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  84. {
  85. this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  86. }
  87. if (this.PageIndex != null)
  88. {
  89. if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1))
  90. {
  91. CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1);
  92. this.CurrentPage.Text = (PageIndexLists[CurrentIndex]+1).ToString();
  93. AwaitRenderBitmap(document);
  94. }
  95. else
  96. {
  97. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  98. MessageBox.Show("超出页面范围");
  99. }
  100. }
  101. }
  102. }
  103. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  104. {
  105. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  106. }
  107. private void CurrentPage_LostFocus(object sender, RoutedEventArgs e)
  108. {
  109. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  110. {
  111. this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  112. }
  113. if (this.PageIndex != null)
  114. {
  115. if (PageIndexLists.Contains(int.Parse(this.CurrentPage.Text) - 1))
  116. {
  117. CurrentIndex = PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text) - 1);
  118. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  119. AwaitRenderBitmap(document);
  120. }
  121. else
  122. {
  123. this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  124. }
  125. }
  126. }
  127. }
  128. }