PageTurningPreview.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. /// <summary>
  55. /// 预览是否显示Form表单
  56. /// </summary>
  57. public bool PageIsForm
  58. {
  59. get { return (bool)GetValue(PageIsFormProperty); }
  60. set { SetValue(PageIsFormProperty, value); }
  61. }
  62. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  63. public static readonly DependencyProperty PageIsFormProperty =
  64. DependencyProperty.Register("PageIsForm", typeof(bool), typeof(PageTurningPreview), new PropertyMetadata(true));
  65. /// <summary>
  66. /// 预览是否显示Annot
  67. /// </summary>
  68. public bool PageIsAnnot
  69. {
  70. get { return (bool)GetValue(PageIsAnnotProperty); }
  71. set { SetValue(PageIsAnnotProperty, value); }
  72. }
  73. // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
  74. public static readonly DependencyProperty PageIsAnnotProperty =
  75. DependencyProperty.Register("PageIsAnnot", typeof(bool), typeof(PageTurningPreview), new PropertyMetadata(true));
  76. public async Task RenderBitmap(CPDFDocument doc)
  77. {
  78. CPDFPage page = doc.PageAtIndex(PageIndexLists[CurrentIndex]);
  79. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  80. bool IsAnnot = this.PageIsAnnot;
  81. bool IsForm=this.PageIsForm;
  82. await Task.Run(delegate
  83. {
  84. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, IsAnnot ? 1:0, IsForm);
  85. });
  86. PixelFormat fmt = PixelFormats.Bgra32;
  87. 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);
  88. this.Image.Source = bps;
  89. }
  90. public async void AwaitRenderBitmap(CPDFDocument doc)
  91. {
  92. await RenderBitmap(doc);
  93. }
  94. private void PrePage_Click(object sender, RoutedEventArgs e)
  95. {
  96. if (CurrentIndex > 0)
  97. {
  98. CurrentIndex--;
  99. }
  100. }
  101. private void NextPage_Click(object sender, RoutedEventArgs e)
  102. {
  103. if (CurrentIndex < PageIndexLists.Count()-1 && PageIndexLists.Count() > 0)
  104. {
  105. CurrentIndex++;
  106. }
  107. }
  108. private void CurrentPage_KeyDown(object sender, KeyEventArgs e)
  109. {
  110. if (e.Key == Key.Enter)
  111. {
  112. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  113. {
  114. // this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  115. this.CurrentPage.Text = "1";
  116. }
  117. if (this.PageIndex != null)
  118. {
  119. //if (PageIndexLists.Contains( int.Parse(this.CurrentPage.Text)-1))
  120. //{
  121. // CurrentIndex=PageIndexLists.IndexOf(int.Parse(this.CurrentPage.Text)-1);
  122. // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  123. // AwaitRenderBitmap(document);
  124. //}
  125. //else
  126. //{
  127. // this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  128. // MessageBox.Show("超出页面范围");
  129. //}
  130. if (int.Parse(this.CurrentPage.Text)<=PageIndexLists.Count&&int.Parse(this.CurrentPage.Text) > 0)
  131. {
  132. CurrentIndex =int.Parse(this.CurrentPage.Text) - 1;
  133. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  134. AwaitRenderBitmap(document);
  135. }
  136. else
  137. {
  138. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  139. //MessageBox.Show("超出页面范围");
  140. }
  141. }
  142. }
  143. }
  144. private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  145. {
  146. e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
  147. }
  148. private void CurrentPage_LostFocus(object sender, RoutedEventArgs e)
  149. {
  150. if (this.CurrentPage.Text == "" || !int.TryParse(this.CurrentPage.Text, out int _))
  151. {
  152. this.CurrentPage.Text = (PageIndexLists[0] + 1).ToString();
  153. }
  154. if (this.PageIndex != null)
  155. {
  156. if (PageIndexLists.Count>(int.Parse(this.CurrentPage.Text) - 1))
  157. {
  158. CurrentIndex = int.Parse(this.CurrentPage.Text) - 1;
  159. //显示Doc页码
  160. //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  161. //显示位置
  162. this.CurrentPage.Text = (CurrentIndex +1).ToString();
  163. AwaitRenderBitmap(document);
  164. }
  165. else
  166. {
  167. //显示Doc页码
  168. //this.CurrentPage.Text = (PageIndexLists[CurrentIndex] + 1).ToString();
  169. //显示位置
  170. this.CurrentPage.Text = (CurrentIndex + 1).ToString();
  171. }
  172. }
  173. }
  174. }
  175. }