SelectImage.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.Viewer.Helper;
  4. using ComPDFKit.Viewer.Layer;
  5. using ComPDFKitViewer;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using ComPDFKit.Tool.SettingParam;
  16. namespace ComPDFKit.Tool.DrawTool
  17. {
  18. public class PageImageItem
  19. {
  20. public int PageIndex;
  21. /// <summary>
  22. /// PDF DPI(72)
  23. /// </summary>
  24. public Rect PaintRect;
  25. public int PageRotate;
  26. public int ImageIndex;
  27. public PageImageItem Clone()
  28. {
  29. PageImageItem cloneItem = new PageImageItem();
  30. cloneItem.PageIndex = PageIndex;
  31. cloneItem.PaintRect = PaintRect;
  32. cloneItem.PageRotate = PageRotate;
  33. cloneItem.ImageIndex = ImageIndex;
  34. return cloneItem;
  35. }
  36. }
  37. internal class SelectImage : CustomizeLayer
  38. {
  39. protected DrawingContext drawDC { get; set; }
  40. protected DefaultDrawParam drawParam = new DefaultDrawParam();
  41. private Dictionary<int, List<PageImageItem>> pageImageDict = new Dictionary<int, List<PageImageItem>>();
  42. private PageImageItem hoverImageItem = null;
  43. /// <summary>
  44. /// Identify whether the image selection effect is being drawn
  45. /// </summary>
  46. protected bool isDrawSelectImage { get; set; }
  47. public override void Draw()
  48. {
  49. }
  50. public void Draw(CPDFViewer cPDFViewer)
  51. {
  52. Dispatcher.Invoke(() =>
  53. {
  54. drawDC = Open();
  55. List<RenderData> renderDatas = cPDFViewer.CurrentRenderFrame.GetRenderDatas();
  56. foreach (RenderData item in renderDatas)
  57. {
  58. if (pageImageDict.ContainsKey(item.PageIndex))
  59. {
  60. List<PageImageItem> PaintImageList = pageImageDict[item.PageIndex];
  61. foreach (PageImageItem SelectItem in PaintImageList)
  62. {
  63. DrawItem(SelectItem, item, cPDFViewer, drawDC, false);
  64. }
  65. }
  66. if (hoverImageItem != null && hoverImageItem.PageIndex == item.PageIndex)
  67. {
  68. DrawItem(hoverImageItem, item, cPDFViewer, drawDC, true);
  69. }
  70. }
  71. Present();
  72. });
  73. }
  74. public void CleanDraw(CPDFViewer cPDFViewer)
  75. {
  76. Dispatcher.Invoke(() =>
  77. {
  78. drawDC = Open();
  79. Present();
  80. ClearImageItem();
  81. });
  82. }
  83. private void DrawItem(PageImageItem SelectItem, RenderData renderData, CPDFViewer cPDFViewer, DrawingContext dc, bool isHover)
  84. {
  85. Rect drawRect = SelectItem.PaintRect;
  86. if (cPDFViewer.GetDocument() != null)
  87. {
  88. CPDFPage rawPage = cPDFViewer.GetDocument().PageAtIndex(renderData.PageIndex);
  89. if (rawPage != null)
  90. {
  91. int rotation = rawPage.Rotation - SelectItem.PageRotate;
  92. if (rotation != 0)
  93. {
  94. Size rawSize = renderData.RenderRect.Size;
  95. Matrix matrix = new Matrix();
  96. matrix.RotateAt(-rotation * 90, rawSize.Width / 2, rawSize.Height / 2);
  97. Rect checkRect = new Rect(0, 0, rawSize.Width, rawSize.Height);
  98. checkRect.Transform(matrix);
  99. matrix = new Matrix();
  100. matrix.RotateAt(rotation * 90, checkRect.Width / 96D * 72D / 2, checkRect.Height / 96D * 72D / 2);
  101. checkRect = new Rect(0, 0, checkRect.Width / 96D * 72D, checkRect.Height / 96D * 72D);
  102. drawRect.Transform(matrix);
  103. checkRect.Transform(matrix);
  104. drawRect = new Rect(drawRect.Left - checkRect.Left,
  105. drawRect.Top - checkRect.Top,
  106. drawRect.Width, drawRect.Height);
  107. }
  108. }
  109. }
  110. Rect BorderRect = renderData.PageBound;
  111. Rect RawPaintRect = new Rect(drawRect.Left / 72 * cPDFViewer.GetZoom() * 96 - renderData.CropLeft * cPDFViewer.GetZoom(),
  112. drawRect.Top / 72 * cPDFViewer.GetZoom() * 96 - renderData.CropTop * cPDFViewer.GetZoom(),
  113. drawRect.Width / 72 * cPDFViewer.GetZoom() * 96,
  114. drawRect.Height / 72 * cPDFViewer.GetZoom() * 96);
  115. RawPaintRect.X += BorderRect.X;
  116. RawPaintRect.Y += BorderRect.Y;
  117. RectangleGeometry clipGeometry = new RectangleGeometry();
  118. clipGeometry.Rect = renderData.PageBound;
  119. dc.PushClip(clipGeometry);
  120. if (isHover)
  121. {
  122. dc.DrawRectangle(null, drawParam.ViewerImagePen, RawPaintRect);
  123. }
  124. else
  125. {
  126. dc.DrawRectangle(drawParam.ViewerImageBackgroundBrush, null, RawPaintRect);
  127. }
  128. dc.Pop();
  129. }
  130. public bool ProcessMouseDownForSelectImage(Point pdfPoint, int pageIndex, CPDFViewer cPDFViewer, bool isNeedClear)
  131. {
  132. return ProcessSelectImageAtPos(cPDFViewer, pdfPoint, pageIndex, false, isNeedClear);
  133. }
  134. public bool ProcessMouseMoveForSelectImage(Point pdfPoint, int pageIndex, CPDFViewer cPDFViewer)
  135. {
  136. return ProcessSelectImageAtPos(cPDFViewer, pdfPoint, pageIndex, true, true);
  137. }
  138. private bool ProcessSelectImageAtPos(CPDFViewer cPDFViewer, Point pdfPoint, int pageIndex, bool isHover, bool isNeedClear)
  139. {
  140. bool result = false;
  141. if (cPDFViewer == null || cPDFViewer.GetDocument() == null || pageIndex < 0)
  142. {
  143. return result;
  144. }
  145. CPDFPage rawPage = cPDFViewer.GetDocument().PageAtIndex(pageIndex);
  146. CPDFImgSelection imageRanges = rawPage.GetImgSelection();
  147. List<CRect> checkList = imageRanges.GetImageRects();
  148. bool findItem = false;
  149. for (int i = 0; i < checkList.Count; i++)
  150. {
  151. Rect checkRect = DataConversionForWPF.CRectConversionForRect(checkList[i]);
  152. if (checkRect.Contains(pdfPoint))
  153. {
  154. findItem = true;
  155. if (isHover)
  156. {
  157. if (hoverImageItem == null || hoverImageItem.PageIndex != pageIndex || hoverImageItem.ImageIndex != i)
  158. {
  159. hoverImageItem = new PageImageItem()
  160. {
  161. PageIndex = pageIndex,
  162. PaintRect = checkRect,
  163. PageRotate = rawPage.Rotation,
  164. ImageIndex = i
  165. };
  166. result = true;
  167. }
  168. }
  169. else
  170. {
  171. if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
  172. {
  173. if (ContainImageItem(pageIndex, i))
  174. {
  175. result = RemoveImageItem(pageIndex, i);
  176. }
  177. else
  178. {
  179. if (pageImageDict != null && pageImageDict.Count > 0 && pageImageDict.Keys.Contains(pageIndex) == false)
  180. {
  181. ClearImageItem();
  182. }
  183. result = AddPageImageItem(pageIndex, checkRect, rawPage.Rotation, i);
  184. }
  185. }
  186. else
  187. {
  188. if (!ContainImageItem(pageIndex, i) || isNeedClear)
  189. {
  190. ClearImageItem();
  191. result = AddPageImageItem(pageIndex, checkRect, rawPage.Rotation, i);
  192. }
  193. else
  194. {
  195. result = true;
  196. }
  197. }
  198. hoverImageItem = null;
  199. }
  200. break;
  201. }
  202. }
  203. if (findItem == false && hoverImageItem != null)
  204. {
  205. hoverImageItem = null;
  206. result = false;
  207. }
  208. if (findItem == false && !isHover && pageImageDict != null && pageImageDict.Count > 0 && isNeedClear)
  209. {
  210. ClearImageItem();
  211. result = true;
  212. }
  213. return result;
  214. }
  215. private bool AddPageImageItem(int pageIndex, Rect paintRect, int pageRotate, int ImageIndex)
  216. {
  217. bool result = false;
  218. if (pageImageDict.ContainsKey(pageIndex) && pageImageDict[pageIndex] != null)
  219. {
  220. List<PageImageItem> imageItems = pageImageDict[pageIndex];
  221. if (ContainImageItem(pageIndex, ImageIndex) == false)
  222. {
  223. imageItems.Add(new PageImageItem()
  224. {
  225. PageIndex = pageIndex,
  226. PaintRect = paintRect,
  227. PageRotate = pageRotate,
  228. ImageIndex = ImageIndex
  229. });
  230. result = true;
  231. }
  232. }
  233. else
  234. {
  235. List<PageImageItem> imageItems = new List<PageImageItem>();
  236. imageItems.Add(new PageImageItem()
  237. {
  238. PageIndex = pageIndex,
  239. PaintRect = paintRect,
  240. PageRotate = pageRotate,
  241. ImageIndex = ImageIndex
  242. });
  243. pageImageDict.Add(pageIndex, imageItems);
  244. result = true;
  245. }
  246. return result;
  247. }
  248. private bool ContainImageItem(int pageIndex, int ImageIndex)
  249. {
  250. bool result = false;
  251. if (pageImageDict != null && pageImageDict.ContainsKey(pageIndex))
  252. {
  253. List<PageImageItem> imageItems = pageImageDict[pageIndex];
  254. if (imageItems != null && imageItems.Count > 0 && imageItems.Where(x => x.ImageIndex == ImageIndex).Count() > 0)
  255. {
  256. result = true;
  257. }
  258. }
  259. return result;
  260. }
  261. private bool RemoveImageItem(int pageIndex, int ImageIndex)
  262. {
  263. bool result = false;
  264. if (pageImageDict.ContainsKey(pageIndex) && pageImageDict[pageIndex] != null)
  265. {
  266. List<PageImageItem> imageItems = pageImageDict[pageIndex];
  267. if (imageItems.Count > 0)
  268. {
  269. List<PageImageItem> delItems = imageItems.Where(x => x.ImageIndex == ImageIndex).ToList();
  270. if (delItems.Count > 0)
  271. {
  272. result = true;
  273. }
  274. foreach (PageImageItem delItem in delItems)
  275. {
  276. imageItems.Remove(delItem);
  277. }
  278. }
  279. }
  280. return result;
  281. }
  282. public bool ClearImageItem()
  283. {
  284. bool result = false;
  285. if (pageImageDict.Count > 0)
  286. {
  287. pageImageDict.Clear();
  288. result = true;
  289. }
  290. hoverImageItem = null;
  291. return result;
  292. }
  293. public PageImageItem GetHoverImageItem()
  294. {
  295. return hoverImageItem.Clone();
  296. }
  297. public Dictionary<int, List<PageImageItem>> GetSelectImageItems()
  298. {
  299. return pageImageDict;
  300. }
  301. }
  302. }