CreateAnnotTool.cs 85 KB

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