CreateAnnotTool.cs 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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 System.Windows.Shapes;
  32. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  33. using static ComPDFKit.Tool.Help.ImportWin32;
  34. namespace ComPDFKit.Tool.DrawTool
  35. {
  36. internal class CreateAnnotTool : CustomizeLayer
  37. {
  38. public event EventHandler<bool> UpdateAnnotHandler;
  39. public event EventHandler<AnnotParam> CreateFreetextCanceled;
  40. public event EventHandler<MeasureEventArgs> MeasureChanged;
  41. public static DependencyProperty PopupTextAttachDataProperty = DependencyProperty.Register("PopupTextAttachData", typeof(CPDFAnnotation), typeof(CPDFViewerTool));
  42. #region Attributes
  43. /// <summary>
  44. /// Indicates whether proportional scaling is required
  45. /// </summary>
  46. protected bool isProportionalScaling { get; set; } = false;
  47. /// <summary>
  48. /// Mouse start point
  49. /// </summary>
  50. protected Point mouseStartPoint { get; set; }
  51. /// <summary>
  52. /// Mouse end point
  53. /// </summary>
  54. protected Point mouseEndPoint { get; set; }
  55. /// <summary>
  56. /// Crop point
  57. /// </summary>
  58. protected Point cropPoint { get; set; }
  59. /// <summary>
  60. ///Is drawing annotation
  61. /// </summary>
  62. protected bool isDrawAnnot { get; set; }
  63. /// <summary>
  64. /// Current zoom factor
  65. /// </summary>
  66. private double zoomFactor { get; set; } = 1;
  67. /// <summary>
  68. /// Draw rectangle
  69. /// </summary>
  70. protected Rect drawRect { get; set; }
  71. /// <summary>
  72. /// The rectangle representing the maximum drawing area
  73. /// </summary>
  74. protected Rect maxRect { get; set; }
  75. /// <summary>
  76. /// The rectangle representing the original page range (calculated offset in continuous mode)
  77. /// </summary>
  78. protected Rect pageBound { get; set; }
  79. /// <summary>
  80. /// The rectangle at standard DPI (without subtracting half of the pen thickness)
  81. /// </summary>
  82. protected Rect DPIRect { get; set; }
  83. /// <summary>
  84. /// The offset value during movement
  85. /// </summary>
  86. protected Point moveOffset { get; set; } = new Point(0, 0);
  87. protected DrawingContext drawDC { get; set; }
  88. protected CPDFAnnotation cPDFAnnotation
  89. {
  90. get;
  91. set;
  92. }
  93. protected CPDFViewer cPDFViewer { get; set; }
  94. protected List<Point> inkDrawPoints = new List<Point>();
  95. /// <summary>
  96. /// The collection of points measured for annotation drawing
  97. /// </summary>
  98. protected PointCollection drawPoints { get; set; } = new PointCollection();
  99. protected double textPadding { get; set; } = 10;
  100. protected Border lastTextBorder;
  101. protected TextBox lastTextui;
  102. protected Point freeTextPoint { get; set; }
  103. protected MeasureSetting measureSetting = new MeasureSetting();
  104. protected DefaultDrawParam defaultDrawParam = new DefaultDrawParam();
  105. protected DefaultSettingParam defaultSettingParam = new DefaultSettingParam();
  106. #endregion
  107. public CreateAnnotTool(MeasureSetting setting, DefaultDrawParam drawParam, DefaultSettingParam settingParam)
  108. {
  109. measureSetting = setting;
  110. defaultDrawParam = drawParam;
  111. defaultSettingParam = settingParam;
  112. }
  113. public Point GetStartPoint()
  114. {
  115. return DpiHelper.StandardPointToPDFPoint(new Point((mouseStartPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseStartPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  116. }
  117. public Point GetEndPoint()
  118. {
  119. if (moveOffset == new Point())
  120. {
  121. return new Point(-1, -1);
  122. }
  123. else
  124. {
  125. return DpiHelper.StandardPointToPDFPoint(new Point((mouseEndPoint.X - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (mouseEndPoint.Y - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor));
  126. }
  127. }
  128. public double GetMoveLength()
  129. {
  130. if (mouseEndPoint == new Point())
  131. {
  132. return 0;
  133. }
  134. Point checkPoint = mouseEndPoint;
  135. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  136. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  137. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  138. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  139. Vector moveOffset = checkPoint - mouseStartPoint;
  140. return moveOffset.Length;
  141. }
  142. public List<Point> GetInkDrawPoints()
  143. {
  144. List<Point> points = new List<Point>
  145. {
  146. new Point((mouseStartPoint.X - pageBound.Left+(cropPoint.X*zoomFactor)) / zoomFactor,
  147. (mouseStartPoint.Y - pageBound.Top + (cropPoint.Y*zoomFactor)) / zoomFactor)
  148. };
  149. foreach (Point item in inkDrawPoints)
  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. return points;
  155. }
  156. public List<Point> GetMeasureDrawPoints()
  157. {
  158. List<Point> points = new List<Point>();
  159. foreach (Point item in drawPoints)
  160. {
  161. points.Add(new Point((item.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  162. (item.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor));
  163. }
  164. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  165. {
  166. if (points.Count == 2)
  167. {
  168. Rect rect = new Rect(points[0], points[1]);
  169. points.Clear();
  170. points.Add(rect.TopLeft);
  171. points.Add(rect.BottomLeft);
  172. points.Add(rect.BottomRight);
  173. points.Add(rect.TopRight);
  174. }
  175. else if (points.Count == 1)
  176. {
  177. Rect checkRect = pageBound;
  178. // Current drawing during the movement process.
  179. Point checkPoint = mouseEndPoint;
  180. checkPoint.X = Math.Max(checkRect.Left, checkPoint.X);
  181. checkPoint.X = Math.Min(checkRect.Right, checkPoint.X);
  182. checkPoint.Y = Math.Max(checkRect.Top, checkPoint.Y);
  183. checkPoint.Y = Math.Min(checkRect.Bottom, checkPoint.Y);
  184. List<Point> drawPointsList = new List<Point>
  185. {
  186. new Point((drawPoints[0].X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  187. (drawPoints[0].Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor),
  188. new Point((checkPoint.X - pageBound.Left + (cropPoint.X * zoomFactor)) / zoomFactor,
  189. (checkPoint.Y - pageBound.Top + (cropPoint.Y * zoomFactor)) / zoomFactor)
  190. };
  191. Rect rect = new Rect(drawPointsList[0], drawPointsList[1]);
  192. points.Clear();
  193. points.Add(rect.TopLeft);
  194. points.Add(rect.BottomLeft);
  195. points.Add(rect.BottomRight);
  196. points.Add(rect.TopRight);
  197. }
  198. }
  199. return points;
  200. }
  201. public void SetIsProportionalScaling(bool isOpen)
  202. {
  203. isProportionalScaling = isOpen;
  204. }
  205. #region Draw
  206. public CPDFAnnotation StartDraw(Point downPoint, Point cropPoint, CPDFPage cPDFPage, Rect maxRect, Rect pageBound, C_ANNOTATION_TYPE annotType, CPDFViewer viewer, double zoom)
  207. {
  208. RemoveTextBox();
  209. mouseStartPoint = downPoint;
  210. mouseEndPoint = downPoint;
  211. isDrawAnnot = true;
  212. this.maxRect = maxRect;
  213. zoomFactor = zoom;
  214. moveOffset = new Point();
  215. int newIndex = cPDFPage.GetAnnotCount();
  216. cPDFAnnotation = cPDFPage.CreateAnnot(annotType);
  217. if (cPDFAnnotation != null)
  218. {
  219. cPDFAnnotation.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  220. cPDFAnnotation.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  221. List<CPDFAnnotation> annotList = cPDFPage.GetAnnotations();
  222. cPDFAnnotation = annotList[newIndex];
  223. cPDFViewer = viewer;
  224. drawPoints.Add(downPoint);
  225. this.cropPoint = cropPoint;
  226. this.pageBound = pageBound;
  227. DPIRect = new Rect();
  228. if (annotType != C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON)
  229. {
  230. defaultSettingParam.IsCreateSquarePolygonMeasure = false;
  231. }
  232. }
  233. return cPDFAnnotation;
  234. }
  235. public void MoveDraw(Point downPoint, double zoom)
  236. {
  237. if (isDrawAnnot)
  238. {
  239. moveOffset = new Point(
  240. mouseEndPoint.X - downPoint.X,
  241. mouseEndPoint.Y - downPoint.Y
  242. );
  243. mouseEndPoint = downPoint;
  244. zoomFactor = zoom;
  245. Draw();
  246. }
  247. }
  248. public Rect EndDraw()
  249. {
  250. if (isDrawAnnot)
  251. {
  252. if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  253. {
  254. if (DPIRect.Equals(new Rect()))
  255. {
  256. DPIRect = drawRect = new Rect(mouseStartPoint.X, mouseStartPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
  257. }
  258. else
  259. {
  260. DPIRect = drawRect = new Rect(mouseEndPoint.X, mouseEndPoint.Y, 32 * zoomFactor, 32 * zoomFactor);
  261. }
  262. }
  263. Rect StandardRect = new Rect(
  264. (DPIRect.Left - pageBound.X + (cropPoint.X * zoomFactor)) / zoomFactor, (DPIRect.Top - pageBound.Y + (cropPoint.Y * zoomFactor)) / zoomFactor,
  265. DPIRect.Width / zoomFactor, DPIRect.Height / zoomFactor);
  266. isDrawAnnot = false;
  267. freeTextPoint = new Point((mouseStartPoint.X - pageBound.X) / zoomFactor, (mouseStartPoint.Y - pageBound.Y) / zoomFactor);
  268. mouseStartPoint = new Point();
  269. mouseEndPoint = new Point();
  270. moveOffset = new Point();
  271. pageBound = new Rect();
  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 polyLine = (cPDFAnnotation as CPDFPolygonAnnotation);
  947. byte[] bytes = polyLine.LineColor;
  948. Color color = ParamConverter.ConverterByteForColor(bytes);
  949. color.A = polyLine.GetTransparency();
  950. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  951. Pen EndDrawPen = new Pen(Brushes.Black, polyLine.GetBorderWidth());
  952. SolidColorBrush TextBrush = Brushes.Red;
  953. if (polyLine.IsMeasured())
  954. {
  955. CPDFAreaMeasure measureInfo = polyLine.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 (polyLine.Dash != null && polyLine.Dash.Length > 0)
  962. {
  963. DashStyle dash = new DashStyle();
  964. foreach (var offset in polyLine.Dash)
  965. {
  966. dash.Dashes.Add(offset / polyLine.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. PathGeometry drawPath = new PathGeometry();
  998. PathFigure drawFigure = new PathFigure();
  999. drawFigure.StartPoint = points[0];
  1000. PolyLineSegment polySegment = new PolyLineSegment();
  1001. for (int i = 1; i < points.Count; i++)
  1002. {
  1003. polySegment.Points.Add(points[i]);
  1004. }
  1005. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1006. {
  1007. polySegment.Points.Add(points[0]);
  1008. }
  1009. else
  1010. {
  1011. //Add the current point during the movement.
  1012. polySegment.Points.Add(checkPoint);
  1013. }
  1014. if (polySegment.Points.Count > 0)
  1015. {
  1016. drawFigure.Segments.Add(polySegment);
  1017. }
  1018. if (drawFigure.Segments.Count > 0)
  1019. {
  1020. drawPath.Figures.Add(drawFigure);
  1021. }
  1022. //Draw the line segment.
  1023. drawingContext?.DrawGeometry(null, DrawPen, drawPath);
  1024. //Draw the example line connected by the start point and the end point.
  1025. if (points.Count > 1)
  1026. {
  1027. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1028. {
  1029. drawingContext?.DrawLine(DrawPen, points[0], polySegment.Points.Last());
  1030. }
  1031. else
  1032. {
  1033. drawingContext?.DrawLine(EndDrawPen, points[0], polySegment.Points.Last());
  1034. }
  1035. }
  1036. //Calculate the length.
  1037. double totalInch = 0;
  1038. if (points.Count > 1)
  1039. {
  1040. for (int i = 0; i < points.Count - 1; i++)
  1041. {
  1042. totalInch += measureSetting.GetMeasureLength(points[i], points[i + 1], zoomFactor);
  1043. }
  1044. }
  1045. double currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], checkPoint, zoomFactor);
  1046. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1047. {
  1048. currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], points[0], zoomFactor);
  1049. }
  1050. totalInch += currentInch;
  1051. Point closePoint = points[points.Count - 1];
  1052. Vector movevector = checkPoint - closePoint;
  1053. FormattedText moveText = new FormattedText(
  1054. string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
  1055. CultureInfo.GetCultureInfo("en-us"),
  1056. FlowDirection.LeftToRight,
  1057. new Typeface("YaHei"),
  1058. 16,
  1059. TextBrush);
  1060. FormattedText totalText = new FormattedText(
  1061. string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
  1062. CultureInfo.GetCultureInfo("en-us"),
  1063. FlowDirection.LeftToRight,
  1064. new Typeface("YaHei"),
  1065. 16,
  1066. TextBrush);
  1067. //Judge the text display form.
  1068. if (movevector.Length > moveText.Width + textPadding || defaultSettingParam.IsCreateSquarePolygonMeasure)
  1069. {
  1070. if (checkPoint.X >= closePoint.X)
  1071. {
  1072. Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
  1073. Point drawPoint = new Point(
  1074. linePoint.X - moveText.Width - textPadding,
  1075. linePoint.Y - moveText.Height);
  1076. Vector anglevector = linePoint - closePoint;
  1077. RotateTransform transform = new RotateTransform();
  1078. transform.CenterX = closePoint.X;
  1079. transform.CenterY = closePoint.Y;
  1080. double angle = Vector.AngleBetween(movevector, anglevector);
  1081. transform.Angle = -angle;
  1082. drawingContext?.PushTransform(transform);
  1083. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1084. {
  1085. drawingContext?.DrawText(moveText, drawPoint);
  1086. }
  1087. if (totalInch > currentInch)
  1088. {
  1089. drawingContext?.DrawText(totalText, new Point(
  1090. drawPoint.X + moveText.Width + textPadding * 2,
  1091. drawPoint.Y
  1092. ));
  1093. }
  1094. drawingContext.Pop();
  1095. }
  1096. else
  1097. {
  1098. Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
  1099. Point drawPoint = new Point(
  1100. linePoint.X + textPadding,
  1101. linePoint.Y - moveText.Height);
  1102. Vector anglevector = linePoint - closePoint;
  1103. RotateTransform transform = new RotateTransform();
  1104. transform.CenterX = closePoint.X;
  1105. transform.CenterY = closePoint.Y;
  1106. double angle = Vector.AngleBetween(movevector, anglevector);
  1107. transform.Angle = -angle;
  1108. drawingContext?.PushTransform(transform);
  1109. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1110. {
  1111. drawingContext?.DrawText(moveText, drawPoint);
  1112. }
  1113. if (totalInch > currentInch)
  1114. {
  1115. drawingContext?.DrawText(totalText,
  1116. new Point(
  1117. drawPoint.X - totalText.Width - textPadding * 2,
  1118. drawPoint.Y
  1119. ));
  1120. }
  1121. drawingContext.Pop();
  1122. }
  1123. }
  1124. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  1125. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  1126. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  1127. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  1128. DPIRect = new Rect(left, top, right - left, bottom - top);
  1129. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1130. {
  1131. double deleft = points.AsEnumerable().Select(x => x.X).Min();
  1132. double deright = points.AsEnumerable().Select(x => x.X).Max();
  1133. double detop = points.AsEnumerable().Select(x => x.Y).Min();
  1134. double debottom = points.AsEnumerable().Select(x => x.Y).Max();
  1135. DPIRect = new Rect(deleft, detop, deright - deleft, debottom - detop);
  1136. }
  1137. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1138. if (points.Count < 2)
  1139. {
  1140. measureEvent.Angle = 0;
  1141. }
  1142. else
  1143. {
  1144. Vector standVector = points[points.Count - 1] - points[points.Count - 2];
  1145. Vector endvector = closePoint - checkPoint;
  1146. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
  1147. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1148. {
  1149. measureEvent.Angle = 90;
  1150. }
  1151. }
  1152. List<Point> pon = new List<Point>();
  1153. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1154. {
  1155. points.Add(checkPoint);
  1156. }
  1157. foreach (Point drawPoint in points)
  1158. {
  1159. Point savePoint = new Point(
  1160. (drawPoint.X - pageBound.Left) + cropPoint.X,
  1161. (drawPoint.Y - pageBound.Top) + cropPoint.Y);
  1162. pon.Add(DpiHelper.StandardPointToPDFPoint(new Point(
  1163. (float)drawPoint.X / zoomFactor,
  1164. (float)drawPoint.Y / zoomFactor
  1165. )));
  1166. }
  1167. double area = measureSetting.ComputePolygonArea(pon.ToList());
  1168. double ratio = measureSetting.GetMeasureAreaRatio();
  1169. double rate = measureSetting.RulerTranslate / measureSetting.RulerBase;
  1170. double inch = area * ratio * ratio * rate * rate;
  1171. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1172. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1173. measureEvent.RulerBase = measureSetting.RulerBase;
  1174. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1175. measureEvent.Precision = measureSetting.Precision;
  1176. measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  1177. measureEvent.Distance = totalText.Text;
  1178. measureEvent.Area = string.Format("{0} sq {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit);
  1179. MeasureChanged?.Invoke(this, measureEvent);
  1180. }
  1181. }
  1182. private void DrawLineMeasure(DrawingContext drawingContext)
  1183. {
  1184. CPDFLineAnnotation polyLine = (cPDFAnnotation as CPDFLineAnnotation);
  1185. byte[] bytes = polyLine.LineColor;
  1186. Color color = ParamConverter.ConverterByteForColor(bytes);
  1187. color.A = polyLine.GetTransparency();
  1188. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  1189. SolidColorBrush TextBrush = Brushes.Red;
  1190. if (polyLine.IsMeasured())
  1191. {
  1192. CPDFDistanceMeasure measureInfo = polyLine.GetDistanceMeasure();
  1193. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  1194. {
  1195. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  1196. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  1197. }
  1198. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  1199. {
  1200. DashStyle dash = new DashStyle();
  1201. foreach (var offset in polyLine.Dash)
  1202. {
  1203. dash.Dashes.Add(offset / polyLine.LineWidth);
  1204. }
  1205. DrawPen.DashStyle = dash;
  1206. DrawPen.DashCap = PenLineCap.Flat;
  1207. }
  1208. }
  1209. if (isProportionalScaling)
  1210. {
  1211. mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
  1212. }
  1213. Point checkPoint = mouseEndPoint;
  1214. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  1215. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  1216. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  1217. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  1218. double inch = measureSetting.GetMeasureLength(mouseStartPoint, checkPoint, zoomFactor);
  1219. ArrowHelper drawLine = new ArrowHelper();
  1220. drawLine.LineStart = mouseStartPoint;
  1221. drawLine.LineEnd = checkPoint;
  1222. drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor * zoomFactor);
  1223. drawLine.StartSharp = polyLine.HeadLineType;
  1224. drawLine.EndSharp = polyLine.TailLineType;
  1225. drawLine.BuildArrowBody();
  1226. drawingContext?.DrawGeometry(null, DrawPen, drawLine.Body);
  1227. drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
  1228. FormattedText formattedText = new FormattedText(
  1229. string.Format("{0} {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit),
  1230. CultureInfo.GetCultureInfo("en-us"),
  1231. FlowDirection.LeftToRight,
  1232. new Typeface("YaHei"),
  1233. 16,
  1234. TextBrush);
  1235. Vector movevector = checkPoint - mouseStartPoint;
  1236. if (movevector.Length > formattedText.Width + textPadding)
  1237. {
  1238. if (checkPoint.X >= mouseStartPoint.X)
  1239. {
  1240. Point linePoint = new Point(mouseStartPoint.X + movevector.Length, mouseStartPoint.Y);
  1241. Point drawPoint = new Point(
  1242. linePoint.X - formattedText.Width - textPadding,
  1243. linePoint.Y - formattedText.Height);
  1244. Vector anglevector = linePoint - mouseStartPoint;
  1245. RotateTransform transform = new RotateTransform();
  1246. transform.CenterX = mouseStartPoint.X;
  1247. transform.CenterY = mouseStartPoint.Y;
  1248. double angle = Vector.AngleBetween(movevector, anglevector);
  1249. transform.Angle = -angle;
  1250. drawingContext?.PushTransform(transform);
  1251. drawingContext?.DrawText(formattedText, drawPoint);
  1252. drawingContext.Pop();
  1253. }
  1254. else
  1255. {
  1256. Point linePoint = new Point(mouseStartPoint.X - movevector.Length, mouseStartPoint.Y);
  1257. Point drawPoint = new Point(
  1258. linePoint.X + textPadding,
  1259. linePoint.Y - formattedText.Height);
  1260. Vector anglevector = linePoint - mouseStartPoint;
  1261. RotateTransform transform = new RotateTransform();
  1262. transform.CenterX = mouseStartPoint.X;
  1263. transform.CenterY = mouseStartPoint.Y;
  1264. double angle = Vector.AngleBetween(movevector, anglevector);
  1265. transform.Angle = -angle;
  1266. drawingContext?.PushTransform(transform);
  1267. drawingContext?.DrawText(formattedText, drawPoint);
  1268. drawingContext.Pop();
  1269. }
  1270. }
  1271. DPIRect = new Rect(mouseStartPoint, checkPoint);
  1272. if (drawPoints.Count <= 1)
  1273. {
  1274. drawPoints.Add(checkPoint);
  1275. }
  1276. else
  1277. {
  1278. drawPoints[1] = checkPoint;
  1279. }
  1280. Vector standVector = new Vector(1, 0);
  1281. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1282. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  1283. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1284. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1285. measureEvent.RulerBase = measureSetting.RulerBase;
  1286. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1287. measureEvent.Precision = measureSetting.Precision;
  1288. Vector moveVector = checkPoint - mouseStartPoint;
  1289. measureEvent.MousePos = new Point(
  1290. (int)Math.Abs(moveVector.X / zoomFactor / 96D * 72D),
  1291. (int)Math.Abs(moveVector.Y / zoomFactor / 96D * 72D));
  1292. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  1293. measureEvent.Distance = formattedText.Text;
  1294. MeasureChanged?.Invoke(this, measureEvent);
  1295. }
  1296. #endregion
  1297. public void MultipleClick(Point downPoint)
  1298. {
  1299. drawPoints.Add(downPoint);
  1300. }
  1301. public Rect GetMaxRect()
  1302. {
  1303. return maxRect;
  1304. }
  1305. public void CreateTextBox()
  1306. {
  1307. try
  1308. {
  1309. if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1310. {
  1311. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1312. TextBox textui = new TextBox();
  1313. DashedBorder textBorder = new DashedBorder();
  1314. textBorder.Child = textui;
  1315. textui.Width = 200;
  1316. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1317. byte transparency = annotFreeText.GetTransparency();
  1318. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1319. Color textColor = Color.FromArgb(
  1320. transparency,
  1321. textAttribute.FontColor[0],
  1322. textAttribute.FontColor[1],
  1323. textAttribute.FontColor[2]);
  1324. Color borderColor = Colors.Transparent;
  1325. Color backgroundColor = Colors.White;
  1326. byte[] colorArray = new byte[3];
  1327. if (annotFreeText.Transparency > 0)
  1328. {
  1329. borderColor = Color.FromRgb(annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1330. }
  1331. if (annotFreeText.HasBgColor)
  1332. {
  1333. backgroundColor = Color.FromRgb(annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1334. }
  1335. Point MousePoint = new Point((mouseStartPoint.X - pageBound.X), (mouseStartPoint.Y - pageBound.Y));
  1336. textBorder.MaxWidth = (pageBound.Width - MousePoint.X - cropPoint.X);
  1337. textBorder.MaxHeight = (pageBound.Height - MousePoint.Y - cropPoint.Y);
  1338. textui.Foreground = new SolidColorBrush(textColor);
  1339. textui.Background = new SolidColorBrush(backgroundColor);
  1340. textui.MinHeight = 40;
  1341. textui.MinWidth = 200;
  1342. textBorder.Padding = new Thickness(0);
  1343. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  1344. double rawWidth = annotFreeText.GetBorderWidth();
  1345. double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
  1346. textBorder.BorderThickness = new Thickness(drawWidth);
  1347. if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
  1348. {
  1349. //补充保存虚线样式
  1350. DoubleCollection dashCollection = new DoubleCollection();
  1351. foreach (float num in annotFreeText.Dash)
  1352. {
  1353. dashCollection.Add(num);
  1354. }
  1355. textBorder?.DrawDashBorder(true, drawWidth,rawWidth, dashCollection);
  1356. }
  1357. textui.BorderThickness = new Thickness(0);
  1358. textui.Text = annotFreeText.Content;
  1359. string fontName = string.Empty;
  1360. string fontFamily = string.Empty;
  1361. CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1362. textui.FontFamily = new FontFamily(fontFamily);
  1363. textui.AcceptsReturn = true;
  1364. textui.TextWrapping = TextWrapping.Wrap;
  1365. textui.TextAlignment = TextAlignment.Left;
  1366. switch (annotFreeText.Alignment)
  1367. {
  1368. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1369. textui.TextAlignment = TextAlignment.Left;
  1370. break;
  1371. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1372. textui.TextAlignment = TextAlignment.Right;
  1373. break;
  1374. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1375. textui.TextAlignment = TextAlignment.Center;
  1376. break;
  1377. default:
  1378. break;
  1379. }
  1380. textBorder.SetValue(Canvas.LeftProperty, mouseStartPoint.X);
  1381. textBorder.SetValue(Canvas.TopProperty, mouseStartPoint.Y);
  1382. lastTextui = textui;
  1383. lastTextBorder = textBorder;
  1384. textui.Loaded += (object sender, RoutedEventArgs e) =>
  1385. {
  1386. textui.Focus();
  1387. textui.CaretIndex = textui.Text.Length;
  1388. textui.SetValue(PopupTextAttachDataProperty, cPDFAnnotation);
  1389. UpdateAnnotHandler?.Invoke(this, false);
  1390. };
  1391. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  1392. {
  1393. CPDFAnnotation currentAnnot = textui.GetValue(PopupTextAttachDataProperty) as CPDFAnnotation;
  1394. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1395. if (currentAnnot != null && currentAnnot.IsValid())
  1396. {
  1397. CPDFFreeTextAnnotation updateFreeText = currentAnnot as CPDFFreeTextAnnotation;
  1398. if (textui.Text != string.Empty || updateFreeText.GetBorderWidth() != 0)
  1399. {
  1400. updateFreeText.SetContent(textui.Text);
  1401. Rect changeRect = new Rect(
  1402. DpiHelper.StandardNumToPDFNum(freeTextPoint.X),
  1403. DpiHelper.StandardNumToPDFNum(freeTextPoint.Y),
  1404. DpiHelper.StandardNumToPDFNum(textBorder.ActualWidth / zoomFactor),
  1405. DpiHelper.StandardNumToPDFNum(textBorder.ActualHeight / zoomFactor));
  1406. updateFreeText.SetRect(new CRect(
  1407. (float)changeRect.Left,
  1408. (float)changeRect.Bottom,
  1409. (float)changeRect.Right,
  1410. (float)changeRect.Top
  1411. ));
  1412. updateFreeText.UpdateAp();
  1413. FreeTextAnnotHistory freeTextAnnotHistory = new FreeTextAnnotHistory();
  1414. annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1415. annotParam.AnnotIndex = currentAnnot.Page.GetAnnotCount() - 1;
  1416. freeTextAnnotHistory.CurrentParam = (FreeTextParam)annotParam;
  1417. freeTextAnnotHistory.PDFDoc = cPDFViewer.GetDocument();
  1418. cPDFViewer.UndoManager.AddHistory(freeTextAnnotHistory);
  1419. UpdateAnnotHandler?.Invoke(this, true);
  1420. cPDFViewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, freeTextAnnotHistory));
  1421. freeTextPoint = new Point(0, 0);
  1422. }
  1423. else
  1424. {
  1425. updateFreeText.RemoveAnnot();
  1426. CreateFreetextCanceled?.Invoke(this, annotParam);
  1427. }
  1428. }
  1429. RemoveTextBox();
  1430. };
  1431. BaseLayer createAnnotTool = this;
  1432. if (createAnnotTool != null)
  1433. {
  1434. createAnnotTool.Children.Add(textBorder);
  1435. createAnnotTool.Arrange();
  1436. }
  1437. textui.LayoutUpdated += (object sender, EventArgs e) =>
  1438. {
  1439. createAnnotTool.Arrange();
  1440. };
  1441. }
  1442. }
  1443. catch
  1444. {
  1445. }
  1446. }
  1447. public void RemoveTextBox()
  1448. {
  1449. if (lastTextBorder == null)
  1450. {
  1451. return;
  1452. }
  1453. BaseLayer removeLayer = this;
  1454. removeLayer.Children.Remove(lastTextBorder);
  1455. }
  1456. private void DrawText()
  1457. {
  1458. if (isProportionalScaling)
  1459. {
  1460. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  1461. if (mouseOffset.X < 0)
  1462. {
  1463. if (mouseOffset.Y > 0)
  1464. {
  1465. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1466. }
  1467. else
  1468. {
  1469. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1470. }
  1471. }
  1472. else
  1473. {
  1474. if (mouseOffset.Y > 0)
  1475. {
  1476. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1477. }
  1478. else
  1479. {
  1480. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1481. }
  1482. }
  1483. }
  1484. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  1485. double mLeft = rect.Left;
  1486. double mRight = rect.Right;
  1487. double mUp = rect.Top;
  1488. double mDown = rect.Bottom;
  1489. if (rect.Left < maxRect.Left)
  1490. {
  1491. mLeft = maxRect.Left;
  1492. }
  1493. if (rect.Right > maxRect.Right)
  1494. {
  1495. mRight = maxRect.Right;
  1496. }
  1497. if (rect.Top < maxRect.Top)
  1498. {
  1499. mUp = maxRect.Top;
  1500. }
  1501. if (rect.Bottom > maxRect.Bottom)
  1502. {
  1503. mDown = maxRect.Bottom;
  1504. }
  1505. Rect drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  1506. DPIRect = drawRect;
  1507. try
  1508. {
  1509. if (lastTextui != null && lastTextBorder != null && cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1510. {
  1511. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1512. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1513. byte transparency = annotFreeText.GetTransparency();
  1514. lastTextui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1515. Color textColor = Color.FromArgb(
  1516. transparency,
  1517. textAttribute.FontColor[0],
  1518. textAttribute.FontColor[1],
  1519. textAttribute.FontColor[2]);
  1520. Color borderColor = Colors.Transparent;
  1521. Color backgroundColor = Colors.Transparent;
  1522. byte[] colorArray = new byte[3];
  1523. if (annotFreeText.Transparency > 0)
  1524. {
  1525. borderColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1526. }
  1527. if (annotFreeText.HasBgColor)
  1528. {
  1529. backgroundColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1530. }
  1531. Border parentUI = lastTextui.Parent as Border;
  1532. if (parentUI != null)
  1533. {
  1534. parentUI.SetValue(Canvas.LeftProperty, drawRect.Left);
  1535. parentUI.SetValue(Canvas.TopProperty, drawRect.Top);
  1536. // The width is incorrect
  1537. if (mouseEndPoint.X >= mouseStartPoint.X)
  1538. {
  1539. parentUI.MaxWidth = (pageBound.Right - drawRect.X - cropPoint.X);
  1540. }
  1541. else
  1542. {
  1543. parentUI.MaxWidth = (drawRect.Right - pageBound.X - cropPoint.X);
  1544. }
  1545. if (mouseEndPoint.Y >= mouseStartPoint.Y)
  1546. {
  1547. parentUI.MaxHeight = (pageBound.Bottom - drawRect.Y - cropPoint.Y);
  1548. }
  1549. else
  1550. {
  1551. parentUI.MaxHeight = (drawRect.Bottom - pageBound.Y - cropPoint.Y);
  1552. }
  1553. }
  1554. lastTextui.MinWidth = drawRect.Width;
  1555. lastTextui.MinHeight = drawRect.Height;
  1556. lastTextui.Foreground = new SolidColorBrush(textColor);
  1557. lastTextui.Background = new SolidColorBrush(backgroundColor);
  1558. lastTextBorder.Padding = new Thickness(0);
  1559. lastTextBorder.BorderBrush = new SolidColorBrush(borderColor);
  1560. double rawWidth = annotFreeText.GetBorderWidth();
  1561. double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * zoomFactor);
  1562. lastTextBorder.BorderThickness = new Thickness(drawWidth);
  1563. lastTextui.BorderThickness = new Thickness(0);
  1564. lastTextui.Text = annotFreeText.Content;
  1565. lastTextui.Opacity = annotFreeText.Transparency;
  1566. if (annotFreeText.BorderStyle != C_BORDER_STYLE.BS_SOLID && annotFreeText.Dash != null && annotFreeText.Dash.Length > 0)
  1567. {
  1568. //补充保存虚线样式
  1569. DashedBorder dashBorder = (DashedBorder)lastTextBorder;
  1570. DoubleCollection dashCollection = new DoubleCollection();
  1571. foreach (float num in annotFreeText.Dash)
  1572. {
  1573. dashCollection.Add(num);
  1574. }
  1575. dashBorder.DrawDashBorder(true, drawWidth,rawWidth, dashCollection);
  1576. }
  1577. string fontName = string.Empty;
  1578. string fontFamily = string.Empty;
  1579. CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1580. lastTextui.FontFamily = new FontFamily(fontFamily);
  1581. lastTextui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  1582. lastTextui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  1583. lastTextui.AcceptsReturn = true;
  1584. lastTextui.TextWrapping = TextWrapping.Wrap;
  1585. lastTextui.TextAlignment = TextAlignment.Left;
  1586. switch (annotFreeText.Alignment)
  1587. {
  1588. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1589. lastTextui.TextAlignment = TextAlignment.Left;
  1590. break;
  1591. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1592. lastTextui.TextAlignment = TextAlignment.Right;
  1593. break;
  1594. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1595. lastTextui.TextAlignment = TextAlignment.Center;
  1596. break;
  1597. default:
  1598. break;
  1599. }
  1600. }
  1601. }
  1602. catch
  1603. {
  1604. }
  1605. }
  1606. /// <summary>
  1607. /// Use to calculate the point drawn at a fixed angle
  1608. /// </summary>
  1609. /// <param name="currentPoint">
  1610. /// Current point
  1611. /// </param>
  1612. /// <param name="startPoint">
  1613. /// Start point
  1614. /// </param>
  1615. /// <param name="pageBound">
  1616. /// Maximum drawing area
  1617. /// </param>
  1618. /// <returns>
  1619. /// Return the calculated point
  1620. /// </returns>
  1621. internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
  1622. {
  1623. Vector angleVector = currentPoint - startPoint;
  1624. Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
  1625. Vector orignVector = originPoint - startPoint;
  1626. Rect checkRect = pageBound;
  1627. int angle = (int)Vector.AngleBetween(orignVector, angleVector);
  1628. if (angle < 0)
  1629. {
  1630. angle += 360;
  1631. }
  1632. int mod = angle % 45;
  1633. int quot = angle / 45;
  1634. Point anglePoint = currentPoint;
  1635. int rotateAngle = 0;
  1636. if (mod < 22)
  1637. {
  1638. Matrix rotateMatrix = new Matrix();
  1639. rotateAngle = quot * 45;
  1640. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1641. anglePoint = rotateMatrix.Transform(originPoint);
  1642. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1643. }
  1644. else
  1645. {
  1646. Matrix rotateMatrix = new Matrix();
  1647. rotateAngle = (quot + 1) * 45;
  1648. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1649. anglePoint = rotateMatrix.Transform(originPoint);
  1650. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1651. }
  1652. if (checkRect.Contains(anglePoint) == false)
  1653. {
  1654. switch (rotateAngle)
  1655. {
  1656. case 0:
  1657. {
  1658. anglePoint.X = startPoint.X;
  1659. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1660. }
  1661. break;
  1662. case 45:
  1663. {
  1664. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1665. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1666. anglePoint.X = startPoint.X + addValue;
  1667. anglePoint.Y = startPoint.Y - addValue;
  1668. }
  1669. break;
  1670. case 90:
  1671. {
  1672. anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1673. anglePoint.Y = startPoint.Y;
  1674. }
  1675. break;
  1676. case 135:
  1677. {
  1678. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1679. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1680. anglePoint.X = startPoint.X + addValue;
  1681. anglePoint.Y = startPoint.Y + addValue;
  1682. }
  1683. break;
  1684. case 180:
  1685. {
  1686. anglePoint.X = startPoint.X;
  1687. anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
  1688. }
  1689. break;
  1690. case 225:
  1691. {
  1692. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1693. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1694. anglePoint.X = startPoint.X - addValue;
  1695. anglePoint.Y = startPoint.Y + addValue;
  1696. }
  1697. break;
  1698. case 270:
  1699. {
  1700. anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1701. anglePoint.Y = startPoint.Y;
  1702. }
  1703. break;
  1704. case 315:
  1705. {
  1706. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1707. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1708. anglePoint.X = startPoint.X - addValue;
  1709. anglePoint.Y = startPoint.Y - addValue;
  1710. }
  1711. break;
  1712. case 360:
  1713. {
  1714. anglePoint.X = startPoint.X;
  1715. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1716. }
  1717. break;
  1718. default:
  1719. break;
  1720. }
  1721. }
  1722. return anglePoint;
  1723. }
  1724. }
  1725. }