FillCreate.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKit.Tool.Help;
  6. using ComPDFKit.Tool.UndoManger;
  7. using ComPDFKitViewer;
  8. using ComPDFKitViewer.Annot;
  9. using ComPDFKitViewer.Helper;
  10. using ComPDFKitViewer.Layer;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. namespace ComPDFKit.Tool.FillTool
  21. {
  22. internal class FillCreateTool : BaseLayer
  23. {
  24. internal CPDFViewer PDFViewer { get; set; }
  25. internal int PageIndex { get; set; } = -1;
  26. internal Point MouseDownPos { get; set; } = new Point(-1, -1);
  27. internal Point CurrentMousePos { get; set; } = new Point(-1, -1);
  28. internal Rect PageBound { get; set; } = Rect.Empty;
  29. internal FillItem FillParam { get; private set; }
  30. internal bool IsMouseMoved { get; set; }
  31. private Rect DrawBound { get; set; }
  32. private double DPIScale { get; set; } = 96D / 72D;
  33. public override void Draw()
  34. {
  35. if (FillParam == null || PDFViewer == null)
  36. {
  37. return;
  38. }
  39. Point mousePos = Mouse.GetPosition(PDFViewer);
  40. Rect viewRect = new Rect(0, 0, PDFViewer.ActualWidth, PDFViewer.ActualHeight);
  41. if (viewRect.Contains(mousePos) == false && Mouse.LeftButton == MouseButtonState.Released)
  42. {
  43. return;
  44. }
  45. if (CurrentMousePos.X == -1 || CurrentMousePos.Y == -1)
  46. {
  47. return;
  48. }
  49. DrawingContext drawDC = Open();
  50. switch (FillParam.Type)
  51. {
  52. case FillType.Square:
  53. DrawRect(drawDC);
  54. break;
  55. case FillType.Circle:
  56. DrawCircle(drawDC);
  57. break;
  58. case FillType.Line:
  59. DrawLine(drawDC);
  60. break;
  61. case FillType.Cross:
  62. DrawCross(drawDC);
  63. break;
  64. case FillType.Check:
  65. DrawCheck(drawDC);
  66. break;
  67. default:
  68. break;
  69. }
  70. Present();
  71. }
  72. internal void SetFillParam(FillItem param)
  73. {
  74. FillParam = param;
  75. }
  76. private void DrawCircle(DrawingContext drawDC)
  77. {
  78. if (FillParam == null || FillParam.Param == null)
  79. {
  80. return;
  81. }
  82. CircleParam circleParam = FillParam.Param as CircleParam;
  83. if (circleParam == null)
  84. {
  85. return;
  86. }
  87. try
  88. {
  89. double zoom = PDFViewer.GetZoom();
  90. SolidColorBrush fillBrush = Brushes.Black;
  91. if (circleParam.BgColor != null && circleParam.BgColor.Length == 3)
  92. {
  93. fillBrush = new SolidColorBrush(Color.FromRgb(circleParam.BgColor[0], circleParam.BgColor[1], circleParam.BgColor[2]));
  94. }
  95. if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
  96. {
  97. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  98. xPos = Math.Min(xPos, PageBound.Right);
  99. double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
  100. yPos = Math.Min(yPos, PageBound.Bottom);
  101. double centerX = (MouseDownPos.X + xPos) / 2;
  102. double centerY = (MouseDownPos.Y + yPos) / 2;
  103. double radioX = Math.Abs((MouseDownPos.X - xPos) / 2);
  104. double radioY = Math.Abs((MouseDownPos.Y - yPos) / 2);
  105. if (FillParam.KeepRate)
  106. {
  107. double minRadio = Math.Min(radioX, radioY);
  108. radioX = minRadio;
  109. radioY = minRadio;
  110. centerX = MouseDownPos.X + ((xPos - MouseDownPos.X) >= 0 ? 1 : -1) * radioX;
  111. centerY = MouseDownPos.Y + ((yPos - MouseDownPos.Y) >= 0 ? 1 : -1) * radioX;
  112. }
  113. DrawBound = new Rect(centerX - radioX, centerY - radioY, radioX * 2, radioY * 2);
  114. drawDC.DrawEllipse(fillBrush, null, new Point(centerX, centerY), radioX, radioY);
  115. return;
  116. }
  117. DrawBound = new Rect(CurrentMousePos.X - FillParam.Width * zoom / 2, CurrentMousePos.Y - FillParam.Height * zoom / 2, FillParam.Width * zoom, FillParam.Height * zoom);
  118. drawDC.DrawEllipse(fillBrush, null, CurrentMousePos, FillParam.Width * zoom/2, FillParam.Height * zoom/2);
  119. }
  120. catch (Exception ex)
  121. {
  122. }
  123. }
  124. private void DrawRect(DrawingContext drawDC)
  125. {
  126. if (FillParam == null || FillParam.Param == null)
  127. {
  128. return;
  129. }
  130. SquareParam squareParam = FillParam.Param as SquareParam;
  131. if (squareParam == null)
  132. {
  133. return;
  134. }
  135. try
  136. {
  137. double zoom = PDFViewer.GetZoom();
  138. SolidColorBrush lineBrush = Brushes.Black;
  139. if (squareParam.LineColor != null && squareParam.LineColor.Length == 3)
  140. {
  141. lineBrush = new SolidColorBrush(Color.FromRgb(squareParam.LineColor[0], squareParam.LineColor[1], squareParam.LineColor[2]));
  142. }
  143. Pen borderPen = new Pen(lineBrush, squareParam.LineWidth * zoom * DPIScale);
  144. int halfPenWidth = (int)Math.Ceiling(borderPen.Thickness / 2);
  145. if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
  146. {
  147. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  148. xPos = Math.Min(xPos, PageBound.Right);
  149. double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
  150. yPos = Math.Min(yPos, PageBound.Bottom);
  151. double centerX = (MouseDownPos.X + xPos) / 2;
  152. double centerY = (MouseDownPos.Y + yPos) / 2;
  153. double radioX = Math.Abs((MouseDownPos.X - xPos) / 2);
  154. double radioY = Math.Abs((MouseDownPos.Y - yPos) / 2);
  155. if (FillParam.KeepRate)
  156. {
  157. double minRadio = Math.Min(radioX, radioY);
  158. radioX = minRadio;
  159. radioY = minRadio;
  160. centerX = MouseDownPos.X + ((xPos - MouseDownPos.X) >= 0 ? 1 : -1) * radioX;
  161. centerY = MouseDownPos.Y + ((yPos - MouseDownPos.Y) >= 0 ? 1 : -1) * radioY;
  162. }
  163. DrawBound = new Rect(centerX - radioX, centerY - radioY, radioX * 2, radioY * 2);
  164. drawDC.DrawRectangle(null, borderPen, new Rect(DrawBound.Left + halfPenWidth, DrawBound.Top + halfPenWidth, DrawBound.Width - 2 * halfPenWidth, DrawBound.Height - 2 * halfPenWidth));
  165. return;
  166. }
  167. DrawBound = new Rect(
  168. CurrentMousePos.X - FillParam.Width / 2 * zoom,
  169. CurrentMousePos.Y - FillParam.Height / 2 * zoom,
  170. FillParam.Width * zoom, FillParam.Height * zoom);
  171. drawDC.DrawRectangle(null, borderPen, new Rect(DrawBound.Left + halfPenWidth, DrawBound.Top + halfPenWidth, DrawBound.Width - 2 * halfPenWidth, DrawBound.Height - 2 * halfPenWidth));
  172. }
  173. catch (Exception ex)
  174. {
  175. }
  176. }
  177. private void DrawLine(DrawingContext drawDC)
  178. {
  179. if (FillParam == null || FillParam.Param == null)
  180. {
  181. return;
  182. }
  183. LineParam lineParam = FillParam.Param as LineParam;
  184. if (lineParam == null)
  185. {
  186. return;
  187. }
  188. try
  189. {
  190. double zoom = PDFViewer.GetZoom();
  191. SolidColorBrush lineBrush = Brushes.Black;
  192. if (lineParam.LineColor != null && lineParam.LineColor.Length == 3)
  193. {
  194. lineBrush = new SolidColorBrush(Color.FromRgb(lineParam.LineColor[0], lineParam.LineColor[1], lineParam.LineColor[2]));
  195. }
  196. Pen borderPen = new Pen(lineBrush, lineParam.LineWidth * zoom);
  197. if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
  198. {
  199. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  200. xPos = Math.Min(xPos, PageBound.Right);
  201. DrawBound = new Rect(new Point(xPos, MouseDownPos.Y), MouseDownPos);
  202. drawDC.DrawLine(borderPen, new Point(xPos, MouseDownPos.Y), MouseDownPos);
  203. return;
  204. }
  205. Point leftPos = new Point(
  206. CurrentMousePos.X - FillParam.Width / 2 * zoom,
  207. CurrentMousePos.Y);
  208. Point rightPos = new Point(
  209. CurrentMousePos.X + FillParam.Width / 2 * zoom,
  210. CurrentMousePos.Y);
  211. DrawBound = new Rect(leftPos, rightPos);
  212. drawDC.DrawLine(borderPen, leftPos, rightPos);
  213. }
  214. catch (Exception ex)
  215. {
  216. }
  217. }
  218. private void GetPointsBound(List<CPoint> checkList, out Rect checkBound)
  219. {
  220. checkBound = new Rect(0, 0, 0, 0);
  221. if (checkList == null || checkList.Count == 0)
  222. {
  223. return;
  224. }
  225. int left = 0;
  226. int top = 0;
  227. int right = 0;
  228. int bottom = 0;
  229. foreach (CPoint point in checkList)
  230. {
  231. left = (int)Math.Min(left, point.x);
  232. top = (int)Math.Min(t, point.y);
  233. right = (int)Math.Max(right, point.x);
  234. bottom = (int)Math.Max(bottom, point.y);
  235. }
  236. checkBound = new Rect(left, top, right - left, bottom - top);
  237. }
  238. private void DrawCheck(DrawingContext drawDC)
  239. {
  240. if (FillParam == null || FillParam.Param == null)
  241. {
  242. return;
  243. }
  244. InkParam inkParam = FillParam.Param as InkParam;
  245. if (inkParam == null)
  246. {
  247. return;
  248. }
  249. try
  250. {
  251. double zoom = PDFViewer.GetZoom();
  252. SolidColorBrush inkBrush = Brushes.Black;
  253. if (inkParam.InkColor != null && inkParam.InkColor.Length == 3)
  254. {
  255. inkBrush = new SolidColorBrush(Color.FromRgb(inkParam.InkColor[0], inkParam.InkColor[1], inkParam.InkColor[2]));
  256. }
  257. Pen borderPen = new Pen(inkBrush, inkParam.Thickness * zoom);
  258. borderPen.StartLineCap=PenLineCap.Round;
  259. borderPen.EndLineCap=PenLineCap.Round;
  260. if (inkParam.InkPath == null || inkParam.InkPath.Count == 0)
  261. {
  262. int width = Math.Max(0, FillParam.Width);
  263. int height = Math.Max(0, FillParam.Height);
  264. if (width == 0 || height == 0)
  265. {
  266. width = 30;
  267. height = 30;
  268. }
  269. List<List<CPoint>> defaultList = new List<List<CPoint>>();
  270. List<CPoint> pointList = new List<CPoint>();
  271. defaultList.Add(pointList);
  272. pointList.Add(new CPoint(0, height * 2 / 3));
  273. pointList.Add(new CPoint(width / 3, height));
  274. pointList.Add(new CPoint(width, height / 3));
  275. inkParam.InkPath = defaultList;
  276. }
  277. if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
  278. {
  279. double minX = 0;
  280. double minY = 0;
  281. double maxX = 0;
  282. double maxY = 0;
  283. foreach (List<CPoint> loopItems in inkParam.InkPath)
  284. {
  285. GetPointsBound(loopItems, out Rect checkBound);
  286. minX = Math.Min(minX, checkBound.X);
  287. minY = Math.Min(minY, checkBound.Y);
  288. maxX = Math.Max(maxX, checkBound.Right);
  289. maxY = Math.Max(maxY, checkBound.Bottom);
  290. }
  291. Rect prevRect = new Rect(minX, minY, maxX - minX, maxY - minY);
  292. if (prevRect.Width == 0 || prevRect.Height == 0)
  293. {
  294. return;
  295. }
  296. List<List<Point>> drawPathList = new List<List<Point>>();
  297. foreach (List<CPoint> loopItems in inkParam.InkPath)
  298. {
  299. List<Point> drawPath = new List<Point>();
  300. foreach (CPoint item in loopItems)
  301. {
  302. drawPath.Add(new Point(item.x - minX, item.y - minY));
  303. }
  304. if (drawPath.Count >= 2)
  305. {
  306. drawPathList.Add(drawPath);
  307. }
  308. }
  309. double offsetX = (maxX - minX) / 2;
  310. double offsetY = (maxY - minY) / 2;
  311. Point centerPoint = CurrentMousePos;
  312. if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
  313. {
  314. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  315. xPos = Math.Min(xPos, PageBound.Right);
  316. double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
  317. yPos = Math.Min(yPos, PageBound.Bottom);
  318. if (FillParam.KeepRate)
  319. {
  320. if (GetRateMoveSize(prevRect, out Point ratePoint) == false)
  321. {
  322. return;
  323. }
  324. xPos = ratePoint.X;
  325. yPos = ratePoint.Y;
  326. }
  327. Rect drawRect = new Rect(MouseDownPos, new Point(xPos, yPos));
  328. if (drawRect.Width == 0 || drawRect.Height == 0)
  329. {
  330. return;
  331. }
  332. double rateX = drawRect.Width / prevRect.Width / zoom;
  333. double rateY = drawRect.Height / prevRect.Height / zoom;
  334. if (FillParam.KeepRate)
  335. {
  336. rateY = rateX * prevRect.Height / prevRect.Width;
  337. }
  338. offsetX = (offsetX * rateX);
  339. offsetY = (offsetY * rateY);
  340. List<List<Point>> rateList = new List<List<Point>>();
  341. foreach (List<Point> loopItems in drawPathList)
  342. {
  343. List<Point> ratePath = new List<Point>();
  344. foreach (Point item in loopItems)
  345. {
  346. ratePath.Add(new Point((item.X - minX) * rateX, (item.Y - minY) * rateY));
  347. }
  348. rateList.Add(ratePath);
  349. }
  350. drawPathList = rateList;
  351. centerPoint.X = (MouseDownPos.X + xPos) / 2;
  352. centerPoint.Y = (MouseDownPos.Y + yPos) / 2;
  353. }
  354. GeometryGroup drawGroup = new GeometryGroup();
  355. foreach (List<Point> loopItems in drawPathList)
  356. {
  357. PathGeometry pathGeometry = new PathGeometry();
  358. PathFigure path = new PathFigure();
  359. path.IsClosed = false;
  360. path.StartPoint = new Point((loopItems[0].X - offsetX) * zoom + centerPoint.X, (loopItems[0].Y - offsetY) * zoom + centerPoint.Y);
  361. for (int i = 1; i < loopItems.Count; i++)
  362. {
  363. LineSegment segment = new LineSegment();
  364. segment.IsSmoothJoin = true;
  365. segment.Point = new Point((loopItems[i].X - offsetX) * zoom + centerPoint.X, (loopItems[i].Y - offsetY) * zoom + centerPoint.Y);
  366. path.Segments.Add(segment);
  367. }
  368. pathGeometry.Figures.Add(path);
  369. drawGroup.Children.Add(pathGeometry);
  370. }
  371. DrawBound = drawGroup.Bounds;
  372. drawDC.DrawGeometry(null, borderPen, drawGroup);
  373. return;
  374. }
  375. }
  376. catch (Exception ex)
  377. {
  378. }
  379. }
  380. private void DrawCross(DrawingContext drawDC)
  381. {
  382. if (FillParam == null || FillParam.Param == null)
  383. {
  384. return;
  385. }
  386. InkParam inkParam = FillParam.Param as InkParam;
  387. if (inkParam == null)
  388. {
  389. return;
  390. }
  391. try
  392. {
  393. double zoom = PDFViewer.GetZoom();
  394. SolidColorBrush inkBrush = Brushes.Black;
  395. if (inkParam.InkColor != null && inkParam.InkColor.Length == 3)
  396. {
  397. inkBrush = new SolidColorBrush(Color.FromRgb(inkParam.InkColor[0], inkParam.InkColor[1], inkParam.InkColor[2]));
  398. }
  399. Pen borderPen = new Pen(inkBrush, inkParam.Thickness * zoom);
  400. borderPen.StartLineCap = PenLineCap.Round;
  401. borderPen.EndLineCap = PenLineCap.Round;
  402. if (inkParam.InkPath == null || inkParam.InkPath.Count == 0)
  403. {
  404. int width = Math.Max(0, FillParam.Width);
  405. int height = Math.Max(0, FillParam.Height);
  406. if (width == 0 || height == 0)
  407. {
  408. width = 30;
  409. height = 30;
  410. }
  411. List<List<CPoint>> defaultList = new List<List<CPoint>>();
  412. List<CPoint> pointList = new List<CPoint>();
  413. defaultList.Add(pointList);
  414. pointList.Add(new CPoint(0, 0));
  415. pointList.Add(new CPoint(width / 2, height / 2));
  416. pointList.Add(new CPoint(width, height));
  417. pointList = new List<CPoint>();
  418. defaultList.Add(pointList);
  419. pointList.Add(new CPoint(0, height));
  420. pointList.Add(new CPoint(width / 2, height / 2));
  421. pointList.Add(new CPoint(width, 0));
  422. inkParam.InkPath = defaultList;
  423. }
  424. if (inkParam.InkPath != null && inkParam.InkPath.Count > 0)
  425. {
  426. double minX = 0;
  427. double minY = 0;
  428. double maxX = 0;
  429. double maxY = 0;
  430. foreach (List<CPoint> loopItems in inkParam.InkPath)
  431. {
  432. GetPointsBound(loopItems, out Rect checkBound);
  433. minX = Math.Min(minX, checkBound.X);
  434. minY = Math.Min(minY, checkBound.Y);
  435. maxX = Math.Max(maxX, checkBound.Right);
  436. maxY = Math.Max(maxY, checkBound.Bottom);
  437. }
  438. Rect prevRect = new Rect(minX, minY, maxX - minX, maxY - minY);
  439. if (prevRect.Width == 0 || prevRect.Height == 0)
  440. {
  441. return;
  442. }
  443. List<List<Point>> drawPathList = new List<List<Point>>();
  444. foreach (List<CPoint> loopItems in inkParam.InkPath)
  445. {
  446. List<Point> drawPath = new List<Point>();
  447. foreach (CPoint item in loopItems)
  448. {
  449. drawPath.Add(new Point(item.x - minX, item.y - minY));
  450. }
  451. if (drawPath.Count >= 2)
  452. {
  453. drawPathList.Add(drawPath);
  454. }
  455. }
  456. double offsetX = (maxX - minX) / 2;
  457. double offsetY = (maxY - minY) / 2;
  458. Point centerPoint = CurrentMousePos;
  459. if (PageIndex >= 0 && Mouse.LeftButton == MouseButtonState.Pressed)
  460. {
  461. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  462. xPos = Math.Min(xPos, PageBound.Right);
  463. double yPos = Math.Max(PageBound.Top, CurrentMousePos.Y);
  464. yPos = Math.Min(yPos, PageBound.Bottom);
  465. if (FillParam.KeepRate)
  466. {
  467. if(GetRateMoveSize(prevRect,out Point ratePoint)==false)
  468. {
  469. return;
  470. }
  471. xPos = ratePoint.X;
  472. yPos = ratePoint.Y;
  473. }
  474. Rect drawRect = new Rect(MouseDownPos, new Point(xPos, yPos));
  475. if (drawRect.Width == 0 || drawRect.Height == 0)
  476. {
  477. return;
  478. }
  479. double rateX = drawRect.Width / prevRect.Width / zoom;
  480. double rateY = drawRect.Height / prevRect.Height / zoom;
  481. if (FillParam.KeepRate)
  482. {
  483. rateY = rateX * prevRect.Height / prevRect.Width;
  484. }
  485. offsetX = (offsetX * rateX);
  486. offsetY = (offsetY * rateY);
  487. List<List<Point>> rateList = new List<List<Point>>();
  488. foreach (List<Point> loopItems in drawPathList)
  489. {
  490. List<Point> ratePath = new List<Point>();
  491. foreach (Point item in loopItems)
  492. {
  493. ratePath.Add(new Point((item.X - minX) * rateX, (item.Y - minY) * rateY));
  494. }
  495. rateList.Add(ratePath);
  496. }
  497. drawPathList = rateList;
  498. centerPoint.X = (MouseDownPos.X + xPos) / 2;
  499. centerPoint.Y = (MouseDownPos.Y + yPos) / 2;
  500. }
  501. GeometryGroup drawGroup = new GeometryGroup();
  502. foreach (List<Point> loopItems in drawPathList)
  503. {
  504. PathGeometry pathGeometry = new PathGeometry();
  505. PathFigure path = new PathFigure();
  506. path.IsClosed = false;
  507. path.StartPoint = new Point((loopItems[0].X - offsetX) * zoom + centerPoint.X, (loopItems[0].Y - offsetY) * zoom + centerPoint.Y);
  508. for (int i = 1; i < loopItems.Count; i++)
  509. {
  510. LineSegment segment = new LineSegment();
  511. segment.IsSmoothJoin=true;
  512. segment.Point = new Point((loopItems[i].X - offsetX) * zoom + centerPoint.X, (loopItems[i].Y - offsetY) * zoom + centerPoint.Y);
  513. path.Segments.Add(segment);
  514. }
  515. pathGeometry.Figures.Add(path);
  516. drawGroup.Children.Add(pathGeometry);
  517. }
  518. DrawBound = drawGroup.Bounds;
  519. drawDC.DrawGeometry(null, borderPen, drawGroup);
  520. return;
  521. }
  522. }
  523. catch (Exception ex)
  524. {
  525. }
  526. }
  527. private bool GetRateMoveSize(Rect oldRect ,out Point ratePoint)
  528. {
  529. ratePoint=new Point(0,0);
  530. double rateY = oldRect.Height / oldRect.Width;
  531. double xPos = Math.Max(PageBound.Left, CurrentMousePos.X);
  532. xPos = Math.Min(xPos, PageBound.Right);
  533. double moveX = xPos - MouseDownPos.X;
  534. double moveY = CurrentMousePos.Y - MouseDownPos.Y;
  535. if (moveX == 0)
  536. {
  537. return false;
  538. }
  539. if (moveY == 0)
  540. {
  541. moveY = 1;
  542. }
  543. double rateMoveY = Math.Abs(moveX * rateY);
  544. double yPos = MouseDownPos.Y + rateMoveY * Math.Abs(moveY) / moveY;
  545. if (yPos >= PageBound.Top && yPos <= PageBound.Bottom)
  546. {
  547. ratePoint= new Point(xPos, yPos);
  548. return true;
  549. }
  550. if(moveY>=0)
  551. {
  552. rateMoveY=PageBound.Bottom-MouseDownPos.Y;
  553. yPos=PageBound.Bottom;
  554. }
  555. else
  556. {
  557. rateMoveY=MouseDownPos.Y-PageBound.Top;
  558. yPos=PageBound.Top;
  559. }
  560. double rateMoveX = rateMoveY / rateY;
  561. xPos = MouseDownPos.X + rateMoveX * Math.Abs(moveX) / (moveX);
  562. ratePoint = new Point(xPos, yPos);
  563. return true;
  564. }
  565. internal FillItem SaveFillItem()
  566. {
  567. if (FillParam == null || PageIndex < 0 || PDFViewer == null)
  568. {
  569. return null;
  570. }
  571. try
  572. {
  573. switch (FillParam.Type)
  574. {
  575. case FillType.Square:
  576. return SaveRect();
  577. case FillType.Circle:
  578. return SaveCircle();
  579. case FillType.Line:
  580. return SaveLine();
  581. case FillType.Check:
  582. return SaveCheck();
  583. case FillType.Cross:
  584. return SaveCross();
  585. default:
  586. return null;
  587. }
  588. }
  589. catch (Exception ex)
  590. {
  591. }
  592. return null;
  593. }
  594. private FillItem SaveRect()
  595. {
  596. double zoom = PDFViewer.GetZoom();
  597. CPDFDocument pdfDoc = PDFViewer.GetDocument();
  598. if (zoom <= 0 || pdfDoc == null)
  599. {
  600. return null;
  601. }
  602. CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
  603. SquareParam param = FillParam.Param as SquareParam;
  604. if (pdfPage == null || param == null)
  605. {
  606. return null;
  607. }
  608. Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
  609. Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
  610. Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
  611. CPDFSquareAnnotation squareAnnot = (CPDFSquareAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE);
  612. squareAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
  613. squareAnnot.SetLineWidth((float)param.LineWidth);
  614. squareAnnot.SetLineColor(param.LineColor);
  615. squareAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  616. if(string.IsNullOrEmpty(param.Author)==false)
  617. {
  618. squareAnnot.SetAuthor(param.Author);
  619. }
  620. squareAnnot.UpdateAp();
  621. FillItem cloneItem = FillParam.Clone();
  622. cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, squareAnnot);
  623. cloneItem.HistoryItem = new SquareAnnotHistory();
  624. cloneItem.HistoryItem.Action = HistoryAction.Add;
  625. cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, squareAnnot);
  626. cloneItem.HistoryItem.PDFDoc = pdfDoc;
  627. PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
  628. return cloneItem;
  629. }
  630. private FillItem SaveCircle()
  631. {
  632. double zoom = PDFViewer.GetZoom();
  633. CPDFDocument pdfDoc = PDFViewer.GetDocument();
  634. if (zoom <= 0 || pdfDoc == null)
  635. {
  636. return null;
  637. }
  638. CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
  639. CircleParam param = FillParam.Param as CircleParam;
  640. if (pdfPage == null || param == null)
  641. {
  642. return null;
  643. }
  644. Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
  645. Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
  646. Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
  647. CPDFCircleAnnotation circleAnnot = (CPDFCircleAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE);
  648. circleAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
  649. if (param.HasBgColor)
  650. {
  651. circleAnnot.SetBgColor(param.BgColor);
  652. circleAnnot.SetLineColor(param.BgColor);
  653. }
  654. circleAnnot.SetLineWidth(0);
  655. circleAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  656. if (string.IsNullOrEmpty(param.Author) == false)
  657. {
  658. circleAnnot.SetAuthor(param.Author);
  659. }
  660. circleAnnot.UpdateAp();
  661. FillItem cloneItem = FillParam.Clone();
  662. cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, circleAnnot);
  663. cloneItem.HistoryItem = new CircleAnnotHistory();
  664. cloneItem.HistoryItem.Action = HistoryAction.Add;
  665. cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, circleAnnot);
  666. cloneItem.HistoryItem.PDFDoc = pdfDoc;
  667. PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
  668. return cloneItem;
  669. }
  670. private FillItem SaveLine()
  671. {
  672. double zoom = PDFViewer.GetZoom();
  673. CPDFDocument pdfDoc = PDFViewer.GetDocument();
  674. if (zoom <= 0 || pdfDoc == null)
  675. {
  676. return null;
  677. }
  678. CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
  679. LineParam param = FillParam.Param as LineParam;
  680. if (pdfPage == null || param == null)
  681. {
  682. return null;
  683. }
  684. Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
  685. Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
  686. Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
  687. CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_LINE);
  688. lineAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
  689. lineAnnot.SetLinePoints(new CPoint((float)pdfRect.Left,(float)(pdfRect.Top+pdfRect.Height/2)),new CPoint((float)pdfRect.Right,(float)(pdfRect.Top+pdfRect.Height/2)));
  690. lineAnnot.SetLineWidth((float)param.LineWidth);
  691. lineAnnot.SetLineColor(param.LineColor);
  692. lineAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  693. if (string.IsNullOrEmpty(param.Author) == false)
  694. {
  695. lineAnnot.SetAuthor(param.Author);
  696. }
  697. lineAnnot.UpdateAp();
  698. FillItem cloneItem = FillParam.Clone();
  699. cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, lineAnnot);
  700. cloneItem.HistoryItem = new LineAnnotHistory();
  701. cloneItem.HistoryItem.Action = HistoryAction.Add;
  702. cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, lineAnnot);
  703. cloneItem.HistoryItem.PDFDoc = pdfDoc;
  704. PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
  705. return cloneItem;
  706. }
  707. private FillItem SaveCheck()
  708. {
  709. double zoom = PDFViewer.GetZoom();
  710. CPDFDocument pdfDoc = PDFViewer.GetDocument();
  711. if (zoom <= 0 || pdfDoc == null)
  712. {
  713. return null;
  714. }
  715. CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
  716. InkParam param = FillParam.Param as InkParam;
  717. if (pdfPage == null || param == null)
  718. {
  719. return null;
  720. }
  721. if(param==null || param.InkPath==null || param.InkPath.Count==0)
  722. {
  723. return null;
  724. }
  725. List<List<CPoint>> rawPointList = new List<List<CPoint>>();
  726. Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
  727. Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
  728. Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
  729. foreach (List<CPoint> segmentList in param.InkPath)
  730. {
  731. List<CPoint> pointList = new List<CPoint>();
  732. foreach (CPoint point in segmentList)
  733. {
  734. pointList.Add(new CPoint(point.x/96F*72F+(float)pdfRect.Left,point.y/96F*72F+(float)pdfRect.Top));
  735. }
  736. if (pointList.Count > 0)
  737. {
  738. rawPointList.Add(pointList);
  739. }
  740. }
  741. CPDFInkAnnotation inkAnnot = (CPDFInkAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  742. inkAnnot.SetInkPath(rawPointList);
  743. inkAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
  744. inkAnnot.SetInkColor(param.InkColor);
  745. inkAnnot.SetThickness((float)param.Thickness);
  746. inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  747. if (string.IsNullOrEmpty(param.Author) == false)
  748. {
  749. inkAnnot.SetAuthor(param.Author);
  750. }
  751. inkAnnot.UpdateAp();
  752. FillItem cloneItem = FillParam.Clone();
  753. cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
  754. cloneItem.HistoryItem = new InkAnnotHistory();
  755. cloneItem.HistoryItem.Action = HistoryAction.Add;
  756. cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
  757. cloneItem.HistoryItem.PDFDoc = pdfDoc;
  758. PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
  759. return cloneItem;
  760. }
  761. private FillItem SaveCross()
  762. {
  763. double zoom = PDFViewer.GetZoom();
  764. CPDFDocument pdfDoc = PDFViewer.GetDocument();
  765. if (zoom <= 0 || pdfDoc == null)
  766. {
  767. return null;
  768. }
  769. CPDFPage pdfPage = pdfDoc.PageAtIndex(PageIndex);
  770. InkParam param = FillParam.Param as InkParam;
  771. if (pdfPage == null || param == null)
  772. {
  773. return null;
  774. }
  775. if (param == null || param.InkPath == null || param.InkPath.Count == 0)
  776. {
  777. return null;
  778. }
  779. List<List<CPoint>> rawPointList = new List<List<CPoint>>();
  780. Rect offsetRect = new Rect(DrawBound.X - PageBound.X, DrawBound.Y - PageBound.Y, DrawBound.Width, DrawBound.Height);
  781. Rect standRect = new Rect(offsetRect.X / zoom, offsetRect.Y / zoom, offsetRect.Width / zoom, offsetRect.Height / zoom);
  782. Rect pdfRect = new Rect(standRect.X / 96D * 72D, standRect.Y / 96D * 72D, standRect.Width / 96D * 72D, standRect.Height / 96D * 72D);
  783. foreach (List<CPoint> segmentList in param.InkPath)
  784. {
  785. List<CPoint> pointList = new List<CPoint>();
  786. foreach (CPoint point in segmentList)
  787. {
  788. pointList.Add(new CPoint(point.x / 96F * 72F + (float)pdfRect.Left, point.y / 96F * 72F + (float)pdfRect.Top));
  789. }
  790. if (pointList.Count > 0)
  791. {
  792. rawPointList.Add(pointList);
  793. }
  794. }
  795. CPDFInkAnnotation inkAnnot = (CPDFInkAnnotation)pdfPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_INK);
  796. inkAnnot.SetInkPath(rawPointList);
  797. inkAnnot.SetRect(new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top));
  798. inkAnnot.SetInkColor(param.InkColor);
  799. inkAnnot.SetThickness((float)param.Thickness);
  800. inkAnnot.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  801. if (string.IsNullOrEmpty(param.Author) == false)
  802. {
  803. inkAnnot.SetAuthor(param.Author);
  804. }
  805. inkAnnot.UpdateAp();
  806. FillItem cloneItem = FillParam.Clone();
  807. cloneItem.Param = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
  808. cloneItem.HistoryItem = new InkAnnotHistory();
  809. cloneItem.HistoryItem.Action = HistoryAction.Add;
  810. cloneItem.HistoryItem.CurrentParam = ParamConverter.AnnotConverter(pdfDoc, inkAnnot);
  811. cloneItem.HistoryItem.PDFDoc = pdfDoc;
  812. PDFViewer.UndoManager.AddHistory(cloneItem.HistoryItem);
  813. return cloneItem;
  814. }
  815. }
  816. }