CreateAnnotTool.cs 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  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. if(annotLine.Dash!=null && annotLine.Dash.Length>0)
  615. {
  616. DashStyle dash = new DashStyle();
  617. foreach (var offset in annotLine.Dash)
  618. {
  619. dash.Dashes.Add(offset);
  620. }
  621. DrawPen.DashStyle = dash;
  622. DrawPen.DashCap = PenLineCap.Flat;
  623. }
  624. Rect checkRect = pageBound;
  625. RectangleGeometry rectGeometry = new RectangleGeometry();
  626. drawRect = rectGeometry.Rect = checkRect;
  627. drawingContext?.PushClip(rectGeometry);
  628. drawingContext?.DrawGeometry(null, DrawPen, pathDraw);
  629. }
  630. private void DrawLink(DrawingContext drawingContext)
  631. {
  632. Pen DrawPen = defaultDrawParam.LinkPen;
  633. DrawPen.Thickness *= zoomFactor;
  634. SolidColorBrush FillBrush = defaultDrawParam.LinkBrush;
  635. if (isProportionalScaling)
  636. {
  637. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  638. if (mouseOffset.X < 0)
  639. {
  640. if (mouseOffset.Y > 0)
  641. {
  642. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  643. }
  644. else
  645. {
  646. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  647. }
  648. }
  649. else
  650. {
  651. if (mouseOffset.Y > 0)
  652. {
  653. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  654. }
  655. else
  656. {
  657. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  658. }
  659. }
  660. }
  661. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  662. double mLeft = rect.Left;
  663. double mRight = rect.Right;
  664. double mUp = rect.Top;
  665. double mDown = rect.Bottom;
  666. if (rect.Left < maxRect.Left)
  667. {
  668. mLeft = maxRect.Left;
  669. }
  670. if (rect.Right > maxRect.Right)
  671. {
  672. mRight = maxRect.Right;
  673. }
  674. if (rect.Top < maxRect.Top)
  675. {
  676. mUp = maxRect.Top;
  677. }
  678. if (rect.Bottom > maxRect.Bottom)
  679. {
  680. mDown = maxRect.Bottom;
  681. }
  682. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  683. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  684. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  685. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  686. if (drawWidth > 0 && drawHeight > 0)
  687. {
  688. drawRect = new Rect(
  689. (int)DPIRect.Left + halfPenWidth,
  690. (int)DPIRect.Top + halfPenWidth,
  691. (int)DPIRect.Width - halfPenWidth * 2,
  692. (int)DPIRect.Height - halfPenWidth * 2);
  693. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  694. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  695. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  696. {
  697. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  698. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  699. }
  700. }
  701. }
  702. private void DrawRedact(DrawingContext drawingContext)
  703. {
  704. Pen DrawPen = defaultDrawParam.RedactPen;
  705. if (cPDFAnnotation != null && cPDFAnnotation.IsValid() && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  706. {
  707. CPDFRedactAnnotation redactAnnot = cPDFAnnotation as CPDFRedactAnnotation;
  708. if (redactAnnot.OutlineColor != null && redactAnnot.OutlineColor.Length == 3)
  709. {
  710. DrawPen = new Pen(new SolidColorBrush(Color.FromRgb(redactAnnot.OutlineColor[0], redactAnnot.OutlineColor[1], redactAnnot.OutlineColor[2])), DrawPen.Thickness);
  711. }
  712. }
  713. SolidColorBrush FillBrush = new SolidColorBrush(Color.FromArgb(0x46, 0x46, 0x82, 0xB4));
  714. if (isProportionalScaling)
  715. {
  716. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  717. if (mouseOffset.X < 0)
  718. {
  719. if (mouseOffset.Y > 0)
  720. {
  721. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  722. }
  723. else
  724. {
  725. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  726. }
  727. }
  728. else
  729. {
  730. if (mouseOffset.Y > 0)
  731. {
  732. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  733. }
  734. else
  735. {
  736. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  737. }
  738. }
  739. }
  740. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  741. double mLeft = rect.Left;
  742. double mRight = rect.Right;
  743. double mUp = rect.Top;
  744. double mDown = rect.Bottom;
  745. if (rect.Left < maxRect.Left)
  746. {
  747. mLeft = maxRect.Left;
  748. }
  749. if (rect.Right > maxRect.Right)
  750. {
  751. mRight = maxRect.Right;
  752. }
  753. if (rect.Top < maxRect.Top)
  754. {
  755. mUp = maxRect.Top;
  756. }
  757. if (rect.Bottom > maxRect.Bottom)
  758. {
  759. mDown = maxRect.Bottom;
  760. }
  761. DPIRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  762. int halfPenWidth = (int)Math.Ceiling(DrawPen.Thickness / 2);
  763. double drawWidth = DPIRect.Width - halfPenWidth * 2;
  764. double drawHeight = DPIRect.Height - halfPenWidth * 2;
  765. if (drawWidth > 0 && drawHeight > 0)
  766. {
  767. drawRect = new Rect(
  768. (int)DPIRect.Left + halfPenWidth,
  769. (int)DPIRect.Top + halfPenWidth,
  770. (int)DPIRect.Width - halfPenWidth * 2,
  771. (int)DPIRect.Height - halfPenWidth * 2);
  772. drawingContext?.DrawRectangle(null, DrawPen, drawRect);
  773. halfPenWidth = (int)Math.Floor(DrawPen.Thickness / 2);
  774. if (drawRect.Width - halfPenWidth * 2 > 0 && drawRect.Height - halfPenWidth * 2 > 0)
  775. {
  776. Rect innerRect = new Rect(drawRect.Left + halfPenWidth, drawRect.Top + halfPenWidth, drawRect.Width - 2 * halfPenWidth, drawRect.Height - 2 * halfPenWidth);
  777. drawingContext?.DrawRectangle(FillBrush, null, innerRect);
  778. }
  779. }
  780. }
  781. private void DrawPolyLineMeasure(DrawingContext drawingContext)
  782. {
  783. CPDFPolylineAnnotation polyLine = (cPDFAnnotation as CPDFPolylineAnnotation);
  784. byte[] bytes = polyLine.LineColor;
  785. Color color = ParamConverter.ConverterByteForColor(bytes);
  786. color.A = polyLine.GetTransparency();
  787. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  788. SolidColorBrush TextBrush = Brushes.Red;
  789. if (polyLine.IsMeasured())
  790. {
  791. CPDFPerimeterMeasure measureInfo = polyLine.GetPerimeterMeasure();
  792. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  793. {
  794. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  795. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  796. }
  797. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  798. {
  799. DashStyle dash = new DashStyle();
  800. foreach (var offset in polyLine.Dash)
  801. {
  802. dash.Dashes.Add(offset / polyLine.LineWidth);
  803. }
  804. DrawPen.DashStyle = dash;
  805. DrawPen.DashCap = PenLineCap.Flat;
  806. }
  807. }
  808. if (isProportionalScaling)
  809. {
  810. if (drawPoints != null && drawPoints.Count > 0)
  811. {
  812. mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
  813. }
  814. }
  815. Point checkPoint = mouseEndPoint;
  816. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  817. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  818. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  819. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  820. if (drawPoints.Count > 0)
  821. {
  822. PathGeometry drawPath = new PathGeometry();
  823. PathFigure drawFigure = new PathFigure();
  824. drawFigure.StartPoint = drawPoints[0];
  825. PolyLineSegment polySegment = new PolyLineSegment();
  826. for (int i = 1; i < drawPoints.Count; i++)
  827. {
  828. polySegment.Points.Add(drawPoints[i]);
  829. }
  830. polySegment.Points.Add(checkPoint);
  831. if (polySegment.Points.Count > 0)
  832. {
  833. drawFigure.Segments.Add(polySegment);
  834. }
  835. if (drawFigure.Segments.Count > 0)
  836. {
  837. drawPath.Figures.Add(drawFigure);
  838. }
  839. double totalInch = 0;
  840. if (drawPoints.Count > 1)
  841. {
  842. for (int i = 0; i < drawPoints.Count - 1; i++)
  843. {
  844. totalInch += measureSetting.GetMeasureLength(drawPoints[i], drawPoints[i + 1], zoomFactor);
  845. }
  846. }
  847. double currentInch = measureSetting.GetMeasureLength(drawPoints[drawPoints.Count - 1], checkPoint, zoomFactor);
  848. totalInch += currentInch;
  849. drawingContext?.DrawGeometry(null, DrawPen, drawPath);
  850. Point closePoint = drawPoints[drawPoints.Count - 1];
  851. Vector movevector = checkPoint - closePoint;
  852. FormattedText moveText = new FormattedText(
  853. string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
  854. CultureInfo.GetCultureInfo("en-us"),
  855. FlowDirection.LeftToRight,
  856. new Typeface("YaHei"),
  857. 16,
  858. TextBrush);
  859. FormattedText totalText = new FormattedText(
  860. string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
  861. CultureInfo.GetCultureInfo("en-us"),
  862. FlowDirection.LeftToRight,
  863. new Typeface("YaHei"),
  864. 16,
  865. TextBrush);
  866. if (movevector.Length > moveText.Width + textPadding)
  867. {
  868. if (checkPoint.X >= closePoint.X)
  869. {
  870. Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
  871. Point drawPoint = new Point(
  872. linePoint.X - moveText.Width - textPadding,
  873. linePoint.Y - moveText.Height);
  874. Vector anglevector = linePoint - closePoint;
  875. RotateTransform transform = new RotateTransform();
  876. transform.CenterX = closePoint.X;
  877. transform.CenterY = closePoint.Y;
  878. double angle = Vector.AngleBetween(movevector, anglevector);
  879. transform.Angle = -angle;
  880. drawingContext?.PushTransform(transform);
  881. drawingContext?.DrawText(moveText, drawPoint);
  882. if (totalInch > currentInch)
  883. {
  884. drawingContext?.DrawText(totalText, new Point(
  885. drawPoint.X + moveText.Width + textPadding * 2,
  886. drawPoint.Y
  887. ));
  888. }
  889. drawingContext.Pop();
  890. }
  891. else
  892. {
  893. Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
  894. Point drawPoint = new Point(
  895. linePoint.X + textPadding,
  896. linePoint.Y - moveText.Height);
  897. Vector anglevector = linePoint - closePoint;
  898. RotateTransform transform = new RotateTransform();
  899. transform.CenterX = closePoint.X;
  900. transform.CenterY = closePoint.Y;
  901. double angle = Vector.AngleBetween(movevector, anglevector);
  902. transform.Angle = -angle;
  903. drawingContext?.PushTransform(transform);
  904. drawingContext?.DrawText(moveText, drawPoint);
  905. if (totalInch > currentInch)
  906. {
  907. drawingContext?.DrawText(totalText,
  908. new Point(
  909. drawPoint.X - totalText.Width - textPadding * 2,
  910. drawPoint.Y
  911. ));
  912. }
  913. drawingContext.Pop();
  914. }
  915. }
  916. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  917. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  918. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  919. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  920. DPIRect = new Rect(left, top, right - left, bottom - top);
  921. MeasureEventArgs measureEvent = new MeasureEventArgs();
  922. if (drawPoints.Count < 2)
  923. {
  924. measureEvent.Angle = 0;
  925. }
  926. else
  927. {
  928. Vector standVector = drawPoints[drawPoints.Count - 1] - drawPoints[drawPoints.Count - 2];
  929. Vector endvector = closePoint - checkPoint;
  930. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
  931. }
  932. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  933. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  934. measureEvent.RulerBase = measureSetting.RulerBase;
  935. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  936. measureEvent.Precision = measureSetting.Precision;
  937. measureEvent.Type = CPDFMeasureType.CPDF_PERIMETER_MEASURE;
  938. measureEvent.Distance = totalText.Text;
  939. MeasureChanged?.Invoke(this, measureEvent);
  940. }
  941. }
  942. private void DrawPolygonMeasure(DrawingContext drawingContext)
  943. {
  944. CPDFPolygonAnnotation polyLine = (cPDFAnnotation as CPDFPolygonAnnotation);
  945. byte[] bytes = polyLine.LineColor;
  946. Color color = ParamConverter.ConverterByteForColor(bytes);
  947. color.A = polyLine.GetTransparency();
  948. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  949. Pen EndDrawPen = new Pen(Brushes.Black, polyLine.GetBorderWidth());
  950. SolidColorBrush TextBrush = Brushes.Red;
  951. if (polyLine.IsMeasured())
  952. {
  953. CPDFAreaMeasure measureInfo = polyLine.GetAreaMeasure();
  954. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  955. {
  956. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  957. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  958. }
  959. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  960. {
  961. DashStyle dash = new DashStyle();
  962. foreach (var offset in polyLine.Dash)
  963. {
  964. dash.Dashes.Add(offset / polyLine.LineWidth);
  965. }
  966. DrawPen.DashStyle = dash;
  967. DrawPen.DashCap = PenLineCap.Flat;
  968. }
  969. }
  970. if (isProportionalScaling)
  971. {
  972. if (drawPoints != null && drawPoints.Count > 0)
  973. {
  974. mouseEndPoint = CalcAnglePoint(mouseEndPoint, drawPoints[drawPoints.Count - 1], pageBound);
  975. }
  976. }
  977. Point checkPoint = mouseEndPoint;
  978. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  979. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  980. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  981. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  982. PointCollection points = drawPoints.Clone();
  983. if (defaultSettingParam.IsCreateSquarePolygonMeasure && drawPoints.Count == 1)
  984. {
  985. Point star = points[0];
  986. Rect rect = new Rect(star, checkPoint);
  987. points.Clear();
  988. points.Add(rect.TopLeft);
  989. points.Add(rect.BottomLeft);
  990. points.Add(rect.BottomRight);
  991. points.Add(rect.TopRight);
  992. }
  993. if (points.Count > 0)
  994. {
  995. PathGeometry drawPath = new PathGeometry();
  996. PathFigure drawFigure = new PathFigure();
  997. drawFigure.StartPoint = points[0];
  998. PolyLineSegment polySegment = new PolyLineSegment();
  999. for (int i = 1; i < points.Count; i++)
  1000. {
  1001. polySegment.Points.Add(points[i]);
  1002. }
  1003. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1004. {
  1005. polySegment.Points.Add(points[0]);
  1006. }
  1007. else
  1008. {
  1009. //Add the current point during the movement.
  1010. polySegment.Points.Add(checkPoint);
  1011. }
  1012. if (polySegment.Points.Count > 0)
  1013. {
  1014. drawFigure.Segments.Add(polySegment);
  1015. }
  1016. if (drawFigure.Segments.Count > 0)
  1017. {
  1018. drawPath.Figures.Add(drawFigure);
  1019. }
  1020. //Draw the line segment.
  1021. drawingContext?.DrawGeometry(null, DrawPen, drawPath);
  1022. //Draw the example line connected by the start point and the end point.
  1023. if (points.Count > 1)
  1024. {
  1025. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1026. {
  1027. drawingContext?.DrawLine(DrawPen, points[0], polySegment.Points.Last());
  1028. }
  1029. else
  1030. {
  1031. drawingContext?.DrawLine(EndDrawPen, points[0], polySegment.Points.Last());
  1032. }
  1033. }
  1034. //Calculate the length.
  1035. double totalInch = 0;
  1036. if (points.Count > 1)
  1037. {
  1038. for (int i = 0; i < points.Count - 1; i++)
  1039. {
  1040. totalInch += measureSetting.GetMeasureLength(points[i], points[i + 1], zoomFactor);
  1041. }
  1042. }
  1043. double currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], checkPoint, zoomFactor);
  1044. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1045. {
  1046. currentInch = measureSetting.GetMeasureLength(points[points.Count - 1], points[0], zoomFactor);
  1047. }
  1048. totalInch += currentInch;
  1049. Point closePoint = points[points.Count - 1];
  1050. Vector movevector = checkPoint - closePoint;
  1051. FormattedText moveText = new FormattedText(
  1052. string.Format("{0} {1}", measureSetting.GetPrecisionData(currentInch), measureSetting.RulerTranslateUnit),
  1053. CultureInfo.GetCultureInfo("en-us"),
  1054. FlowDirection.LeftToRight,
  1055. new Typeface("YaHei"),
  1056. 16,
  1057. TextBrush);
  1058. FormattedText totalText = new FormattedText(
  1059. string.Format("{0} {1}", measureSetting.GetPrecisionData(totalInch), measureSetting.RulerTranslateUnit),
  1060. CultureInfo.GetCultureInfo("en-us"),
  1061. FlowDirection.LeftToRight,
  1062. new Typeface("YaHei"),
  1063. 16,
  1064. TextBrush);
  1065. //Judge the text display form.
  1066. if (movevector.Length > moveText.Width + textPadding || defaultSettingParam.IsCreateSquarePolygonMeasure)
  1067. {
  1068. if (checkPoint.X >= closePoint.X)
  1069. {
  1070. Point linePoint = new Point(closePoint.X + movevector.Length, closePoint.Y);
  1071. Point drawPoint = new Point(
  1072. linePoint.X - moveText.Width - textPadding,
  1073. linePoint.Y - moveText.Height);
  1074. Vector anglevector = linePoint - closePoint;
  1075. RotateTransform transform = new RotateTransform();
  1076. transform.CenterX = closePoint.X;
  1077. transform.CenterY = closePoint.Y;
  1078. double angle = Vector.AngleBetween(movevector, anglevector);
  1079. transform.Angle = -angle;
  1080. drawingContext?.PushTransform(transform);
  1081. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1082. {
  1083. drawingContext?.DrawText(moveText, drawPoint);
  1084. }
  1085. if (totalInch > currentInch)
  1086. {
  1087. drawingContext?.DrawText(totalText, new Point(
  1088. drawPoint.X + moveText.Width + textPadding * 2,
  1089. drawPoint.Y
  1090. ));
  1091. }
  1092. drawingContext.Pop();
  1093. }
  1094. else
  1095. {
  1096. Point linePoint = new Point(closePoint.X - movevector.Length, closePoint.Y);
  1097. Point drawPoint = new Point(
  1098. linePoint.X + textPadding,
  1099. linePoint.Y - moveText.Height);
  1100. Vector anglevector = linePoint - closePoint;
  1101. RotateTransform transform = new RotateTransform();
  1102. transform.CenterX = closePoint.X;
  1103. transform.CenterY = closePoint.Y;
  1104. double angle = Vector.AngleBetween(movevector, anglevector);
  1105. transform.Angle = -angle;
  1106. drawingContext?.PushTransform(transform);
  1107. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1108. {
  1109. drawingContext?.DrawText(moveText, drawPoint);
  1110. }
  1111. if (totalInch > currentInch)
  1112. {
  1113. drawingContext?.DrawText(totalText,
  1114. new Point(
  1115. drawPoint.X - totalText.Width - textPadding * 2,
  1116. drawPoint.Y
  1117. ));
  1118. }
  1119. drawingContext.Pop();
  1120. }
  1121. }
  1122. double left = drawPoints.AsEnumerable().Select(x => x.X).Min();
  1123. double right = drawPoints.AsEnumerable().Select(x => x.X).Max();
  1124. double top = drawPoints.AsEnumerable().Select(x => x.Y).Min();
  1125. double bottom = drawPoints.AsEnumerable().Select(x => x.Y).Max();
  1126. DPIRect = new Rect(left, top, right - left, bottom - top);
  1127. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1128. {
  1129. double deleft = points.AsEnumerable().Select(x => x.X).Min();
  1130. double deright = points.AsEnumerable().Select(x => x.X).Max();
  1131. double detop = points.AsEnumerable().Select(x => x.Y).Min();
  1132. double debottom = points.AsEnumerable().Select(x => x.Y).Max();
  1133. DPIRect = new Rect(deleft, detop, deright - deleft, debottom - detop);
  1134. }
  1135. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1136. if (points.Count < 2)
  1137. {
  1138. measureEvent.Angle = 0;
  1139. }
  1140. else
  1141. {
  1142. Vector standVector = points[points.Count - 1] - points[points.Count - 2];
  1143. Vector endvector = closePoint - checkPoint;
  1144. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(endvector, standVector));
  1145. if (defaultSettingParam.IsCreateSquarePolygonMeasure)
  1146. {
  1147. measureEvent.Angle = 90;
  1148. }
  1149. }
  1150. List<Point> pon = new List<Point>();
  1151. if (!defaultSettingParam.IsCreateSquarePolygonMeasure)
  1152. {
  1153. points.Add(checkPoint);
  1154. }
  1155. foreach (Point drawPoint in points)
  1156. {
  1157. Point savePoint = new Point(
  1158. (drawPoint.X - pageBound.Left) + cropPoint.X,
  1159. (drawPoint.Y - pageBound.Top) + cropPoint.Y);
  1160. pon.Add(DpiHelper.StandardPointToPDFPoint(new Point(
  1161. (float)drawPoint.X / zoomFactor,
  1162. (float)drawPoint.Y / zoomFactor
  1163. )));
  1164. }
  1165. double area = measureSetting.ComputePolygonArea(pon.ToList());
  1166. double ratio = measureSetting.GetMeasureAreaRatio();
  1167. double rate = measureSetting.RulerTranslate / measureSetting.RulerBase;
  1168. double inch = area * ratio * ratio * rate * rate;
  1169. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1170. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1171. measureEvent.RulerBase = measureSetting.RulerBase;
  1172. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1173. measureEvent.Precision = measureSetting.Precision;
  1174. measureEvent.Type = CPDFMeasureType.CPDF_AREA_MEASURE;
  1175. measureEvent.Distance = totalText.Text;
  1176. measureEvent.Area = string.Format("{0} sq {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit);
  1177. MeasureChanged?.Invoke(this, measureEvent);
  1178. }
  1179. }
  1180. private void DrawLineMeasure(DrawingContext drawingContext)
  1181. {
  1182. CPDFLineAnnotation polyLine = (cPDFAnnotation as CPDFLineAnnotation);
  1183. byte[] bytes = polyLine.LineColor;
  1184. Color color = ParamConverter.ConverterByteForColor(bytes);
  1185. color.A = polyLine.GetTransparency();
  1186. Pen DrawPen = new Pen(new SolidColorBrush(color), polyLine.GetBorderWidth());
  1187. SolidColorBrush TextBrush = Brushes.Red;
  1188. if (polyLine.IsMeasured())
  1189. {
  1190. CPDFDistanceMeasure measureInfo = polyLine.GetDistanceMeasure();
  1191. if (measureInfo != null && measureInfo.TextAttribute != null && measureInfo.TextAttribute.FontColor != null && measureInfo.TextAttribute.FontColor.Length >= 3)
  1192. {
  1193. byte[] fontColor = measureInfo.TextAttribute.FontColor;
  1194. TextBrush = new SolidColorBrush(Color.FromRgb(fontColor[0], fontColor[1], fontColor[2]));
  1195. }
  1196. if (polyLine.Dash != null && polyLine.Dash.Length > 0)
  1197. {
  1198. DashStyle dash = new DashStyle();
  1199. foreach (var offset in polyLine.Dash)
  1200. {
  1201. dash.Dashes.Add(offset / polyLine.LineWidth);
  1202. }
  1203. DrawPen.DashStyle = dash;
  1204. DrawPen.DashCap = PenLineCap.Flat;
  1205. }
  1206. }
  1207. if (isProportionalScaling)
  1208. {
  1209. mouseEndPoint = CalcAnglePoint(mouseEndPoint, mouseStartPoint, pageBound);
  1210. }
  1211. Point checkPoint = mouseEndPoint;
  1212. checkPoint.X = Math.Max(pageBound.Left, checkPoint.X);
  1213. checkPoint.X = Math.Min(pageBound.Right, checkPoint.X);
  1214. checkPoint.Y = Math.Max(pageBound.Top, checkPoint.Y);
  1215. checkPoint.Y = Math.Min(pageBound.Bottom, checkPoint.Y);
  1216. double inch = measureSetting.GetMeasureLength(mouseStartPoint, checkPoint, zoomFactor);
  1217. ArrowHelper drawLine = new ArrowHelper();
  1218. drawLine.LineStart = mouseStartPoint;
  1219. drawLine.LineEnd = checkPoint;
  1220. drawLine.ArrowLength = (uint)Math.Max(DrawPen.Thickness * 3, 12 * zoomFactor * zoomFactor);
  1221. drawLine.StartSharp = polyLine.HeadLineType;
  1222. drawLine.EndSharp = polyLine.TailLineType;
  1223. drawLine.BuildArrowBody();
  1224. drawingContext?.DrawGeometry(null, DrawPen, drawLine.Body);
  1225. drawingContext.DrawGeometry(null, DrawPen, drawLine.BuildArrowBody());
  1226. FormattedText formattedText = new FormattedText(
  1227. string.Format("{0} {1}", measureSetting.GetPrecisionData(inch), measureSetting.RulerTranslateUnit),
  1228. CultureInfo.GetCultureInfo("en-us"),
  1229. FlowDirection.LeftToRight,
  1230. new Typeface("YaHei"),
  1231. 16,
  1232. TextBrush);
  1233. Vector movevector = checkPoint - mouseStartPoint;
  1234. if (movevector.Length > formattedText.Width + textPadding)
  1235. {
  1236. if (checkPoint.X >= mouseStartPoint.X)
  1237. {
  1238. Point linePoint = new Point(mouseStartPoint.X + movevector.Length, mouseStartPoint.Y);
  1239. Point drawPoint = new Point(
  1240. linePoint.X - formattedText.Width - textPadding,
  1241. linePoint.Y - formattedText.Height);
  1242. Vector anglevector = linePoint - mouseStartPoint;
  1243. RotateTransform transform = new RotateTransform();
  1244. transform.CenterX = mouseStartPoint.X;
  1245. transform.CenterY = mouseStartPoint.Y;
  1246. double angle = Vector.AngleBetween(movevector, anglevector);
  1247. transform.Angle = -angle;
  1248. drawingContext?.PushTransform(transform);
  1249. drawingContext?.DrawText(formattedText, drawPoint);
  1250. drawingContext.Pop();
  1251. }
  1252. else
  1253. {
  1254. Point linePoint = new Point(mouseStartPoint.X - movevector.Length, mouseStartPoint.Y);
  1255. Point drawPoint = new Point(
  1256. linePoint.X + textPadding,
  1257. linePoint.Y - formattedText.Height);
  1258. Vector anglevector = linePoint - mouseStartPoint;
  1259. RotateTransform transform = new RotateTransform();
  1260. transform.CenterX = mouseStartPoint.X;
  1261. transform.CenterY = mouseStartPoint.Y;
  1262. double angle = Vector.AngleBetween(movevector, anglevector);
  1263. transform.Angle = -angle;
  1264. drawingContext?.PushTransform(transform);
  1265. drawingContext?.DrawText(formattedText, drawPoint);
  1266. drawingContext.Pop();
  1267. }
  1268. }
  1269. DPIRect = new Rect(mouseStartPoint, checkPoint);
  1270. if (drawPoints.Count <= 1)
  1271. {
  1272. drawPoints.Add(checkPoint);
  1273. }
  1274. else
  1275. {
  1276. drawPoints[1] = checkPoint;
  1277. }
  1278. Vector standVector = new Vector(1, 0);
  1279. MeasureEventArgs measureEvent = new MeasureEventArgs();
  1280. measureEvent.Angle = (int)Math.Abs(Vector.AngleBetween(movevector, standVector));
  1281. measureEvent.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
  1282. measureEvent.RulerTranslate = measureSetting.RulerTranslate;
  1283. measureEvent.RulerBase = measureSetting.RulerBase;
  1284. measureEvent.RulerBaseUnit = measureSetting.RulerBaseUnit;
  1285. measureEvent.Precision = measureSetting.Precision;
  1286. Vector moveVector = checkPoint - mouseStartPoint;
  1287. measureEvent.MousePos = new Point(
  1288. (int)Math.Abs(moveVector.X / zoomFactor / 96D * 72D),
  1289. (int)Math.Abs(moveVector.Y / zoomFactor / 96D * 72D));
  1290. measureEvent.Type = CPDFMeasureType.CPDF_DISTANCE_MEASURE;
  1291. measureEvent.Distance = formattedText.Text;
  1292. MeasureChanged?.Invoke(this, measureEvent);
  1293. }
  1294. #endregion
  1295. public void MultipleClick(Point downPoint)
  1296. {
  1297. drawPoints.Add(downPoint);
  1298. }
  1299. public Rect GetMaxRect()
  1300. {
  1301. return maxRect;
  1302. }
  1303. public void CreateTextBox()
  1304. {
  1305. try
  1306. {
  1307. if (cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1308. {
  1309. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1310. TextBox textui = new TextBox();
  1311. Border textBorder = new Border();
  1312. textBorder.Child = textui;
  1313. textui.Width = 200;
  1314. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1315. byte transparency = annotFreeText.GetTransparency();
  1316. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1317. Color textColor = Color.FromArgb(
  1318. transparency,
  1319. textAttribute.FontColor[0],
  1320. textAttribute.FontColor[1],
  1321. textAttribute.FontColor[2]);
  1322. Color borderColor = Colors.Transparent;
  1323. Color backgroundColor = Colors.White;
  1324. byte[] colorArray = new byte[3];
  1325. if (annotFreeText.Transparency > 0)
  1326. {
  1327. borderColor = Color.FromRgb(annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1328. }
  1329. if (annotFreeText.HasBgColor)
  1330. {
  1331. backgroundColor = Color.FromRgb(annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1332. }
  1333. Point MousePoint = new Point((mouseStartPoint.X - pageBound.X), (mouseStartPoint.Y - pageBound.Y));
  1334. textBorder.MaxWidth = (pageBound.Width - MousePoint.X - cropPoint.X);
  1335. textBorder.MaxHeight = (pageBound.Height - MousePoint.Y - cropPoint.Y);
  1336. textui.Foreground = new SolidColorBrush(textColor);
  1337. textui.Background = new SolidColorBrush(backgroundColor);
  1338. textui.MinHeight = 40;
  1339. textui.MinWidth = 200;
  1340. textBorder.Padding = new Thickness(0);
  1341. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  1342. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(annotFreeText.GetBorderWidth() * zoomFactor));
  1343. textui.BorderThickness = new Thickness(0);
  1344. textui.Text = annotFreeText.Content;
  1345. string fontName = string.Empty;
  1346. string fontFamily = string.Empty;
  1347. CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1348. textui.FontFamily = new FontFamily(fontFamily);
  1349. textui.AcceptsReturn = true;
  1350. textui.TextWrapping = TextWrapping.Wrap;
  1351. textui.TextAlignment = TextAlignment.Left;
  1352. switch (annotFreeText.Alignment)
  1353. {
  1354. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1355. textui.TextAlignment = TextAlignment.Left;
  1356. break;
  1357. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1358. textui.TextAlignment = TextAlignment.Right;
  1359. break;
  1360. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1361. textui.TextAlignment = TextAlignment.Center;
  1362. break;
  1363. default:
  1364. break;
  1365. }
  1366. textBorder.SetValue(Canvas.LeftProperty, mouseStartPoint.X);
  1367. textBorder.SetValue(Canvas.TopProperty, mouseStartPoint.Y);
  1368. lastTextui = textui;
  1369. lastTextBorder = textBorder;
  1370. textui.Loaded += (object sender, RoutedEventArgs e) =>
  1371. {
  1372. textui.Focus();
  1373. textui.CaretIndex = textui.Text.Length;
  1374. textui.SetValue(PopupTextAttachDataProperty, cPDFAnnotation);
  1375. UpdateAnnotHandler?.Invoke(this, false);
  1376. };
  1377. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  1378. {
  1379. CPDFAnnotation currentAnnot = textui.GetValue(PopupTextAttachDataProperty) as CPDFAnnotation;
  1380. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1381. if (currentAnnot != null && currentAnnot.IsValid())
  1382. {
  1383. CPDFFreeTextAnnotation updateFreeText = currentAnnot as CPDFFreeTextAnnotation;
  1384. if (textui.Text != string.Empty || updateFreeText.GetBorderWidth() != 0)
  1385. {
  1386. updateFreeText.SetContent(textui.Text);
  1387. Rect changeRect = new Rect(
  1388. DpiHelper.StandardNumToPDFNum(freeTextPoint.X),
  1389. DpiHelper.StandardNumToPDFNum(freeTextPoint.Y),
  1390. DpiHelper.StandardNumToPDFNum(textBorder.ActualWidth / zoomFactor),
  1391. DpiHelper.StandardNumToPDFNum(textBorder.ActualHeight / zoomFactor));
  1392. updateFreeText.SetRect(new CRect(
  1393. (float)changeRect.Left,
  1394. (float)changeRect.Bottom,
  1395. (float)changeRect.Right,
  1396. (float)changeRect.Top
  1397. ));
  1398. updateFreeText.UpdateAp();
  1399. FreeTextAnnotHistory freeTextAnnotHistory = new FreeTextAnnotHistory();
  1400. annotParam = ParamConverter.AnnotConverter(cPDFViewer.GetDocument(), currentAnnot);
  1401. annotParam.AnnotIndex = currentAnnot.Page.GetAnnotCount() - 1;
  1402. freeTextAnnotHistory.CurrentParam = (FreeTextParam)annotParam;
  1403. freeTextAnnotHistory.PDFDoc = cPDFViewer.GetDocument();
  1404. cPDFViewer.UndoManager.AddHistory(freeTextAnnotHistory);
  1405. UpdateAnnotHandler?.Invoke(this, true);
  1406. cPDFViewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, freeTextAnnotHistory));
  1407. freeTextPoint = new Point(0, 0);
  1408. }
  1409. else
  1410. {
  1411. updateFreeText.RemoveAnnot();
  1412. CreateFreetextCanceled?.Invoke(this, annotParam);
  1413. }
  1414. }
  1415. RemoveTextBox();
  1416. };
  1417. BaseLayer createAnnotTool = this;
  1418. if (createAnnotTool != null)
  1419. {
  1420. createAnnotTool.Children.Add(textBorder);
  1421. createAnnotTool.Arrange();
  1422. }
  1423. textui.LayoutUpdated += (object sender, EventArgs e) =>
  1424. {
  1425. createAnnotTool.Arrange();
  1426. };
  1427. }
  1428. }
  1429. catch
  1430. {
  1431. }
  1432. }
  1433. public void RemoveTextBox()
  1434. {
  1435. if (lastTextBorder == null)
  1436. {
  1437. return;
  1438. }
  1439. BaseLayer removeLayer = this;
  1440. removeLayer.Children.Remove(lastTextBorder);
  1441. }
  1442. private void DrawText()
  1443. {
  1444. if (isProportionalScaling)
  1445. {
  1446. Point mouseOffset = (Point)(mouseStartPoint - mouseEndPoint);
  1447. if (mouseOffset.X < 0)
  1448. {
  1449. if (mouseOffset.Y > 0)
  1450. {
  1451. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1452. }
  1453. else
  1454. {
  1455. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1456. }
  1457. }
  1458. else
  1459. {
  1460. if (mouseOffset.Y > 0)
  1461. {
  1462. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseEndPoint.X - mouseStartPoint.X);
  1463. }
  1464. else
  1465. {
  1466. mouseEndPoint = new Point(mouseEndPoint.X, mouseStartPoint.Y + mouseStartPoint.X - mouseEndPoint.X);
  1467. }
  1468. }
  1469. }
  1470. Rect rect = new Rect(mouseStartPoint, mouseEndPoint);
  1471. double mLeft = rect.Left;
  1472. double mRight = rect.Right;
  1473. double mUp = rect.Top;
  1474. double mDown = rect.Bottom;
  1475. if (rect.Left < maxRect.Left)
  1476. {
  1477. mLeft = maxRect.Left;
  1478. }
  1479. if (rect.Right > maxRect.Right)
  1480. {
  1481. mRight = maxRect.Right;
  1482. }
  1483. if (rect.Top < maxRect.Top)
  1484. {
  1485. mUp = maxRect.Top;
  1486. }
  1487. if (rect.Bottom > maxRect.Bottom)
  1488. {
  1489. mDown = maxRect.Bottom;
  1490. }
  1491. Rect drawRect = new Rect(mLeft, mUp, mRight - mLeft, mDown - mUp);
  1492. DPIRect = drawRect;
  1493. try
  1494. {
  1495. if (lastTextui != null && lastTextBorder != null && cPDFAnnotation != null && cPDFAnnotation.Type == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  1496. {
  1497. CPDFFreeTextAnnotation annotFreeText = (cPDFAnnotation as CPDFFreeTextAnnotation);
  1498. CTextAttribute textAttribute = annotFreeText.FreeTextDa;
  1499. byte transparency = annotFreeText.GetTransparency();
  1500. lastTextui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * zoomFactor);
  1501. Color textColor = Color.FromArgb(
  1502. transparency,
  1503. textAttribute.FontColor[0],
  1504. textAttribute.FontColor[1],
  1505. textAttribute.FontColor[2]);
  1506. Color borderColor = Colors.Transparent;
  1507. Color backgroundColor = Colors.Transparent;
  1508. byte[] colorArray = new byte[3];
  1509. if (annotFreeText.Transparency > 0)
  1510. {
  1511. borderColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.LineColor[0], annotFreeText.LineColor[1], annotFreeText.LineColor[2]);
  1512. }
  1513. if (annotFreeText.HasBgColor)
  1514. {
  1515. backgroundColor = Color.FromArgb(annotFreeText.Transparency, annotFreeText.BgColor[0], annotFreeText.BgColor[1], annotFreeText.BgColor[2]);
  1516. }
  1517. Border parentUI = lastTextui.Parent as Border;
  1518. if (parentUI != null)
  1519. {
  1520. parentUI.SetValue(Canvas.LeftProperty, drawRect.Left);
  1521. parentUI.SetValue(Canvas.TopProperty, drawRect.Top);
  1522. // The width is incorrect
  1523. if (mouseEndPoint.X >= mouseStartPoint.X)
  1524. {
  1525. parentUI.MaxWidth = (pageBound.Right - drawRect.X - cropPoint.X);
  1526. }
  1527. else
  1528. {
  1529. parentUI.MaxWidth = (drawRect.Right - pageBound.X - cropPoint.X);
  1530. }
  1531. if (mouseEndPoint.Y >= mouseStartPoint.Y)
  1532. {
  1533. parentUI.MaxHeight = (pageBound.Bottom - drawRect.Y - cropPoint.Y);
  1534. }
  1535. else
  1536. {
  1537. parentUI.MaxHeight = (drawRect.Bottom - pageBound.Y - cropPoint.Y);
  1538. }
  1539. }
  1540. lastTextui.MinWidth = drawRect.Width;
  1541. lastTextui.MinHeight = drawRect.Height;
  1542. lastTextui.Foreground = new SolidColorBrush(textColor);
  1543. lastTextui.Background = new SolidColorBrush(backgroundColor);
  1544. lastTextBorder.Padding = new Thickness(0);
  1545. lastTextBorder.BorderBrush = new SolidColorBrush(borderColor);
  1546. lastTextBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(annotFreeText.GetBorderWidth() * zoomFactor));
  1547. lastTextui.BorderThickness = new Thickness(0);
  1548. lastTextui.Text = annotFreeText.Content;
  1549. lastTextui.Opacity = annotFreeText.Transparency;
  1550. string fontName = string.Empty;
  1551. string fontFamily = string.Empty;
  1552. CPDFFont.GetFamilyStyleName(annotFreeText.FreeTextDa.FontName, ref fontFamily, ref fontName);
  1553. lastTextui.FontFamily = new FontFamily(fontFamily);
  1554. lastTextui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  1555. lastTextui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  1556. lastTextui.AcceptsReturn = true;
  1557. lastTextui.TextWrapping = TextWrapping.Wrap;
  1558. lastTextui.TextAlignment = TextAlignment.Left;
  1559. switch (annotFreeText.Alignment)
  1560. {
  1561. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  1562. lastTextui.TextAlignment = TextAlignment.Left;
  1563. break;
  1564. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  1565. lastTextui.TextAlignment = TextAlignment.Right;
  1566. break;
  1567. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  1568. lastTextui.TextAlignment = TextAlignment.Center;
  1569. break;
  1570. default:
  1571. break;
  1572. }
  1573. }
  1574. }
  1575. catch
  1576. {
  1577. }
  1578. }
  1579. /// <summary>
  1580. /// Use to calculate the point drawn at a fixed angle
  1581. /// </summary>
  1582. /// <param name="currentPoint">
  1583. /// Current point
  1584. /// </param>
  1585. /// <param name="startPoint">
  1586. /// Start point
  1587. /// </param>
  1588. /// <param name="pageBound">
  1589. /// Maximum drawing area
  1590. /// </param>
  1591. /// <returns>
  1592. /// Return the calculated point
  1593. /// </returns>
  1594. internal Point CalcAnglePoint(Point currentPoint, Point startPoint, Rect pageBound)
  1595. {
  1596. Vector angleVector = currentPoint - startPoint;
  1597. Point originPoint = new Point(startPoint.X, startPoint.Y - angleVector.Length);
  1598. Vector orignVector = originPoint - startPoint;
  1599. Rect checkRect = pageBound;
  1600. int angle = (int)Vector.AngleBetween(orignVector, angleVector);
  1601. if (angle < 0)
  1602. {
  1603. angle += 360;
  1604. }
  1605. int mod = angle % 45;
  1606. int quot = angle / 45;
  1607. Point anglePoint = currentPoint;
  1608. int rotateAngle = 0;
  1609. if (mod < 22)
  1610. {
  1611. Matrix rotateMatrix = new Matrix();
  1612. rotateAngle = quot * 45;
  1613. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1614. anglePoint = rotateMatrix.Transform(originPoint);
  1615. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1616. }
  1617. else
  1618. {
  1619. Matrix rotateMatrix = new Matrix();
  1620. rotateAngle = (quot + 1) * 45;
  1621. rotateMatrix.RotateAt(rotateAngle, startPoint.X, startPoint.Y);
  1622. anglePoint = rotateMatrix.Transform(originPoint);
  1623. anglePoint = new Point((int)anglePoint.X, (int)anglePoint.Y);
  1624. }
  1625. if (checkRect.Contains(anglePoint) == false)
  1626. {
  1627. switch (rotateAngle)
  1628. {
  1629. case 0:
  1630. {
  1631. anglePoint.X = startPoint.X;
  1632. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1633. }
  1634. break;
  1635. case 45:
  1636. {
  1637. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1638. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1639. anglePoint.X = startPoint.X + addValue;
  1640. anglePoint.Y = startPoint.Y - addValue;
  1641. }
  1642. break;
  1643. case 90:
  1644. {
  1645. anglePoint.X = startPoint.X + Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1646. anglePoint.Y = startPoint.Y;
  1647. }
  1648. break;
  1649. case 135:
  1650. {
  1651. double addValue = Math.Min(anglePoint.X - startPoint.X, checkRect.Right - startPoint.X);
  1652. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1653. anglePoint.X = startPoint.X + addValue;
  1654. anglePoint.Y = startPoint.Y + addValue;
  1655. }
  1656. break;
  1657. case 180:
  1658. {
  1659. anglePoint.X = startPoint.X;
  1660. anglePoint.Y = Math.Min(anglePoint.Y, checkRect.Bottom);
  1661. }
  1662. break;
  1663. case 225:
  1664. {
  1665. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1666. addValue = Math.Min(addValue, checkRect.Bottom - startPoint.Y);
  1667. anglePoint.X = startPoint.X - addValue;
  1668. anglePoint.Y = startPoint.Y + addValue;
  1669. }
  1670. break;
  1671. case 270:
  1672. {
  1673. anglePoint.X = startPoint.X - Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1674. anglePoint.Y = startPoint.Y;
  1675. }
  1676. break;
  1677. case 315:
  1678. {
  1679. double addValue = Math.Min(startPoint.X - anglePoint.X, startPoint.X - checkRect.Left);
  1680. addValue = Math.Min(addValue, startPoint.Y - checkRect.Top);
  1681. anglePoint.X = startPoint.X - addValue;
  1682. anglePoint.Y = startPoint.Y - addValue;
  1683. }
  1684. break;
  1685. case 360:
  1686. {
  1687. anglePoint.X = startPoint.X;
  1688. anglePoint.Y = Math.Max(checkRect.Top, Math.Min(anglePoint.Y, startPoint.Y));
  1689. }
  1690. break;
  1691. default:
  1692. break;
  1693. }
  1694. }
  1695. return anglePoint;
  1696. }
  1697. }
  1698. }