CreateAnnotTool.cs 80 KB

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