CreateAnnotTool.cs 82 KB

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