PreviewControl.xaml.cs 10 KB

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