CreateAnnotTool.cs 82 KB

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