PreviewControl.xaml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media;
  18. using System.Windows.Media.Imaging;
  19. using System.Windows.Navigation;
  20. using System.Windows.Shapes;
  21. using System.Xml.Linq;
  22. namespace Compdfkit_Tools.PDFControl
  23. {
  24. /// <summary>
  25. /// Interaction logic for PreviewControl.xaml
  26. /// </summary>
  27. public partial class PreviewControl : UserControl, INotifyPropertyChanged
  28. {
  29. private List<int> _pageIndexList = new List<int>();
  30. public List<int> PageRangeList
  31. {
  32. get => _pageIndexList;
  33. set
  34. {
  35. _pageIndexList = value;
  36. PageCount = _pageIndexList.Count;
  37. }
  38. }
  39. private int _pageCount = 1;
  40. public int PageCount
  41. {
  42. get => _pageCount;
  43. set
  44. {
  45. UpdateProper(ref _pageCount, value);
  46. }
  47. }
  48. private int _currentIndex = 1;
  49. public int CurrentIndex
  50. {
  51. get => _currentIndex;
  52. set
  53. {
  54. if (value < 1)
  55. {
  56. value = 1;
  57. }
  58. else if (value > PageCount)
  59. {
  60. value = PageCount;
  61. }
  62. if (UpdateProper(ref _currentIndex, value))
  63. {
  64. ImageSource = LoadImage(Document.PageAtIndex(_currentIndex - 1));
  65. }
  66. }
  67. }
  68. private double _scale = 1.0;
  69. public double Scale
  70. {
  71. get => _scale;
  72. set
  73. {
  74. UpdateProper(ref _scale, Math.Min((Math.Max(value, 0.1)), 10));
  75. }
  76. }
  77. private WriteableBitmap _imageSource;
  78. public WriteableBitmap ImageSource
  79. {
  80. get => _imageSource;
  81. set
  82. {
  83. UpdateProper(ref _imageSource, value);
  84. }
  85. }
  86. private CPDFDocument _document;
  87. public CPDFDocument Document
  88. {
  89. get { return _document; }
  90. set
  91. {
  92. _document = value;
  93. }
  94. }
  95. private double aspectRatio;
  96. private double thumbnailWidth;
  97. private double thumbnailHeight;
  98. public PreviewControl()
  99. {
  100. InitializeComponent();
  101. DataContext = this;
  102. }
  103. public void InitPreview(CPDFDocument document, List<int> pageRangeList)
  104. {
  105. Document = document;
  106. this.PageRangeList = pageRangeList;
  107. }
  108. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  109. {
  110. }
  111. private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  112. {
  113. }
  114. private void Image_MouseMove(object sender, MouseEventArgs e)
  115. {
  116. }
  117. public event PropertyChangedEventHandler PropertyChanged;
  118. protected virtual void OnPropertyChanged(string propertyName = null)
  119. {
  120. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  121. }
  122. protected bool UpdateProper<T>(ref T properValue,
  123. T newValue,
  124. [CallerMemberName] string properName = "")
  125. {
  126. if (object.Equals(properValue, newValue))
  127. return false;
  128. properValue = newValue;
  129. OnPropertyChanged(properName);
  130. return true;
  131. }
  132. private void CurrentIndexTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  133. {
  134. TextBox textBox = sender as TextBox;
  135. if (textBox != null)
  136. {
  137. if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, @"^[0-9]$"))
  138. {
  139. e.Handled = true;
  140. }
  141. }
  142. }
  143. public async Task LoadImageAsync(CPDFPage pdfPage)
  144. {
  145. ImageSource = null;
  146. WriteableBitmap bitmap = await Task.Run(() => LoadImage(pdfPage));
  147. Application.Current.Dispatcher.Invoke(new Action(() =>
  148. {
  149. ImageSource = bitmap;
  150. }));
  151. }
  152. private WriteableBitmap LoadImage(CPDFPage pdfPage)
  153. {
  154. Size pageSize = pdfPage.PageSize;
  155. double ratio = CalculateThumbnailSize(pageSize);
  156. Rect pageRect = new Rect(0, 0, (int)(pageSize.Width / 72.0 * 96 * ratio), (int)(pageSize.Height / 72.0 * 96 * ratio));
  157. byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * (96 / 72.0) * (96 / 72.0) * 4)];
  158. pdfPage.RenderPageBitmapWithMatrix((float)(96 / 72.0 * ratio), pageRect, 0xFFFFFFFF, bmpData, 0, true);
  159. WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.Width, (int)pageRect.Height, 96, 96, PixelFormats.Bgra32, null);
  160. writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.Width, (int)pageRect.Height), bmpData, writeableBitmap.BackBufferStride, 0);
  161. writeableBitmap.Freeze();
  162. return writeableBitmap;
  163. }
  164. private double CalculateThumbnailSize(Size size)
  165. {
  166. if (size.Height / size.Width > aspectRatio)
  167. {
  168. return thumbnailWidth / size.Width;
  169. }
  170. else
  171. {
  172. return thumbnailHeight / size.Height;
  173. }
  174. }
  175. private async void UserControl_Loaded(object sender, RoutedEventArgs e)
  176. {
  177. aspectRatio = ImageGd.ActualHeight / ImageGd.ActualWidth;
  178. thumbnailWidth = ImageGd.ActualWidth;
  179. thumbnailHeight = ImageGd.ActualHeight;
  180. await LoadImageAsync(Document.PageAtIndex(0));
  181. }
  182. private void CurrentIndexTxt_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  183. {
  184. try
  185. {
  186. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  187. {
  188. string checkText = Clipboard.GetText();
  189. if (int.TryParse(checkText, out int value))
  190. {
  191. e.CanExecute = true;
  192. }
  193. e.Handled = true;
  194. }
  195. }
  196. catch (Exception ex)
  197. {
  198. }
  199. }
  200. private void PageBtn_Click(object sender, RoutedEventArgs e)
  201. {
  202. var button = sender as Button;
  203. if (button != null)
  204. {
  205. if (button.Name == "PrePageBtn")
  206. {
  207. CurrentIndex--;
  208. }
  209. else
  210. {
  211. CurrentIndex++;
  212. }
  213. }
  214. }
  215. }
  216. }