CreateAnnotTool.cs 80 KB

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