PageTurningPreview.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using PDF_Master.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_Master.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. //显示Doc页码
  39. //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  40. //显示位置
  41. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  42. if (PageIndexLists != null)
  43. {
  44. if (CurrentIndex == PageIndexLists.Count-1) { NextPage.IsEnabled = false; } else { NextPage.IsEnabled = true; }
  45. if (CurrentIndex == 0) { PrePage.IsEnabled = false; } else { PrePage.IsEnabled = true; }
  46. AwaitRenderBitmap(document);
  47. }
  48. }
  49. }
  50. public PageTurningPreview()
  51. {
  52. InitializeComponent();
  53. }
  54. public async Task RenderBitmap(CPDFDocument doc)
  55. {
  56. CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]);
  57. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  58. await Task.Run(delegate
  59. {
  60. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  61. });
  62. PixelFormat fmt = PixelFormats.Bgra32;
  63. 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);
  64. this.Image.Source = bps;
  65. }
  66. public async void AwaitRenderBitmap(CPDFDocument doc)
  67. {
  68. await RenderBitmap(doc);
  69. }
  70. private void PrePage_Click(object sender, RoutedEventArgs e)
  71. {
  72. if (CurrentIndex > 0)
  73. {
  74. CurrentIndex--;
  75. }
  76. }
  77. private void NextPage_Click(object sender, RoutedEventArgs e)
  78. {
  79. if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0)
  80. {
  81. CurrentIndex++;
  82. }
  83. }
  84. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  85. {
  86. if (e.Key == Key.Enter)
  87. {
  88. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  89. {
  90. // this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  91. this.CurrentPage.Text = "1";
  92. }
  93. if (this.PageIndex != null)
  94. {
  95. //if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1))
  96. //{
  97. // CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1);
  98. // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  99. // AwaitRenderBitmap(document);
  100. //}
  101. //else
  102. //{
  103. // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  104. // MessageBox.Show("超出页面范围");
  105. //}
  106. if (int.Parse(this.CurrentPage.Text)<=PageIndexLists.Count&&int.Parse(this.CurrentPage.Text) > 0)
  107. {
  108. CurrentIndex =int.Parse(this.CurrentPage.Text) - 1;
  109. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  110. AwaitRenderBitmap(document);
  111. }
  112. else
  113. {
  114. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  115. //MessageBox.Show("超出页面范围");
  116. }
  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. private void CurrentPage_LostFocus(object sender, RoutedEventArgs e)
  125. {
  126. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  127. {
  128. this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  129. }
  130. if (this.PageIndex != null)
  131. {
  132. if (PageIndexLists.Count>(int.Parse(this.CurrentPage.Text) - 1))
  133. {
  134. CurrentIndex = int.Parse(this.CurrentPage.Text) - 1;
  135. //显示Doc页码
  136. //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  137. //显示位置
  138. this.CurrentPage.Text = (CurrentIndex +1).ToString();
  139. AwaitRenderBitmap(document);
  140. }
  141. else
  142. {
  143. //显示Doc页码
  144. //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  145. //显示位置
  146. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  147. }
  148. }
  149. }
  150. }
  151. }