PreviewControl.xaml.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 Point lastMousePosition;
  30. private double startVerticalOffset;
  31. private double startHorizontalOffset;
  32. private List<int> _pageIndexList = new List<int>();
  33. public List<int> PageRangeList
  34. {
  35. get => _pageIndexList;
  36. set
  37. {
  38. _pageIndexList = value;
  39. PageCount = _pageIndexList.Count;
  40. }
  41. }
  42. private int _pageCount = 1;
  43. public int PageCount
  44. {
  45. get => _pageCount;
  46. set
  47. {
  48. UpdateProper(ref _pageCount, value);
  49. }
  50. }
  51. private int _currentIndex = 1;
  52. public int CurrentIndex
  53. {
  54. get => _currentIndex;
  55. set
  56. {
  57. if (value < 1)
  58. {
  59. value = 1;
  60. }
  61. else if (value > PageCount)
  62. {
  63. value = PageCount;
  64. }
  65. if (UpdateProper(ref _currentIndex, value))
  66. {
  67. ImageSource = LoadImage(Document.PageAtIndex(_currentIndex - 1));
  68. }
  69. }
  70. }
  71. private double _scale = 0.3;
  72. public double Scale
  73. {
  74. get => _scale;
  75. set
  76. {
  77. UpdateProper(ref _scale, Math.Min((Math.Max(value, 0.1)), 1));
  78. }
  79. }
  80. private WriteableBitmap _imageSource;
  81. public WriteableBitmap ImageSource
  82. {
  83. get => _imageSource;
  84. set
  85. {
  86. UpdateProper(ref _imageSource, value);
  87. }
  88. }
  89. private CPDFDocument _document;
  90. public CPDFDocument Document
  91. {
  92. get { return _document; }
  93. set
  94. {
  95. _document = value;
  96. }
  97. }
  98. private double aspectRatio;
  99. private double thumbnailWidth;
  100. private double thumbnailHeight;
  101. public PreviewControl()
  102. {
  103. InitializeComponent();
  104. DataContext = this;
  105. }
  106. public void InitPreview(CPDFDocument document, List<int> pageRangeList)
  107. {
  108. Document = document;
  109. this.PageRangeList = pageRangeList;
  110. }
  111. private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  112. {
  113. lastMousePosition = e.GetPosition(Image);
  114. startVerticalOffset = ImageSv.VerticalOffset;
  115. startHorizontalOffset = ImageSv.HorizontalOffset;
  116. Image.CaptureMouse();
  117. this.Cursor = Cursors.Hand;
  118. }
  119. private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  120. {
  121. Image.ReleaseMouseCapture();
  122. this.Cursor = Cursors.Arrow;
  123. }
  124. private void Image_MouseMove(object sender, MouseEventArgs e)
  125. {
  126. if (ImageSv.Visibility == Visibility.Visible && Image.IsMouseCaptured)
  127. {
  128. Point currentMousePosition = e.GetPosition(Image);
  129. double deltaVerticalOffset = lastMousePosition.Y - currentMousePosition.Y;
  130. double deltaHorizontalOffset = lastMousePosition.X - currentMousePosition.X;
  131. double newVerticalOffset = startVerticalOffset + deltaVerticalOffset/3;
  132. double newHorizontalOffset = startHorizontalOffset + deltaHorizontalOffset/3;
  133. ImageSv.ScrollToVerticalOffset(newVerticalOffset);
  134. ImageSv.ScrollToHorizontalOffset(newHorizontalOffset);
  135. }
  136. }
  137. public event PropertyChangedEventHandler PropertyChanged;
  138. protected virtual void OnPropertyChanged(string propertyName = null)
  139. {
  140. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  141. }
  142. protected bool UpdateProper<T>(ref T properValue,
  143. T newValue,
  144. [CallerMemberName] string properName = "")
  145. {
  146. if (object.Equals(properValue, newValue))
  147. return false;
  148. properValue = newValue;
  149. OnPropertyChanged(properName);
  150. return true;
  151. }
  152. private void CurrentIndexTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
  153. {
  154. TextBox textBox = sender as TextBox;
  155. if (textBox != null)
  156. {
  157. if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, @"^[0-9]$"))
  158. {
  159. e.Handled = true;
  160. }
  161. }
  162. }
  163. public async Task LoadImageAsync(CPDFPage pdfPage)
  164. {
  165. ImageSource = null;
  166. WriteableBitmap bitmap = await Task.Run(() => LoadImage(pdfPage));
  167. Application.Current.Dispatcher.Invoke(new Action(() =>
  168. {
  169. ImageSource = bitmap;
  170. }));
  171. }
  172. private WriteableBitmap LoadImage(CPDFPage pdfPage)
  173. {
  174. Size pageSize = pdfPage.PageSize;
  175. double ratio = CalculateThumbnailSize(pageSize) * 3;
  176. Rect pageRect = new Rect(0, 0, (int)(pageSize.Width * ratio), (int)(pageSize.Height * ratio));
  177. byte[] bmpData = new byte[(int)(pageRect.Width * pageRect.Height * 4)];
  178. pdfPage.RenderPageBitmapWithMatrix((float)ratio, pageRect, 0xFFFFFFFF, bmpData, 0, true);
  179. WriteableBitmap writeableBitmap = new WriteableBitmap((int)pageRect.Width, (int)pageRect.Height, 96, 96, PixelFormats.Bgra32, null);
  180. writeableBitmap.WritePixels(new Int32Rect(0, 0, (int)pageRect.Width, (int)pageRect.Height), bmpData, writeableBitmap.BackBufferStride, 0);
  181. writeableBitmap.Freeze();
  182. return writeableBitmap;
  183. }
  184. private double CalculateThumbnailSize(Size size)
  185. {
  186. if (size.Height / size.Width > aspectRatio)
  187. {
  188. return (thumbnailWidth) / size.Width;
  189. }
  190. else
  191. {
  192. return (thumbnailHeight) / size.Height;
  193. }
  194. }
  195. private async void UserControl_Loaded(object sender, RoutedEventArgs e)
  196. {
  197. aspectRatio = ImageGd.ActualHeight / ImageGd.ActualWidth;
  198. thumbnailWidth = ImageGd.ActualWidth - 20;
  199. thumbnailHeight = ImageGd.ActualHeight - 20;
  200. await LoadImageAsync(Document.PageAtIndex(0));
  201. }
  202. private void CurrentIndexTxt_CanExecute(object sender, CanExecuteRoutedEventArgs e)
  203. {
  204. try
  205. {
  206. if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
  207. {
  208. string checkText = Clipboard.GetText();
  209. if (int.TryParse(checkText, out int value))
  210. {
  211. e.CanExecute = true;
  212. }
  213. e.Handled = true;
  214. }
  215. }
  216. catch (Exception ex)
  217. {
  218. }
  219. }
  220. private void PageBtn_Click(object sender, RoutedEventArgs e)
  221. {
  222. var button = sender as Button;
  223. if (button != null)
  224. {
  225. if (button.Name == "PrePageBtn")
  226. {
  227. CurrentIndex--;
  228. }
  229. else
  230. {
  231. CurrentIndex++;
  232. }
  233. }
  234. }
  235. private void ScaleBtn_Click(object sender, RoutedEventArgs e)
  236. {
  237. var button = sender as Button;
  238. if (button != null)
  239. {
  240. if (button.Name == "ZoomInBtn")
  241. {
  242. Scale -= 0.1;
  243. }
  244. else
  245. {
  246. Scale += 0.1;
  247. }
  248. }
  249. }
  250. }
  251. }