CreateAnnotTool.cs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. using ComPDFKit.Import;
  2. using ComPDFKit.Measure;
  3. using ComPDFKit.PDFAnnotation;
  4. using ComPDFKit.PDFDocument;
  5. using ComPDFKit.PDFPage;
  6. using ComPDFKit.Tool.Help;
  7. using ComPDFKit.Tool.SettingParam;
  8. using ComPDFKit.Tool.UndoManger;
  9. using ComPDFKit.Viewer.Helper;
  10. using ComPDFKit.Viewer.Layer;
  11. using ComPDFKitViewer;
  12. using ComPDFKitViewer.Helper;
  13. using ComPDFKitViewer.Layer;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Globalization;
  18. using System.Linq;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Media;
  22. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  23. namespace ComPDFKit.Tool.DrawTool
  24. {
  25. internal class CreateAnnotTool : CustomizeLayer
  26. {
  27. public event EventHandler<bool> UpdateAnnotHandler;
  28. public event EventHandler<AnnotParam> CreateFreetextCanceled;
  29. public event EventHandler<MeasureEventArgs> MeasureChanged;
  30. public static DependencyProperty PopupTextAttachDataProperty = DependencyProperty.Register("PopupTextAttachData", typeof(CPDFAnnotation), typeof(CPDFViewerTool));
  31. #region Attributes
  32. /// <summary>
  33. /// Indicates whether proportional scaling is required
  34. /// </summary>
  35. protected bool isProportionalScaling { get; set; } = false;
  36. /// <summary>
  37. /// Mouse start point
  38. /// </summary>
  39. protected Point mouseStartPoint { get; set; }
  40. /// <summary>
  41. /// Mouse end point
  42. /// </summary>
  43. protected Point mouseEndPoint { get; set; }
  44. /// <summary>
  45. /// Crop point
  46. /// </summary>
  47. protected Point cropPoint { get; set; }
  48. /// <summary>
  49. ///Is drawing annotation
  50. /// </summary>
  51. protected bool isDrawAnnot { get; set; }
  52. /// <summary>
  53. /// Current zoom factor
  54. /// </summary>
  55. private double zoomFactor { get; set; } = 1;
  56. /// <summary>
  57. /// Draw rectangle
  58. /// </summary>
  59. protected Rect drawRect { get; set; }
  60. /// <summary>
  61. /// The rectangle representing the maximum drawing area
  62. /// </summary>
  63. protected Rect maxRect { get; set; }
  64. /// <summary>
  65. /// The rectangle representing the original page range (calculated offset in continuous mode)
  66. /// </summary>
  67. protected Rect pageBound { get; set; }
  68. protected int pageIndex { get; set; } = -1;
  69. /// <summary>
  70. /// The rectangle at standard DPI (without subtracting half of the pen thickness)
  71. /// </summary>
  72. protected Rect DPIRect { get; set; }
  73. /// <summary>
  74. /// The offset value during movement
  75. /// </summary>
  76. protected Point moveOffset { get; set; } = new Point(0, 0);
  77. protected DrawingContext drawDC { get; set; }
  78. protected CPDFAnnotation cPDFAnnotation
  79. {
  80. get;
  81. set;
  82. }
  83. protected CPDFViewer cPDFViewer { get; set; }
  84. protected List<Point> inkDrawPoints = new List<Point>();
  85. /// <summary>
  86. /// The collection of points measured for annotation drawing
  87. /// </summary>
  88. protected PointCollection drawPoints { get; set; } = new PointCollection();
  89. protected double textPadding { get; set; } = 10;
  90. protected Border lastTextBorder;
  91. protected TextBox lastTextui;
  92. protected Point freeTextPoint { get; set; }
  93. protected MeasureSetting measureSetting = new MeasureSetting();
  94. protected DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
  95. protected DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
  96. #endregion
  97. public CreateAnnotTool(MeasureSetting setting, DefaultDrawParam drawParam, DefaultSettingParam settingParam)
  98. {
  99. measureSetting = setting;
  100. defaultDrawParam = drawParam;
  101. defaultSettingParam = settingParam;
  102. }
  103. public Point GetStartPoint()
  104. {
  105. return DpiHelper.StandardPointToPDFPoint(new Point((mouseStartPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseStartPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  106. }
  107. public Point GetEndPoint()
  108. {
  109. if (moveOffset == new Point())
  110. {
  111. return new Point(-1, -1);
  112. }
  113. else
  114. {
  115. return DpiHelper.StandardPointToPDFPoint(new Point((mouseEndPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseEndPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  116. }
  117. }
  118. public double GetMoveLength()
  119. {
  120. if (mouseEndPoint == new Point())
  121. {
  122. return 0;
  123. }
  124. Point checkPoint = mouseEndPoint;
  125. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  126. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  127. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  128. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  129. Vector moveOffset = checkPoint - mouseStartPoint;
  130. return moveOffset.Length;
  131. }
  132. public List<Point> GetInkDrawPoints()
  133. {
  134. List<Point> points = new List<Point>
  135. {
  136. new Point((mouseStartPoint.X - pageBound.Left+(cropPoint.X*zoomFactor)) / zoomFactor,
  137. (mouseStartPoint.Y - pageBound.Top + (cropPoint.Y*zoomFactor)) / zoomFactor)
  138. };
  139. foreach (Point item in inkDrawPoints)
  140. {
  141. points.Add(new Point((item.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  142. (item.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor));
  143. }
  144. return points;
  145. }
  146. public List<Point> GetMeasureDrawPoints()
  147. {
  148. List<Point> points = new List<Point>();
  149. foreach (Point item in drawPoints)
  150. {
  151. points.Add(new Point((item.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  152. (item.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor));
  153. }
  154. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  155. {
  156. if (points.Count == 2)
  157. {
  158. Rect rect = new Rect(points[0], points[1]);
  159. points.Clear();
  160. points.Add(rect.TopLeft);
  161. points.Add(rect.BottomLeft);
  162. points.Add(rect.BottomRight);
  163. points.Add(rect.TopRight);
  164. }
  165. else if (points.Count == 1)
  166. {
  167. Rect checkRect = pageBound;
  168. // Current drawing during the movement process.
  169. Point checkPoint = mouseEndPoint;
  170. checkPoint.X = Math.Max(checkRect.Left, checkPoint.X);
  171. checkPoint.X = Math.Min(checkRect.Right, checkPoint.X);
  172. checkPoint.Y = Math.Max(checkRect.Top, checkPoint.Y);
  173. checkPoint.Y = Math.Min(checkRect.Bottom, checkPoint.Y);
  174. List<Point> drawPointsList = new List<Point>
  175. {
  176. new Point((drawPoints[0].X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  177. (drawPoints[0].Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor),
  178. new Point((checkPoint.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  179. (checkPoint.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor)
  180. };
  181. Rect rect = new Rect(drawPointsList[0], drawPointsList[1]);
  182. points.Clear();
  183. points.Add(rect.TopLeft);
  184. points.Add(rect.BottomLeft);
  185. points.Add(rect.BottomRight);
  186. points.Add(rect.TopRight);
  187. }
  188. }
  189. return points;
  190. }
  191. public void SetIsProportionalScaling(bool isOpen)
  192. {
  193. isProportionalScaling = isOpen;
  194. }
  195. #region Draw
  196. public CPDFAnnotation StartDraw(Point downPoint, Point cropPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, C_ANNOTATION_TYPE annotType, CPDFViewer viewer, double zoom)
  197. {
  198. RemoveTextBox();
  199. mouseStartPoint = downPoint;
  200. mouseEndPoint = downPoint;
  201. isDrawAnnot = true;
  202. this.maxRect = maxRect;
  203. zoomFactor = zoom;
  204. moveOffset = new Point();
  205. int newIndex = cPDFPage.GetAnnotCount();
  206. cPDFAnnotation = cPDFPage.CreateAnnot(annotType);
  207. if (cPDFAnnotation != null)
  208. {
  209. cPDFAnnotation.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  210. cPDFAnnotation.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  211. List<CPDFAnnotation> annotList = cPDFPage.GetAnnotations();
  212. cPDFAnnotation = annotList[newIndex];
  213. cPDFViewer = viewer;
  214. drawPoints.Add(downPoint);
  215. this.cropPoint = cropPoint;
  216. this.pageBound = pageBound;
  217. this.pageIndex = cPDFPage.PageIndex;
  218. DPIRect = new Rect();
  219. if (annotType != C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  220. {
  221. defaultSettingParam.IsCreateSquarePolygonMeasure = false;
  222. }
  223. }
  224. return cPDFAnnotation;
  225. }
  226. public void MoveDraw(Point downPoint, double zoom)
  227. {
  228. if (isDrawAnnot)
  229. {
  230. moveOffset = new Point(
  231. mouseEndPoint.X - downPoint.X,
  232. mouseEndPoint.Y - downPoint.Y
  233. );
  234. mouseEndPoint = downPoint;
  235. zoomFactor = zoom;
  236. Draw();
  237. }
  238. }
  239. public Rect EndDraw()
  240. {
  241. if (isDrawAnnot)
  242. {
  243. if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  244. {
  245. if (DPIRect.Equals(new Rect()))
  246. {
  247. DPIRect = drawRect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
  248. }
  249. else
  250. {
  251. DPIRect = drawRect = new Rect(mouseEndPoint.X, mouseEndPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
  252. }
  253. }
  254. if (cPDFAnnotation is CPDFPolylineAnnotation)
  255. {
  256. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  257. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  258. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  259. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  260. DPIRect = new Rect(left, top, right - left, bottom - top);
  261. }
  262. Rect standardRect = new Rect(
  263. (DPIRect.Left - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (DPIRect.Top - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor,
  264. DPIRect.Width / zoomFactor, DPIRect.Height / zoomFactor);
  265. isDrawAnnot = false;
  266. freeTextPoint = new Point((mouseStartPoint.X - pageBound.X) / zoomFactor, (mouseStartPoint.Y - pageBound.Y) / zoomFactor);
  267. mouseStartPoint = new Point();
  268. mouseEndPoint = new Point();
  269. moveOffset = new Point();
  270. pageBound = new Rect();
  271. pageIndex = -1;
  272. DPIRect = new Rect();
  273. cPDFAnnotation = null;
  274. inkDrawPoints.Clear();
  275. drawPoints.Clear();
  276. return DpiHelper.StandardRectToPDFRect(standardRect);
  277. }
  278. return new Rect();
  279. }
  280. public override void Draw()
  281. {
  282. Dispatcher.Invoke(() =>
  283. {
  284. if (cPDFAnnotation == null)
  285. {
  286. return;
  287. }
  288. drawDC = Open();
  289. switch (cPDFAnnotation.Type)
  290. {
  291. case C_ANNOTATION_TYPE.C_ANNOTATION_TEXT:
  292. DPIRect = drawRect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, 32, 32);
  293. break;
  294. case C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT:
  295. DrawText();
  296. break;
  297. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  298. if ((cPDFAnnotation as CPDFLineAnnotation).IsMeasured())
  299. {
  300. DrawLineMeasure(drawDC);
  301. }
  302. else
  303. {
  304. DrawLine(drawDC);
  305. }
  306. break;
  307. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUARE:
  308. DrawSquare(drawDC);
  309. break;
  310. case C_ANNOTATION_TYPE.C_ANNOTATION_CIRCLE:
  311. DrawCircle(drawDC);
  312. break;
  313. case C_ANNOTATION_TYPE.C_ANNOTATION_INK:
  314. DrawInk(drawDC);
  315. break;
  316. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  317. DrawLink(drawDC);
  318. break;
  319. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  320. DrawPolygonMeasure(drawDC);
  321. break;
  322. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  323. DrawPolyLineMeasure(drawDC);
  324. break;
  325. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  326. DrawRedact(drawDC);
  327. break;
  328. default:
  329. break;
  330. }
  331. Present();
  332. });
  333. }
  334. public virtual void ClearDraw()
  335. {
  336. RemoveTextBox();
  337. Open();
  338. Present();
  339. }
  340. private void DrawCircle(DrawingContext drawingContext)
  341. {
  342. CPDFCircleAnnotation circleAnnot = (cPDFAnnotation as CPDFCircleAnnotation);
  343. Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(circleAnnot.LineColor[0], circleAnnot.LineColor[1], circleAnnot.LineColor[2])), (int)Math.Ceiling(circleAnnot.LineWidth / 72 * 96 * zoomFactor));
  344. SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
  345. if (circleAnnot.HasBgColor)
  346. {
  347. FillBrush = new SolidColorBrush(Color.FromRgb(circleAnnot.BgColor[0], circleAnnot.BgColor[1], circleAnnot.BgColor[2]));
  348. }
  349. DrawPen.Brush.Opacity = circleAnnot.Transparency / 255D;
  350. FillBrush.Opacity = circleAnnot.Transparency / 255D;
  351. if (circleAnnot.Dash != null && circleAnnot.Dash.Length > 0 && circleAnnot.LineWidth > 0)
  352. {
  353. DashStyle dash = new DashStyle();
  354. foreach (var offset in circleAnnot.Dash)
  355. {
  356. dash.Dashes.Add(offset / circleAnnot.LineWidth);
  357. }
  358. DrawPen.DashStyle = dash;
  359. DrawPen.DashCap = PenLineCap.Flat;
  360. }
  361. if (isProportionalScaling)
  362. {
  363. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  364. if (mouseOffset.X < 0)
  365. {
  366. if (mouseOffset.Y > 0)
  367. {
  368. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  369. }
  370. else
  371. {
  372. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  373. }
  374. }
  375. else
  376. {
  377. if (mouseOffset.Y > 0)
  378. {
  379. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  380. }
  381. else
  382. {
  383. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  384. }
  385. }
  386. }
  387. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  388. double mLeft = rect.Left;
  389. double mRight = rect.Right;
  390. double mUp = rect.Top;
  391. double mDown = rect.Bottom;
  392. if (rect.Left < maxRect.Left)
  393. {
  394. mLeft = maxRect.Left;
  395. }
  396. if (rect.Right > maxRect.Right)
  397. {
  398. mRight = maxRect.Right;
  399. }
  400. if (rect.Top < maxRect.Top)
  401. {
  402. mUp = maxRect.Top;
  403. }
  404. if (rect.Bottom > maxRect.Bottom)
  405. {
  406. mDown = maxRect.Bottom;
  407. }
  408. DPIRect = drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  409. double centerX = (drawRect.Left + drawRect.Width / 2);
  410. double centerY = (drawRect.Top + drawRect.Height / 2);
  411. double radiusX = drawRect.Width / 2 - DrawPen.Thickness;
  412. double radiusY = drawRect.Height / 2 - DrawPen.Thickness;
  413. if (radiusX <= 0 || radiusY <= 0)
  414. {
  415. drawingContext.DrawEllipse(DrawPen.Brush, null, new Point(centerX, centerY), (drawRect.Width / 2), (drawRect.Height / 2));
  416. }
  417. else
  418. {
  419. drawingContext?.DrawEllipse(null, DrawPen, new Point(centerX, centerY), radiusX, radiusY);
  420. if ((int)(drawRect.Width / 2 - DrawPen.Thickness) > 0 && (int)(drawRect.Height / 2 - DrawPen.Thickness) > 0)
  421. {
  422. drawingContext?.DrawEllipse(FillBrush, null, new Point(centerX, centerY), (drawRect.Width / 2 - DrawPen.Thickness), (drawRect.Height / 2 - DrawPen.Thickness));
  423. }
  424. }
  425. }
  426. private void DrawSquare(DrawingContext drawingContext)
  427. {
  428. CPDFSquareAnnotation squareAnnot = (cPDFAnnotation as CPDFSquareAnnotation);
  429. Pen DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(squareAnnot.LineColor[0], squareAnnot.LineColor[1], squareAnnot.LineColor[2])), (int)Math.Ceiling(squareAnnot.LineWidth / 72 * 96 * zoomFactor));
  430. SolidColorBrush FillBrush = new SolidColorBrush(Colors.Transparent);
  431. if (squareAnnot.HasBgColor)
  432. {
  433. FillBrush = new SolidColorBrush(Color.FromRgb(squareAnnot.BgColor[0], squareAnnot.BgColor[1], squareAnnot.BgColor[2]));
  434. }
  435. DrawPen.Brush.Opacity = squareAnnot.Transparency / 255D;
  436. FillBrush.Opacity = squareAnnot.Transparency / 255D;
  437. if (squareAnnot.Dash != null && squareAnnot.Dash.Length > 0 && squareAnnot.LineWidth > 0)
  438. {
  439. DashStyle dash = new DashStyle();
  440. foreach (var offset in squareAnnot.Dash)
  441. {
  442. dash.Dashes.Add(offset / squareAnnot.LineWidth);
  443. }
  444. DrawPen.DashStyle = dash;
  445. DrawPen.DashCap = PenLineCap.Flat;
  446. }
  447. if (isProportionalScaling)
  448. {
  449. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  450. if (mouseOffset.X < 0)
  451. {
  452. if (mouseOffset.Y > 0)
  453. {
  454. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  455. }
  456. else
  457. {
  458. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  459. }
  460. }
  461. else
  462. {
  463. if (mouseOffset.Y > 0)
  464. {
  465. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  466. }
  467. else
  468. {
  469. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  470. }
  471. }
  472. }
  473. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  474. double mLeft = rect.Left;
  475. double mRight = rect.Right;
  476. double mUp = rect.Top;
  477. double mDown = rect.Bottom;
  478. if (rect.Left < maxRect.Left)
  479. {
  480. mLeft = maxRect.Left;
  481. }
  482. if (rect.Right > maxRect.Right)
  483. {
  484. mRight = maxRect.Right;
  485. }
  486. if (rect.Top < maxRect.Top)
  487. {
  488. mUp = maxRect.Top;
  489. }
  490. if (rect.Bottom > maxRect.Bottom)
  491. {
  492. mDown = maxRect.Bottom;
  493. }
  494. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  495. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  496. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  497. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  498. if (drawWidth > 0 && drawHeight > 0)
  499. {
  500. drawRect = new Rect(
  501. (int)DPIRect.Left + halfPenWidth,
  502. (int)DPIRect.Top + halfPenWidth,
  503. (int)DPIRect.Width - halfPenWidth * 2,
  504. (int)DPIRect.Height - halfPenWidth * 2);
  505. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  506. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  507. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  508. {
  509. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  510. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  511. }
  512. }
  513. }
  514. private void DrawLine(DrawingContext drawingContext)
  515. {
  516. CPDFLineAnnotation annotLine = (cPDFAnnotation as CPDFLineAnnotation);
  517. Pen DrawPen = new Pen(new SolidColorBrush(
  518. Color.FromRgb(
  519. annotLine.LineColor[0],
  520. annotLine.LineColor[1],
  521. annotLine.LineColor[2])),
  522. (int)Math.Ceiling(annotLine.LineWidth == 0 ? 0.5 : annotLine.LineWidth * zoomFactor));
  523. DrawPen.Brush.Opacity = annotLine.Transparency / 255D;
  524. if (annotLine.Dash != null && annotLine.Dash.Length > 0 && annotLine.LineWidth > 0)
  525. {
  526. DashStyle dash = new DashStyle();
  527. foreach (var offset in annotLine.Dash)
  528. {
  529. dash.Dashes.Add(offset / annotLine.LineWidth);
  530. }
  531. DrawPen.DashStyle = dash;
  532. DrawPen.DashCap = PenLineCap.Flat;
  533. }
  534. ArrowHelper drawLine = new ArrowHelper();
  535. if (isProportionalScaling)
  536. {
  537. mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
  538. DPIRect = new Rect(mouseStartPoint, mouseEndPoint);
  539. drawLine.LineStart = mouseStartPoint;
  540. drawLine.LineEnd = mouseEndPoint;
  541. drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor);
  542. drawLine.StartSharp = annotLine.HeadLineType;
  543. drawLine.EndSharp = annotLine.TailLineType;
  544. drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
  545. }
  546. else
  547. {
  548. Point checkPoint = mouseEndPoint;
  549. if (mouseEndPoint.X < maxRect.Left)
  550. {
  551. checkPoint.X = maxRect.Left;
  552. }
  553. if (mouseEndPoint.X > maxRect.Right)
  554. {
  555. checkPoint.X = maxRect.Right;
  556. }
  557. if (mouseEndPoint.Y < maxRect.Top)
  558. {
  559. checkPoint.Y = maxRect.Top;
  560. }
  561. if (mouseEndPoint.Y > maxRect.Bottom)
  562. {
  563. checkPoint.Y = maxRect.Bottom;
  564. }
  565. mouseEndPoint = checkPoint;
  566. DPIRect = new Rect(mouseStartPoint, mouseEndPoint);
  567. drawLine.LineStart = mouseStartPoint;
  568. drawLine.LineEnd = mouseEndPoint;
  569. drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor);
  570. drawLine.StartSharp = annotLine.HeadLineType;
  571. drawLine.EndSharp = annotLine.TailLineType;
  572. drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
  573. }
  574. }
  575. private void DrawInk(DrawingContext drawingContext)
  576. {
  577. CPDFInkAnnotation annotLine = (cPDFAnnotation as CPDFInkAnnotation);
  578. if (annotLine == null || annotLine.IsValid() == false)
  579. {
  580. return;
  581. }
  582. byte transparent = annotLine.GetTransparency();
  583. Pen DrawPen = new Pen(new SolidColorBrush(Color.FromArgb(transparent, annotLine.InkColor[0], annotLine.InkColor[1], annotLine.InkColor[2])), annotLine.Thickness * zoomFactor);
  584. DrawPen.StartLineCap = PenLineCap.Round;
  585. DrawPen.EndLineCap = PenLineCap.Round;
  586. PathGeometry pathDraw = new PathGeometry();
  587. Point CurrentPoint = mouseEndPoint;
  588. Rect MaxRect = pageBound;
  589. if (CurrentPoint.X > MaxRect.Right)
  590. {
  591. CurrentPoint.X = MaxRect.Right;
  592. }
  593. if (CurrentPoint.X < MaxRect.Left)
  594. {
  595. CurrentPoint.X = MaxRect.Left;
  596. }
  597. if (CurrentPoint.Y > MaxRect.Bottom)
  598. {
  599. CurrentPoint.Y = MaxRect.Bottom;
  600. }
  601. if (CurrentPoint.Y < MaxRect.Top)
  602. {
  603. CurrentPoint.Y = MaxRect.Top;
  604. }
  605. inkDrawPoints.Add(CurrentPoint);
  606. pathDraw.Figures = new PathFigureCollection();
  607. PathFigure pathFigure = new PathFigure();
  608. pathDraw.Figures.Add(pathFigure);
  609. pathFigure.StartPoint = mouseStartPoint;
  610. foreach (Point addPoint in inkDrawPoints)
  611. {
  612. LineSegment lineSegment = new LineSegment(addPoint, true);
  613. lineSegment.IsSmoothJoin = true;
  614. pathFigure.Segments.Add(lineSegment);
  615. }
  616. if (annotLine.Dash != null && annotLine.Dash.Length > 0)
  617. {
  618. DashStyle dash = new DashStyle();
  619. foreach (var offset in annotLine.Dash)
  620. {
  621. dash.Dashes.Add(offset);
  622. }
  623. DrawPen.DashStyle = dash;
  624. DrawPen.DashCap = PenLineCap.Flat;
  625. }
  626. Rect checkRect = pageBound;
  627. RectangleGeometry rectGeometry = new RectangleGeometry();
  628. drawRect = rectGeometry.Rect = checkRect;
  629. drawingContext?.PushClip(rectGeometry);
  630. drawingContext?.DrawGeometry(null, DrawPen, pathDraw);
  631. }
  632. private void DrawLink(DrawingContext drawingContext)
  633. {
  634. Pen DrawPen = defaultDrawParam.LinkPen;
  635. DrawPen.Thickness *= zoomFactor;
  636. SolidColorBrush FillBrush = defaultDrawParam.LinkBrush;
  637. if (isProportionalScaling)
  638. {
  639. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  640. if (mouseOffset.X < 0)
  641. {
  642. if (mouseOffset.Y > 0)
  643. {
  644. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  645. }
  646. else
  647. {
  648. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  649. }
  650. }
  651. else
  652. {
  653. if (mouseOffset.Y > 0)
  654. {
  655. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  656. }
  657. else
  658. {
  659. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  660. }
  661. }
  662. }
  663. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  664. double mLeft = rect.Left;
  665. double mRight = rect.Right;
  666. double mUp = rect.Top;
  667. double mDown = rect.Bottom;
  668. if (rect.Left < maxRect.Left)
  669. {
  670. mLeft = maxRect.Left;
  671. }
  672. if (rect.Right > maxRect.Right)
  673. {
  674. mRight = maxRect.Right;
  675. }
  676. if (rect.Top < maxRect.Top)
  677. {
  678. mUp = maxRect.Top;
  679. }
  680. if (rect.Bottom > maxRect.Bottom)
  681. {
  682. mDown = maxRect.Bottom;
  683. }
  684. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  685. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  686. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  687. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  688. if (drawWidth > 0 && drawHeight > 0)
  689. {
  690. drawRect = new Rect(
  691. (int)DPIRect.Left + halfPenWidth,
  692. (int)DPIRect.Top + halfPenWidth,
  693. (int)DPIRect.Width - halfPenWidth * 2,
  694. (int)DPIRect.Height - halfPenWidth * 2);
  695. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  696. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  697. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  698. {
  699. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  700. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  701. }
  702. }
  703. }
  704. private void DrawRedact(DrawingContext drawingContext)
  705. {
  706. Pen DrawPen = defaultDrawParam.RedactPen;
  707. if (cPDFAnnotation != null && cPDFAnnotation.IsValid() && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  708. {
  709. CPDFRedactAnnotation redactAnnot = cPDFAnnotation as CPDFRedactAnnotation;
  710. if (redactAnnot.OutlineColor != null && redactAnnot.OutlineColor.Length == 3)
  711. {
  712. DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(redactAnnot.OutlineColor[0], redactAnnot.OutlineColor[1], redactAnnot.OutlineColor[2])), DrawPen.Thickness);
  713. }
  714. }
  715. SolidColorBrush FillBrush = new SolidColorBrush(Color.FromArgb(0x46, 0x46, 0x82, 0xB4));
  716. if (isProportionalScaling)
  717. {
  718. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  719. if (mouseOffset.X < 0)
  720. {
  721. if (mouseOffset.Y > 0)
  722. {
  723. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  724. }
  725. else
  726. {
  727. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  728. }
  729. }
  730. else
  731. {
  732. if (mouseOffset.Y > 0)
  733. {
  734. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  735. }
  736. else
  737. {
  738. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  739. }
  740. }
  741. }
  742. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  743. double mLeft = rect.Left;
  744. double mRight = rect.Right;
  745. double mUp = rect.Top;
  746. double mDown = rect.Bottom;
  747. if (rect.Left < maxRect.Left)
  748. {
  749. mLeft = maxRect.Left;
  750. }
  751. if (rect.Right > maxRect.Right)
  752. {
  753. mRight = maxRect.Right;
  754. }
  755. if (rect.Top < maxRect.Top)
  756. {
  757. mUp = maxRect.Top;
  758. }
  759. if (rect.Bottom > maxRect.Bottom)
  760. {
  761. mDown = maxRect.Bottom;
  762. }
  763. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  764. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  765. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  766. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  767. if (drawWidth > 0 && drawHeight > 0)
  768. {
  769. drawRect = new Rect(
  770. (int)DPIRect.Left + halfPenWidth,
  771. (int)DPIRect.Top + halfPenWidth,
  772. (int)DPIRect.Width - halfPenWidth * 2,
  773. (int)DPIRect.Height - halfPenWidth * 2);
  774. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  775. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  776. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  777. {
  778. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  779. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  780. }
  781. }
  782. }
  783. private void DrawPolyLineMeasure(DrawingContext drawingContext)
  784. {
  785. CPDFPolylineAnnotation polyLine = (cPDFAnnotation as CPDFPolylineAnnotation);
  786. byte[] bytes = polyLine.LineColor;
  787. Color color = ParamConverter.ConverterByteForColor(bytes);
  788. color.A = polyLine.GetTransparency();
  789. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  790. SolidColorBrush TextBrush = Brushes.Red;
  791. if (polyLine.IsMeasured())
  792. {
  793. CPDFPerimeterMeasure measureInfo = polyLine.GetPerimeterMeasure();
  794. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  795. {
  796. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  797. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  798. }
  799. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  800. {
  801. DashStyle dash = new DashStyle();
  802. foreach (var offset in polyLine.Dash)
  803. {
  804. dash.Dashes.Add(offset / polyLine.LineWidth);
  805. }
  806. DrawPen.DashStyle = dash;
  807. DrawPen.DashCap = PenLineCap.Flat;
  808. }
  809. }
  810. if (isProportionalScaling)
  811. {
  812. if (drawPoints != null && drawPoints.Count > 0)
  813. {
  814. mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
  815. }
  816. }
  817. Point checkPoint = mouseEndPoint;
  818. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  819. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  820. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  821. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  822. if (drawPoints.Count > 0)
  823. {
  824. PathGeometry drawPath = new PathGeometry();
  825. PathFigure drawFigure = new PathFigure();
  826. drawFigure.StartPoint = drawPoints[0];
  827. PolyLineSegment polySegment = new PolyLineSegment();
  828. for (int i = 1; i < drawPoints.Count; i++)
  829. {
  830. polySegment.Points.Add(drawPoints[i]);
  831. }
  832. polySegment.Points.Add(checkPoint);
  833. if (polySegment.Points.Count > 0)
  834. {
  835. drawFigure.Segments.Add(polySegment);
  836. }
  837. if (drawFigure.Segments.Count > 0)
  838. {
  839. drawPath.Figures.Add(drawFigure);
  840. }
  841. double totalInch = 0;
  842. if (drawPoints.Count > 1)
  843. {
  844. for (int i = 0; i < drawPoints.Count - 1; i++)
  845. {
  846. totalInch += measureSetting.GetMeasureLength(drawPoints[i], drawPoints[i + 1], zoomFactor);
  847. }
  848. }
  849. double currentInch = measureSetting.GetMeasureLength(drawPoints[drawPoints.Count - 1], checkPoint, zoomFactor);
  850. totalInch += currentInch;
  851. drawingContext?.DrawGeometry(null, DrawPen, drawPath);
  852. Point closePoint = drawPoints[drawPoints.Count - 1];
  853. Vector movevector = checkPoint - closePoint;
  854. FormattedText moveText = new FormattedText(
  855. string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
  856. CultureInfo.GetCultureInfo("en-us"),
  857. FlowDirection.LeftToRight,
  858. new Typeface("YaHei"),
  859. 16,
  860. TextBrush);
  861. FormattedText totalText = new FormattedText(
  862. string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
  863. CultureInfo.GetCultureInfo("en-us"),
  864. FlowDirection.LeftToRight,
  865. new Typeface("YaHei"),
  866. 16,
  867. TextBrush);
  868. if (movevector.Length > moveText.Width + textPadding)
  869. {
  870. if (checkPoint.X >= closePoint.X)
  871. {
  872. Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
  873. Point drawPoint = new Point(
  874. linePoint.X - moveText.Width - textPadding,
  875. linePoint.Y - moveText.Height);
  876. Vector anglevector = linePoint - closePoint;
  877. RotateTransform transform = new RotateTransform();
  878. transform.CenterX = closePoint.X;
  879. transform.CenterY = closePoint.Y;
  880. double angle = Vector.AngleBetween(movevector, anglevector);
  881. transform.Angle = -angle;
  882. drawingContext?.PushTransform(transform);
  883. drawingContext?.DrawText(moveText, drawPoint);
  884. if (totalInch > currentInch)
  885. {
  886. drawingContext?.DrawText(totalText, new Point(
  887. drawPoint.X + moveText.Width + textPadding * 2,
  888. drawPoint.Y
  889. ));
  890. }
  891. drawingContext.Pop();
  892. }
  893. else
  894. {
  895. Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
  896. Point drawPoint = new Point(
  897. linePoint.X + textPadding,
  898. linePoint.Y - moveText.Height);
  899. Vector anglevector = linePoint - closePoint;
  900. RotateTransform transform = new RotateTransform();
  901. transform.CenterX = closePoint.X;
  902. transform.CenterY = closePoint.Y;
  903. double angle = Vector.AngleBetween(movevector, anglevector);
  904. transform.Angle = -angle;
  905. drawingContext?.PushTransform(transform);
  906. drawingContext?.DrawText(moveText, drawPoint);
  907. if (totalInch > currentInch)
  908. {
  909. drawingContext?.DrawText(totalText,
  910. new Point(
  911. drawPoint.X - totalText.Width - textPadding * 2,
  912. drawPoint.Y
  913. ));
  914. }
  915. drawingContext.Pop();
  916. }
  917. }
  918. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  919. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  920. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  921. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  922. DPIRect = new Rect(left, top, right - left, bottom - top);
  923. MeasureEventArgs measureEvent = new MeasureEventArgs();
  924. if (drawPoints.Count < 2)
  925. {
  926. measureEvent.Angle = 0;
  927. }
  928. else
  929. {
  930. Vector standVector = drawPoints[drawPoints.Count - 1] - drawPoints[drawPoints.Count - 2];
  931. Vector endvector = closePoint - checkPoint;
  932. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
  933. }
  934. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  935. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  936. measureEvent.RulerBase = measureSetting.RulerBase;
  937. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  938. measureEvent.Precision = measureSetting.Precision;
  939. measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
  940. measureEvent.Distance = totalText.Text;
  941. MeasureChanged?.Invoke(this, measureEvent);
  942. }
  943. }
  944. private void DrawPolygonMeasure(DrawingContext drawingContext)
  945. {
  946. CPDFPolygonAnnotation polygonAnnot = (cPDFAnnotation as CPDFPolygonAnnotation);
  947. byte[] bytes = polygonAnnot.LineColor;
  948. Color color = ParamConverter.ConverterByteForColor(bytes);
  949. color.A = polygonAnnot.GetTransparency();
  950. Pen DrawPen = new Pen(new SolidColorBrush(color), polygonAnnot.GetBorderWidth());
  951. Pen EndDrawPen = new Pen(Brushes.Black, polygonAnnot.GetBorderWidth());
  952. SolidColorBrush TextBrush = Brushes.Red;
  953. if (polygonAnnot.IsMeasured())
  954. {
  955. CPDFAreaMeasure measureInfo = polygonAnnot.GetAreaMeasure();
  956. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  957. {
  958. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  959. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  960. }
  961. if (polygonAnnot.Dash != null && polygonAnnot.Dash.Length > 0)
  962. {
  963. DashStyle dash = new DashStyle();
  964. foreach (var offset in polygonAnnot.Dash)
  965. {
  966. dash.Dashes.Add(offset / polygonAnnot.LineWidth);
  967. }
  968. DrawPen.DashStyle = dash;
  969. DrawPen.DashCap = PenLineCap.Flat;
  970. }
  971. }
  972. if (isProportionalScaling)
  973. {
  974. if (drawPoints != null && drawPoints.Count > 0)
  975. {
  976. mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
  977. }
  978. }
  979. Point checkPoint = mouseEndPoint;
  980. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  981. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  982. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  983. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  984. PointCollection points = drawPoints.Clone();
  985. if (defaultSettingParam.IsCreateSquarePolygonMeasure && drawPoints.Count == 1)
  986. {
  987. Point star = points[0];
  988. Rect rect = new Rect(star, checkPoint);
  989. points.Clear();
  990. points.Add(rect.TopLeft);
  991. points.Add(rect.BottomLeft);
  992. points.Add(rect.BottomRight);
  993. points.Add(rect.TopRight);
  994. }
  995. if (points.Count > 0)
  996. {
  997. CPDFBorderEffector borderEffector = polygonAnnot.GetAnnotBorderEffector();
  998. if (borderEffector != null && borderEffector.BorderIntensity != C_BORDER_INTENSITY.C_INTENSITY_ZERO && borderEffector.BorderType != C_BORDER_TYPE.C_BORDER_TYPE_STRAIGHT)
  999. {
  1000. //Draw the example line connected by the start point and the end point.
  1001. if (points.Count == 1)
  1002. {
  1003. Pen dashedPen = new Pen(Brushes.Gray, 1);
  1004. dashedPen.DashStyle = new DashStyle(new double[] { 2, 2 }, 0);
  1005. drawingContext?.DrawLine(dashedPen, points[0], checkPoint);
  1006. }
  1007. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  1008. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  1009. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  1010. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  1011. DPIRect = new Rect(left, top, right - left, bottom - top);
  1012. polygonAnnot.SetAnnotBorderEffector(borderEffector);
  1013. drawPoints.Add(checkPoint);
  1014. List<Point> measurePoint = new List<Point>();
  1015. measurePoint = GetMeasureDrawPoints();
  1016. drawPoints.RemoveAt(drawPoints.Count - 1);
  1017. List<CPoint> cPoints = new List<CPoint>();
  1018. foreach (Point item in measurePoint)
  1019. {
  1020. cPoints.Add(DataConversionForWPF.PointConversionForCPoint(DpiHelper.StandardPointToPDFPoint(item)));
  1021. }
  1022. polygonAnnot.SetPoints(cPoints);
  1023. polygonAnnot.UpdateAp();
  1024. cPDFViewer.UpdateAnnotFrame();
  1025. }
  1026. else
  1027. {
  1028. PathGeometry drawPath = new PathGeometry();
  1029. PathFigure drawFigure = new PathFigure();
  1030. drawFigure.StartPoint = points[0];
  1031. PolyLineSegment polySegment = new PolyLineSegment();
  1032. for (int i = 1; i < points.Count; i++)
  1033. {
  1034. polySegment.Points.Add(points[i]);
  1035. }
  1036. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1037. {
  1038. polySegment.Points.Add(points[0]);
  1039. }
  1040. else
  1041. {
  1042. //Add the current point during the movement.
  1043. polySegment.Points.Add(checkPoint);
  1044. }
  1045. if (polySegment.Points.Count > 0)
  1046. {
  1047. drawFigure.Segments.Add(polySegment);
  1048. }
  1049. if (drawFigure.Segments.Count > 0)
  1050. {
  1051. drawPath.Figures.Add(drawFigure);
  1052. }
  1053. //Draw the line segment.
  1054. drawingContext?.DrawGeometry(null, DrawPen, drawPath);
  1055. //Draw the example line connected by the start point and the end point.
  1056. if (points.Count > 1)
  1057. {
  1058. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1059. {
  1060. drawingContext?.DrawLine(DrawPen, points[0], polySegment.Points.Last());
  1061. }
  1062. else
  1063. {
  1064. drawingContext?.DrawLine(EndDrawPen, points[0], polySegment.Points.Last());
  1065. }
  1066. }
  1067. //Calculate the length.
  1068. double totalInch = 0;
  1069. if (points.Count > 1)
  1070. {
  1071. for (int i = 0; i < points.Count - 1; i++)
  1072. {
  1073. totalInch += measureSetting.GetMeasureLength(points[i], points[i + 1], zoomFactor);
  1074. }
  1075. }
  1076. double currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], checkPoint, zoomFactor);
  1077. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1078. {
  1079. currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], points[0], zoomFactor);
  1080. }
  1081. totalInch += currentInch;
  1082. Point closePoint = points[points.Count - 1];
  1083. Vector movevector = checkPoint - closePoint;
  1084. if (polygonAnnot.IsMeasured())
  1085. {
  1086. FormattedText moveText = new FormattedText(
  1087. string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
  1088. CultureInfo.GetCultureInfo("en-us"),
  1089. FlowDirection.LeftToRight,
  1090. new Typeface("YaHei"),
  1091. 16,
  1092. TextBrush);
  1093. FormattedText totalText = new FormattedText(
  1094. string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
  1095. CultureInfo.GetCultureInfo("en-us"),
  1096. FlowDirection.LeftToRight,
  1097. new Typeface("YaHei"),
  1098. 16,
  1099. TextBrush);
  1100. //Judge the text display form.
  1101. if (movevector.Length > moveText.Width + textPadding || defaultSettingParam.IsCreateSquarePolygonMeasure)
  1102. {
  1103. if (checkPoint.X >= closePoint.X)
  1104. {
  1105. Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
  1106. Point drawPoint = new Point(
  1107. linePoint.X - moveText.Width - textPadding,
  1108. linePoint.Y - moveText.Height);
  1109. Vector anglevector = linePoint - closePoint;
  1110. RotateTransform transform = new RotateTransform();
  1111. transform.CenterX = closePoint.X;
  1112. transform.CenterY = closePoint.Y;
  1113. double angle = Vector.AngleBetween(movevector, anglevector);
  1114. transform.Angle = -angle;
  1115. drawingContext?.PushTransform(transform);
  1116. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1117. {
  1118. drawingContext?.DrawText(moveText, drawPoint);
  1119. }
  1120. if (totalInch > currentInch)
  1121. {
  1122. drawingContext?.DrawText(totalText, new Point(
  1123. drawPoint.X + moveText.Width + textPadding * 2,
  1124. drawPoint.Y
  1125. ));
  1126. }
  1127. drawingContext.Pop();
  1128. }
  1129. else
  1130. {
  1131. Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
  1132. Point drawPoint = new Point(
  1133. linePoint.X + textPadding,
  1134. linePoint.Y - moveText.Height);
  1135. Vector anglevector = linePoint - closePoint;
  1136. RotateTransform transform = new RotateTransform();
  1137. transform.CenterX = closePoint.X;
  1138. transform.CenterY = closePoint.Y;
  1139. double angle = Vector.AngleBetween(movevector, anglevector);
  1140. transform.Angle = -angle;
  1141. drawingContext?.PushTransform(transform);
  1142. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1143. {
  1144. drawingContext?.DrawText(moveText, drawPoint);
  1145. }
  1146. if (totalInch > currentInch)
  1147. {
  1148. drawingContext?.DrawText(totalText,
  1149. new Point(
  1150. drawPoint.X - totalText.Width - textPadding * 2,
  1151. drawPoint.Y
  1152. ));
  1153. }
  1154. drawingContext.Pop();
  1155. }
  1156. }
  1157. }
  1158. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1159. {
  1160. double deleft = points.AsEnumerable().Select(x => x.X).Min();
  1161. double deright = points.AsEnumerable().Select(x => x.X).Max();
  1162. double detop = points.AsEnumerable().Select(x => x.Y).Min();
  1163. double debottom = points.AsEnumerable().Select(x => x.Y).Max();
  1164. DPIRect = new Rect(deleft, detop, deright - deleft, debottom - detop);
  1165. }
  1166. else
  1167. {
  1168. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  1169. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  1170. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  1171. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  1172. DPIRect = new Rect(left, top, right - left, bottom - top);
  1173. }
  1174. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1175. if (points.Count < 2)
  1176. {
  1177. measureEvent.Angle = 0;
  1178. }
  1179. else
  1180. {
  1181. Vector standVector = points[points.Count - 1] - points[points.Count - 2];
  1182. Vector endvector = closePoint - checkPoint;
  1183. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
  1184. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1185. {
  1186. measureEvent.Angle = 90;
  1187. }
  1188. }
  1189. List<Point> pon = new List<Point>();
  1190. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1191. {
  1192. points.Add(checkPoint);
  1193. }
  1194. foreach (Point drawPoint in points)
  1195. {
  1196. Point savePoint = new Point(
  1197. (drawPoint.X - pageBound.Left) + cropPoint.X,
  1198. (drawPoint.Y - pageBound.Top) + cropPoint.Y);
  1199. pon.Add(DpiHelper.StandardPointToPDFPoint(new Point(
  1200. (float)drawPoint.X / zoomFactor,
  1201. (float)drawPoint.Y / zoomFactor
  1202. )));
  1203. }
  1204. double area = measureSetting.ComputePolygonArea(pon.ToList());
  1205. double ratio = measureSetting.GetMeasureAreaRatio();
  1206. double rate = measureSetting.RulerTranslate / measureSetting.RulerBase;
  1207. double inch = area * ratio * ratio * rate * rate;
  1208. //measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1209. //measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1210. //measureEvent.RulerBase = measureSetting.RulerBase;
  1211. //measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1212. //measureEvent.Precision = measureSetting.Precision;
  1213. //measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  1214. //measureEvent.Distance = totalText.Text;
  1215. // measureEvent.Area = string.Format("{0} sq {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit);
  1216. MeasureChanged?.Invoke(this, measureEvent);
  1217. }
  1218. }
  1219. }
  1220. private void DrawLineMeasure(DrawingContext drawingContext)
  1221. {
  1222. CPDFLineAnnotation polyLine = (cPDFAnnotation as CPDFLineAnnotation);
  1223. byte[] bytes = polyLine.LineColor;
  1224. Color color = ParamConverter.ConverterByteForColor(bytes);
  1225. color.A = polyLine.GetTransparency();
  1226. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  1227. SolidColorBrush TextBrush = Brushes.Red;
  1228. if (polyLine.IsMeasured())
  1229. {
  1230. CPDFDistanceMeasure measureInfo = polyLine.GetDistanceMeasure();
  1231. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  1232. {
  1233. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  1234. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  1235. }
  1236. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  1237. {
  1238. DashStyle dash = new DashStyle();
  1239. foreach (var offset in polyLine.Dash)
  1240. {
  1241. dash.Dashes.Add(offset / polyLine.LineWidth);
  1242. }
  1243. DrawPen.DashStyle = dash;
  1244. DrawPen.DashCap = PenLineCap.Flat;
  1245. }
  1246. }
  1247. if (isProportionalScaling)
  1248. {
  1249. mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
  1250. }
  1251. Point checkPoint = mouseEndPoint;
  1252. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  1253. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  1254. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  1255. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  1256. double inch = measureSetting.GetMeasureLength(mouseStartPoint, checkPoint, zoomFactor);
  1257. ArrowHelper drawLine = new ArrowHelper();
  1258. drawLine.LineStart = mouseStartPoint;
  1259. drawLine.LineEnd = checkPoint;
  1260. drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor * zoomFactor);
  1261. drawLine.StartSharp = polyLine.HeadLineType;
  1262. drawLine.EndSharp = polyLine.TailLineType;
  1263. drawLine.BuildArrowBody();
  1264. drawingContext?.DrawGeometry(null, DrawPen, drawLine.Body);
  1265. drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
  1266. FormattedText formattedText = new FormattedText(
  1267. string.Format("{0} {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit),
  1268. CultureInfo.GetCultureInfo("en-us"),
  1269. FlowDirection.LeftToRight,
  1270. new Typeface("YaHei"),
  1271. 16,
  1272. TextBrush);
  1273. Vector movevector = checkPoint - mouseStartPoint;
  1274. if (movevector.Length > formattedText.Width + textPadding)
  1275. {
  1276. if (checkPoint.X >= mouseStartPoint.X)
  1277. {
  1278. Point linePoint = new Point(mouseStartPoint.X + movevector.Length, mouseStartPoint.Y);
  1279. Point drawPoint = new Point(
  1280. linePoint.X - formattedText.Width - textPadding,
  1281. linePoint.Y - formattedText.Height);
  1282. Vector anglevector = linePoint - mouseStartPoint;
  1283. RotateTransform transform = new RotateTransform();
  1284. transform.CenterX = mouseStartPoint.X;
  1285. transform.CenterY = mouseStartPoint.Y;
  1286. double angle = Vector.AngleBetween(movevector, anglevector);
  1287. transform.Angle = -angle;
  1288. drawingContext?.PushTransform(transform);
  1289. drawingContext?.DrawText(formattedText, drawPoint);
  1290. drawingContext.Pop();
  1291. }
  1292. else
  1293. {
  1294. Point linePoint = new Point(mouseStartPoint.X - movevector.Length, mouseStartPoint.Y);
  1295. Point drawPoint = new Point(
  1296. linePoint.X + textPadding,
  1297. linePoint.Y - formattedText.Height);
  1298. Vector anglevector = linePoint - mouseStartPoint;
  1299. RotateTransform transform = new RotateTransform();
  1300. transform.CenterX = mouseStartPoint.X;
  1301. transform.CenterY = mouseStartPoint.Y;
  1302. double angle = Vector.AngleBetween(movevector, anglevector);
  1303. transform.Angle = -angle;
  1304. drawingContext?.PushTransform(transform);
  1305. drawingContext?.DrawText(formattedText, drawPoint);
  1306. drawingContext.Pop();
  1307. }
  1308. }
  1309. DPIRect = new Rect(mouseStartPoint, checkPoint);
  1310. if (drawPoints.Count <= 1)
  1311. {
  1312. drawPoints.Add(checkPoint);
  1313. }
  1314. else
  1315. {
  1316. drawPoints[1] = checkPoint;
  1317. }
  1318. Vector standVector = new Vector(1, 0);
  1319. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1320. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  1321. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1322. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1323. measureEvent.RulerBase = measureSetting.RulerBase;
  1324. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1325. measureEvent.Precision = measureSetting.Precision;
  1326. Vector moveVector = checkPoint - mouseStartPoint;
  1327. measureEvent.MousePos = new Point(
  1328. (int)Math.Abs(moveVector.X / zoomFactor / 96D * 72D),
  1329. (int)Math.Abs(moveVector.Y / zoomFactor / 96D * 72D));
  1330. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  1331. measureEvent.Distance = formattedText.Text;
  1332. MeasureChanged?.Invoke(this, measureEvent);
  1333. }
  1334. #endregion
  1335. public void MultipleClick(Point downPoint,int pageIndex)
  1336. {
  1337. if(pageIndex==this.pageIndex)
  1338. {
  1339. drawPoints.Add(downPoint);
  1340. }
  1341. }
  1342. public Rect GetMaxRect()
  1343. {
  1344. return maxRect;
  1345. }
  1346. public void CreateTextBox()
  1347. {
  1348. try
  1349. {
  1350. if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1351. {
  1352. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1353. TextBox textui = new TextBox();
  1354. DashedBorder textBorder = new DashedBorder();
  1355. textBorder.Child = textui;
  1356. textui.Width = 200;
  1357. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1358. byte transparency = annotFreeText.GetTransparency();
  1359. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1360. Color textColor = Color.FromArgb(
  1361. transparency,
  1362. textAttribute.FontColor[0],
  1363. textAttribute.FontColor[1],
  1364. textAttribute.FontColor[2]);
  1365. Color borderColor = Colors.Transparent;
  1366. Color backgroundColor = Colors.White;
  1367. byte[] colorArray = new byte[3];
  1368. if (annotFreeText.Transparency > 0)
  1369. {
  1370. borderColor = Color.FromRgb(annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1371. }
  1372. if (annotFreeText.HasBgColor)
  1373. {
  1374. backgroundColor = Color.FromRgb(annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1375. }
  1376. Point MousePoint = new Point((mouseStartPoint.X - pageBound.X), (mouseStartPoint.Y - pageBound.Y));
  1377. textBorder.MaxWidth = (pageBound.Width - MousePoint.X - cropPoint.X);
  1378. textBorder.MaxHeight = (pageBound.Height - MousePoint.Y - cropPoint.Y);
  1379. textui.Foreground = new SolidColorBrush(textColor);
  1380. textui.Background = new SolidColorBrush(backgroundColor);
  1381. textui.MinHeight = 40;
  1382. textui.MinWidth = 200;
  1383. textBorder.Padding = new Thickness(0);
  1384. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  1385. double rawWidth = annotFreeText.GetBorderWidth();
  1386. double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
  1387. textBorder.BorderThickness = new Thickness(drawWidth);
  1388. if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
  1389. {
  1390. //补充保存虚线样式
  1391. DoubleCollection dashCollection = new DoubleCollection();
  1392. foreach (float num in annotFreeText.Dash)
  1393. {
  1394. dashCollection.Add(num);
  1395. }
  1396. textBorder?.DrawDashBorder(true, drawWidth, rawWidth, dashCollection);
  1397. }
  1398. textui.BorderThickness = new Thickness(0);
  1399. textui.Text = annotFreeText.Content;
  1400. //string fontName = string.Empty;
  1401. //string fontFamily = string.Empty;
  1402. //CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1403. textui.FontFamily = new FontFamily(annotFreeText.FreeTextDa.FontName + ",Microsoft YaHei");
  1404. textui.AcceptsReturn = true;
  1405. textui.TextWrapping = TextWrapping.Wrap;
  1406. textui.TextAlignment = TextAlignment.Left;
  1407. switch (annotFreeText.Alignment)
  1408. {
  1409. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1410. textui.TextAlignment = TextAlignment.Left;
  1411. break;
  1412. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1413. textui.TextAlignment = TextAlignment.Right;
  1414. break;
  1415. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1416. textui.TextAlignment = TextAlignment.Center;
  1417. break;
  1418. default:
  1419. break;
  1420. }
  1421. textBorder.SetValue(Canvas.LeftProperty, mouseStartPoint.X);
  1422. textBorder.SetValue(Canvas.TopProperty, mouseStartPoint.Y);
  1423. lastTextui = textui;
  1424. lastTextBorder = textBorder;
  1425. textui.Loaded += (object sender, RoutedEventArgs e) =>
  1426. {
  1427. textui.Focus();
  1428. textui.CaretIndex = textui.Text.Length;
  1429. textui.SetValue(PopupTextAttachDataProperty, cPDFAnnotation);
  1430. UpdateAnnotHandler?.Invoke(this, false);
  1431. };
  1432. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  1433. {
  1434. CPDFAnnotation currentAnnot = textui.GetValue(PopupTextAttachDataProperty) as CPDFAnnotation;
  1435. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1436. if (currentAnnot != null && currentAnnot.IsValid())
  1437. {
  1438. CPDFFreeTextAnnotation updateFreeText = currentAnnot as CPDFFreeTextAnnotation;
  1439. if (textui.Text != string.Empty || updateFreeText.GetBorderWidth() != 0)
  1440. {
  1441. updateFreeText.SetContent(textui.Text);
  1442. Rect changeRect = new Rect(
  1443. DpiHelper.StandardNumToPDFNum(freeTextPoint.X),
  1444. DpiHelper.StandardNumToPDFNum(freeTextPoint.Y),
  1445. DpiHelper.StandardNumToPDFNum(textBorder.ActualWidth / zoomFactor),
  1446. DpiHelper.StandardNumToPDFNum(textBorder.ActualHeight / zoomFactor));
  1447. updateFreeText.SetRect(new CRect(
  1448. (float)changeRect.Left,
  1449. (float)changeRect.Bottom,
  1450. (float)changeRect.Right,
  1451. (float)changeRect.Top
  1452. ));
  1453. updateFreeText.UpdateAp();
  1454. FreeTextAnnotHistory freeTextAnnotHistory = new FreeTextAnnotHistory();
  1455. annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1456. annotParam.AnnotIndex = currentAnnot.Page.GetAnnotCount() - 1;
  1457. freeTextAnnotHistory.CurrentParam = (FreeTextParam)annotParam;
  1458. freeTextAnnotHistory.PDFDoc = cPDFViewer.GetDocument();
  1459. cPDFViewer.UndoManager.AddHistory(freeTextAnnotHistory);
  1460. UpdateAnnotHandler?.Invoke(this, true);
  1461. cPDFViewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, freeTextAnnotHistory));
  1462. freeTextPoint = new Point(0, 0);
  1463. }
  1464. else
  1465. {
  1466. updateFreeText.RemoveAnnot();
  1467. CreateFreetextCanceled?.Invoke(this, annotParam);
  1468. }
  1469. }
  1470. RemoveTextBox();
  1471. };
  1472. BaseLayer createAnnotTool = this;
  1473. if (createAnnotTool != null)
  1474. {
  1475. createAnnotTool.Children.Add(textBorder);
  1476. createAnnotTool.Arrange();
  1477. }
  1478. textui.LayoutUpdated += (object sender, EventArgs e) =>
  1479. {
  1480. createAnnotTool.Arrange();
  1481. };
  1482. }
  1483. }
  1484. catch
  1485. {
  1486. }
  1487. }
  1488. public void RemoveTextBox()
  1489. {
  1490. if (lastTextBorder == null)
  1491. {
  1492. return;
  1493. }
  1494. BaseLayer removeLayer = this;
  1495. removeLayer.Children.Remove(lastTextBorder);
  1496. }
  1497. private void DrawText()
  1498. {
  1499. if (isProportionalScaling)
  1500. {
  1501. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  1502. if (mouseOffset.X < 0)
  1503. {
  1504. if (mouseOffset.Y > 0)
  1505. {
  1506. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1507. }
  1508. else
  1509. {
  1510. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1511. }
  1512. }
  1513. else
  1514. {
  1515. if (mouseOffset.Y > 0)
  1516. {
  1517. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1518. }
  1519. else
  1520. {
  1521. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1522. }
  1523. }
  1524. }
  1525. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  1526. double mLeft = rect.Left;
  1527. double mRight = rect.Right;
  1528. double mUp = rect.Top;
  1529. double mDown = rect.Bottom;
  1530. if (rect.Left < maxRect.Left)
  1531. {
  1532. mLeft = maxRect.Left;
  1533. }
  1534. if (rect.Right > maxRect.Right)
  1535. {
  1536. mRight = maxRect.Right;
  1537. }
  1538. if (rect.Top < maxRect.Top)
  1539. {
  1540. mUp = maxRect.Top;
  1541. }
  1542. if (rect.Bottom > maxRect.Bottom)
  1543. {
  1544. mDown = maxRect.Bottom;
  1545. }
  1546. Rect drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  1547. DPIRect = drawRect;
  1548. try
  1549. {
  1550. if (lastTextui != null && lastTextBorder != null && cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1551. {
  1552. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1553. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1554. byte transparency = annotFreeText.GetTransparency();
  1555. lastTextui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1556. Color textColor = Color.FromArgb(
  1557. transparency,
  1558. textAttribute.FontColor[0],
  1559. textAttribute.FontColor[1],
  1560. textAttribute.FontColor[2]);
  1561. Color borderColor = Colors.Transparent;
  1562. Color backgroundColor = Colors.Transparent;
  1563. byte[] colorArray = new byte[3];
  1564. if (annotFreeText.Transparency > 0)
  1565. {
  1566. borderColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1567. }
  1568. if (annotFreeText.HasBgColor)
  1569. {
  1570. backgroundColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1571. }
  1572. Border parentUI = lastTextui.Parent as Border;
  1573. if (parentUI != null)
  1574. {
  1575. parentUI.SetValue(Canvas.LeftProperty, drawRect.Left);
  1576. parentUI.SetValue(Canvas.TopProperty, drawRect.Top);
  1577. // The width is incorrect
  1578. if (mouseEndPoint.X >= mouseStartPoint.X)
  1579. {
  1580. parentUI.MaxWidth = (pageBound.Right - drawRect.X - cropPoint.X);
  1581. }
  1582. else
  1583. {
  1584. parentUI.MaxWidth = (drawRect.Right - pageBound.X - cropPoint.X);
  1585. }
  1586. if (mouseEndPoint.Y >= mouseStartPoint.Y)
  1587. {
  1588. parentUI.MaxHeight = (pageBound.Bottom - drawRect.Y - cropPoint.Y);
  1589. }
  1590. else
  1591. {
  1592. parentUI.MaxHeight = (drawRect.Bottom - pageBound.Y - cropPoint.Y);
  1593. }
  1594. }
  1595. lastTextui.MinWidth = drawRect.Width;
  1596. lastTextui.MinHeight = drawRect.Height;
  1597. lastTextui.Foreground = new SolidColorBrush(textColor);
  1598. lastTextui.Background = new SolidColorBrush(backgroundColor);
  1599. lastTextBorder.Padding = new Thickness(0);
  1600. lastTextBorder.BorderBrush = new SolidColorBrush(borderColor);
  1601. double rawWidth = annotFreeText.GetBorderWidth();
  1602. double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
  1603. lastTextBorder.BorderThickness = new Thickness(drawWidth);
  1604. lastTextui.BorderThickness = new Thickness(0);
  1605. lastTextui.Text = annotFreeText.Content;
  1606. lastTextui.Opacity = annotFreeText.Transparency;
  1607. if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
  1608. {
  1609. //补充保存虚线样式
  1610. DashedBorder dashBorder = (DashedBorder)lastTextBorder;
  1611. DoubleCollection dashCollection = new DoubleCollection();
  1612. foreach (float num in annotFreeText.Dash)
  1613. {
  1614. dashCollection.Add(num);
  1615. }
  1616. dashBorder.DrawDashBorder(true, drawWidth, rawWidth, dashCollection);
  1617. }
  1618. //string fontName = string.Empty;
  1619. //string fontFamily = string.Empty;
  1620. //CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1621. lastTextui.FontFamily = new FontFamily(annotFreeText.FreeTextDa.FontName + ",Microsoft YaHei");
  1622. lastTextui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  1623. lastTextui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  1624. lastTextui.AcceptsReturn = true;
  1625. lastTextui.TextWrapping = TextWrapping.Wrap;
  1626. lastTextui.TextAlignment = TextAlignment.Left;
  1627. switch (annotFreeText.Alignment)
  1628. {
  1629. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1630. lastTextui.TextAlignment = TextAlignment.Left;
  1631. break;
  1632. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1633. lastTextui.TextAlignment = TextAlignment.Right;
  1634. break;
  1635. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1636. lastTextui.TextAlignment = TextAlignment.Center;
  1637. break;
  1638. default:
  1639. break;
  1640. }
  1641. }
  1642. }
  1643. catch
  1644. {
  1645. }
  1646. }
  1647. /// <summary>
  1648. /// Use to calculate the point drawn at a fixed angle
  1649. /// </summary>
  1650. /// <param name="currentPoint">
  1651. /// Current point
  1652. /// </param>
  1653. /// <param name="startPoint">
  1654. /// Start point
  1655. /// </param>
  1656. /// <param name="pageBound">
  1657. /// Maximum drawing area
  1658. /// </param>
  1659. /// <returns>
  1660. /// Return the calculated point
  1661. /// </returns>
  1662. internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
  1663. {
  1664. Vector angleVector = currentPoint - startPoint;
  1665. Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
  1666. Vector orignVector = originPoint - startPoint;
  1667. Rect checkRect = pageBound;
  1668. int angle = (int)Vector.AngleBetween(orignVector, angleVector);
  1669. if (angle < 0)
  1670. {
  1671. angle += 360;
  1672. }
  1673. int mod = angle % 45;
  1674. int quot = angle / 45;
  1675. Point anglePoint = currentPoint;
  1676. int rotateAngle = 0;
  1677. if (mod < 22)
  1678. {
  1679. Matrix rotateMatrix = new Matrix();
  1680. rotateAngle = quot * 45;
  1681. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1682. anglePoint = rotateMatrix.Transform(originPoint);
  1683. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1684. }
  1685. else
  1686. {
  1687. Matrix rotateMatrix = new Matrix();
  1688. rotateAngle = (quot + 1) * 45;
  1689. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1690. anglePoint = rotateMatrix.Transform(originPoint);
  1691. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1692. }
  1693. if (checkRect.Contains(anglePoint) == false)
  1694. {
  1695. switch (rotateAngle)
  1696. {
  1697. case 0:
  1698. {
  1699. anglePoint.X = startPoint.X;
  1700. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1701. }
  1702. break;
  1703. case 45:
  1704. {
  1705. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1706. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1707. anglePoint.X = startPoint.X + addValue;
  1708. anglePoint.Y = startPoint.Y - addValue;
  1709. }
  1710. break;
  1711. case 90:
  1712. {
  1713. anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1714. anglePoint.Y = startPoint.Y;
  1715. }
  1716. break;
  1717. case 135:
  1718. {
  1719. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1720. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1721. anglePoint.X = startPoint.X + addValue;
  1722. anglePoint.Y = startPoint.Y + addValue;
  1723. }
  1724. break;
  1725. case 180:
  1726. {
  1727. anglePoint.X = startPoint.X;
  1728. anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
  1729. }
  1730. break;
  1731. case 225:
  1732. {
  1733. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1734. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1735. anglePoint.X = startPoint.X - addValue;
  1736. anglePoint.Y = startPoint.Y + addValue;
  1737. }
  1738. break;
  1739. case 270:
  1740. {
  1741. anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1742. anglePoint.Y = startPoint.Y;
  1743. }
  1744. break;
  1745. case 315:
  1746. {
  1747. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1748. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1749. anglePoint.X = startPoint.X - addValue;
  1750. anglePoint.Y = startPoint.Y - addValue;
  1751. }
  1752. break;
  1753. case 360:
  1754. {
  1755. anglePoint.X = startPoint.X;
  1756. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1757. }
  1758. break;
  1759. default:
  1760. break;
  1761. }
  1762. }
  1763. return anglePoint;
  1764. }
  1765. public bool IsCreateFinshed()
  1766. {
  1767. if (cPDFAnnotation is CPDFPolygonAnnotation)
  1768. {
  1769. if (drawPoints.Count <= 2)
  1770. return false;
  1771. }
  1772. return true;
  1773. }
  1774. }
  1775. }