CreateAnnotTool.cs 84 KB

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