PageTurningPreview.xaml.cs 6.0 KB

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