PreviewControl.xaml.cs 9.9 KB

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