CPDFViewerTool.Annot.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKit.Tool.DrawTool;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.Annot;
  7. using ComPDFKitViewer.BaseObject;
  8. using ComPDFKitViewer.Widget;
  9. using ComPDFKitViewer.Layer;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Input;
  16. using System.Windows;
  17. using ComPDFKit.Import;
  18. using ComPDFKit.PDFAnnotation.Form;
  19. using System.Windows.Controls;
  20. using System.Windows.Media;
  21. using static ComPDFKit.PDFAnnotation.CTextAttribute.CFontNameHelper;
  22. using ComPDFKitViewer.Helper;
  23. using Microsoft.SqlServer.Server;
  24. using ComPDFKit.Tool.Help;
  25. using System.Xml.Linq;
  26. using ComPDFKit.Tool.SettingParam;
  27. using ComPDFKit.Tool.UndoManger;
  28. using System.Windows.Controls.Primitives;
  29. using ComPDFKit.Measure;
  30. using System.Dynamic;
  31. namespace ComPDFKit.Tool
  32. {
  33. public class MeasureEventArgs : EventArgs
  34. {
  35. public CPDFMeasureType Type { get; set; }
  36. public double RulerBase { get; set; } = 1;
  37. public string RulerBaseUnit { get; set; } = CPDFMeasure.CPDF_CM;
  38. public double RulerTranslate { get; set; } = 1;
  39. public string RulerTranslateUnit { get; set; } = CPDFMeasure.CPDF_CM;
  40. public double Precision { get; set; } = 0.01;
  41. public double Angle { get; set; }
  42. public Point MousePos { get; set; }
  43. public string Round { get; set; }
  44. public string Distance { get; set; }
  45. public string Area { get; set; }
  46. }
  47. public partial class CPDFViewerTool
  48. {
  49. Border textBorder;
  50. TextBox textui;
  51. BaseAnnot caheMoveAnnot;
  52. BaseAnnot cacheHitTestAnnot;
  53. bool isCacheRedaction = false;
  54. int createAnnotTag = -1;
  55. int selectedPageIndex = -1;
  56. int selectedAnnotIndex = -1;
  57. bool canSave = true;
  58. bool isHitTestLink = false;
  59. bool isHitTestRedact = false;
  60. public event EventHandler<MeasureEventArgs> MeasureChanged;
  61. public void InvokeMeasureChangeEvent(object sender, MeasureEventArgs e)
  62. {
  63. MeasureChanged?.Invoke(sender, e);
  64. }
  65. public bool GetIsHitTestLink()
  66. {
  67. return isHitTestLink;
  68. }
  69. public void SetIsHitTestLink(bool canHitTestLink)
  70. {
  71. isHitTestLink = canHitTestLink;
  72. }
  73. public void SetIsOnlyHitTestRedact(bool canHitTestRedact)
  74. {
  75. isHitTestRedact = canHitTestRedact;
  76. }
  77. public bool IsCanSave()
  78. {
  79. return canSave;
  80. }
  81. public void SetIsCanSave(bool save)
  82. {
  83. canSave = save;
  84. }
  85. public BaseAnnot GetCacheHitTestAnnot()
  86. {
  87. return cacheHitTestAnnot;
  88. }
  89. protected bool AnnotMoveHitTest()
  90. {
  91. BaseAnnot baseAnnot = PDFViewer.AnnotHitTest();
  92. if (baseAnnot != null)
  93. {
  94. if (baseAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_REDACT)
  95. {
  96. if (isCacheRedaction)
  97. {
  98. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  99. (caheMoveAnnot as RedactionAnnot).Draw();
  100. }
  101. isCacheRedaction = true;
  102. }
  103. else
  104. {
  105. if (isCacheRedaction)
  106. {
  107. (caheMoveAnnot as RedactionAnnot).SetIsMouseHover(false);
  108. (caheMoveAnnot as RedactionAnnot).Draw();
  109. }
  110. isCacheRedaction = false;
  111. }
  112. caheMoveAnnot = baseAnnot;
  113. return true;
  114. }
  115. return false;
  116. }
  117. protected bool AnnotHitTest()
  118. {
  119. BaseAnnot baseAnnot = PDFViewer.AnnotHitTest(true);
  120. if (baseAnnot != null)
  121. {
  122. if ((baseAnnot as BaseWidget) != null)
  123. {
  124. cacheHitTestAnnot = null;
  125. return false;
  126. }
  127. cacheHitTestAnnot = baseAnnot;
  128. return true;
  129. }
  130. cacheHitTestAnnot = baseAnnot;
  131. return false;
  132. }
  133. public void SelectedAnnotForIndex(int pageIndex, int annotIndex)
  134. {
  135. CleanSelectedRect();
  136. CleanSelectedMultiRect();
  137. CleanEditAnnot();
  138. cacheHitTestAnnot = null;
  139. selectedPageIndex = pageIndex;
  140. selectedAnnotIndex = annotIndex;
  141. }
  142. private bool UnCheckAnnotViewerModel()
  143. {
  144. if (currentModel == ToolType.CreateAnnot || currentModel == ToolType.WidgetEdit)
  145. {
  146. return false;
  147. }
  148. return true;
  149. }
  150. private void InsertAnnotView()
  151. {
  152. CreateAnnotTool createAnnotTool = new CreateAnnotTool(GetMeasureSetting(), GetDefaultDrawParam(), GetDefaultSettingParam());
  153. int annotViewindex = PDFViewer.GetMaxViewIndex();
  154. PDFViewer.InsertView(annotViewindex, createAnnotTool);
  155. createAnnotTag = createAnnotTool.GetResTag();
  156. createAnnotTool.UpdateAnnotHandler += CreateAnnotTool_UpdateAnnotHandler;
  157. createAnnotTool.CreateFreetextCanceled += CreateAnnotTool_CreateFreetextCanceled;
  158. createAnnotTool.MeasureChanged += CreateAnnotTool_MeasureChanged;
  159. }
  160. private void CreateAnnotTool_CreateFreetextCanceled(object sender, AnnotParam e)
  161. {
  162. dynamic notifyData = null;
  163. notifyData = new ExpandoObject();
  164. notifyData.Action = HistoryAction.Remove;
  165. notifyData.PageIndex = e.PageIndex;
  166. notifyData.AnnotIndex = e.AnnotIndex;
  167. notifyData.AnnotType = e.GetType();
  168. notifyData.CurrentParam = e;
  169. AnnotChanged?.Invoke(this, notifyData);
  170. }
  171. private void CreateAnnotTool_MeasureChanged(object sender, MeasureEventArgs e)
  172. {
  173. InvokeMeasureChangeEvent(sender, e);
  174. }
  175. private void CreateAnnotTool_UpdateAnnotHandler(object sender, bool e)
  176. {
  177. PDFViewer.EnableZoom(e);
  178. PDFViewer.CanHorizontallyScroll = e;
  179. PDFViewer.CanVerticallyScroll = e;
  180. if (e)
  181. {
  182. PDFViewer.UpdateAnnotFrame();
  183. }
  184. }
  185. public void SetAnnotIsProportionalScaling(bool isProportionalScaling)
  186. {
  187. if (UnCheckAnnotViewerModel())
  188. {
  189. return;
  190. }
  191. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  192. (baseLayer as CreateAnnotTool).SetIsProportionalScaling(isProportionalScaling);
  193. }
  194. public double GetMoveLength()
  195. {
  196. if (UnCheckAnnotViewerModel())
  197. {
  198. return 0;
  199. }
  200. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  201. return (baseLayer as CreateAnnotTool).GetMoveLength();
  202. }
  203. public CPDFAnnotation StartDrawAnnot(C_ANNOTATION_TYPE annotType)
  204. {
  205. if (UnCheckAnnotViewerModel())
  206. {
  207. return null;
  208. }
  209. Point point = Mouse.GetPosition(this);
  210. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  211. PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
  212. if (index < 0)
  213. {
  214. return null;
  215. }
  216. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  217. CPDFPage cPDFPage = cPDFDocument.PageAtIndex(index);
  218. if (annotType == C_ANNOTATION_TYPE.C_ANNOTATION_STAMP)
  219. {
  220. DefaultSettingParam defaultSettingParam = GetDefaultSettingParam();
  221. StampParam stampParam = defaultSettingParam.StampParamDef;
  222. stampParam.Rotation = cPDFPage.Rotation;
  223. defaultSettingParam.SetAnnotParam(stampParam);
  224. }
  225. Point cropPoint = new Point();
  226. if (PDFViewer.GetIsCrop())
  227. {
  228. CRect cRect = cPDFPage.GetCropBounds();
  229. cropPoint.X = DpiHelper.PDFNumToStandardNum(cRect.left);
  230. cropPoint.Y = DpiHelper.PDFNumToStandardNum(cRect.top);
  231. }
  232. SetScrollAndZoomTypeForAnnot(annotType);
  233. switch (annotType)
  234. {
  235. case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
  236. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
  237. case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
  238. canSave = false;
  239. break;
  240. default:
  241. break;
  242. }
  243. return (baseLayer as CreateAnnotTool).StartDraw(point, cropPoint, cPDFPage, paintRect, pageBound, annotType, PDFViewer, PDFViewer.GetZoom());
  244. }
  245. public void MultipleClick()
  246. {
  247. if (UnCheckAnnotViewerModel())
  248. {
  249. return;
  250. }
  251. Point point = Mouse.GetPosition(this);
  252. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  253. PDFViewer.GetPointPageInfo(point, out int index, out Rect paintRect, out Rect pageBound);
  254. if (index < 0)
  255. {
  256. return;
  257. }
  258. (baseLayer as CreateAnnotTool).MultipleClick(point);
  259. }
  260. public void SetScrollAndZoomTypeForAnnot(C_ANNOTATION_TYPE annotType)
  261. {
  262. bool enableScroll = false;
  263. bool enableZoom = false;
  264. switch (annotType)
  265. {
  266. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  267. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  268. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  269. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  270. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  271. enableScroll = true;
  272. enableZoom = true;
  273. break;
  274. default:
  275. break;
  276. }
  277. PDFViewer.CanHorizontallyScroll = enableScroll;
  278. PDFViewer.CanVerticallyScroll = enableScroll;
  279. PDFViewer.EnableZoom(enableZoom);
  280. }
  281. public void MoveDrawAnnot()
  282. {
  283. if (UnCheckAnnotViewerModel())
  284. {
  285. return;
  286. }
  287. Point point = Mouse.GetPosition(this);
  288. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  289. (baseLayer as CreateAnnotTool).MoveDraw(point, PDFViewer.GetZoom());
  290. }
  291. public void CreateTextBox()
  292. {
  293. if (UnCheckAnnotViewerModel())
  294. {
  295. return;
  296. }
  297. Point point = Mouse.GetPosition(this);
  298. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  299. (baseLayer as CreateAnnotTool).CreateTextBox();
  300. }
  301. public Rect EndDrawAnnot()
  302. {
  303. if (UnCheckAnnotViewerModel())
  304. {
  305. return new Rect();
  306. }
  307. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  308. return (baseLayer as CreateAnnotTool).EndDraw();
  309. }
  310. private void SetMarkupContent(CPDFMarkupAnnotation markupAnnot, string markupContent)
  311. {
  312. if (markupAnnot == null || markupAnnot.IsValid() == false)
  313. {
  314. return;
  315. }
  316. try
  317. {
  318. DefaultSettingParam defaultParam = GetDefaultSettingParam();
  319. if (defaultParam != null)
  320. {
  321. switch (markupAnnot.Type)
  322. {
  323. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  324. if (string.IsNullOrEmpty(defaultParam.HighlightParamDef.Content) == false)
  325. {
  326. markupContent = defaultParam.HighlightParamDef.Content;
  327. }
  328. break;
  329. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  330. if (string.IsNullOrEmpty(defaultParam.UnderlineParamDef.Content) == false)
  331. {
  332. markupContent = defaultParam.UnderlineParamDef.Content;
  333. }
  334. break;
  335. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  336. if (string.IsNullOrEmpty(defaultParam.SquigglyParamDef.Content) == false)
  337. {
  338. markupContent = defaultParam.SquigglyParamDef.Content;
  339. }
  340. break;
  341. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  342. if (string.IsNullOrEmpty(defaultParam.StrikeoutParamDef.Content) == false)
  343. {
  344. markupContent = defaultParam.StrikeoutParamDef.Content;
  345. }
  346. break;
  347. default:
  348. return;
  349. }
  350. }
  351. if (string.IsNullOrEmpty(markupContent) == false)
  352. {
  353. markupAnnot.SetContent(markupContent);
  354. }
  355. }
  356. catch (Exception ex)
  357. {
  358. }
  359. }
  360. public bool CreateAnnotForSelectText(TextSelectInfo textSelectInfo, C_ANNOTATION_TYPE annotType, out GroupHistory historyData)
  361. {
  362. historyData = null;
  363. if (UnCheckAnnotViewerModel())
  364. {
  365. return false;
  366. }
  367. Dictionary<int, List<Rect>> PagesRectList = textSelectInfo.ConvertToSelectRectDict();
  368. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  369. GroupHistory historyGroup = new GroupHistory();
  370. foreach (int pageIndex in PagesRectList.Keys)
  371. {
  372. List<Rect> pageSelectRectList = PagesRectList[pageIndex];
  373. if (pageSelectRectList.Count > 0)
  374. {
  375. CPDFPage docPage = cPDFDocument.PageAtIndex(pageIndex);
  376. docPage.ReleaseAllAnnotations();
  377. CPDFAnnotation annotCore = docPage.CreateAnnot(annotType);
  378. annotCore.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  379. annotCore.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  380. if (annotCore == null || annotCore is CPDFWidget)
  381. {
  382. return false;
  383. }
  384. List<CRect> coreRectList = new List<CRect>();
  385. foreach (Rect copyRect in pageSelectRectList)
  386. {
  387. coreRectList.Add(new CRect((float)copyRect.Left, (float)copyRect.Top, (float)copyRect.Right, (float)copyRect.Bottom));
  388. }
  389. CreateDefaultAnnot(annotCore, annotType, null);
  390. string markupContent = textSelectInfo.PageSelectText[pageIndex];
  391. switch (annotType)
  392. {
  393. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  394. (annotCore as CPDFHighlightAnnotation).SetQuardRects(coreRectList);
  395. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  396. break;
  397. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  398. (annotCore as CPDFUnderlineAnnotation).SetQuardRects(coreRectList);
  399. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  400. break;
  401. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  402. (annotCore as CPDFSquigglyAnnotation).SetQuardRects(coreRectList);
  403. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  404. break;
  405. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  406. (annotCore as CPDFStrikeoutAnnotation).SetQuardRects(coreRectList);
  407. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  408. break;
  409. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  410. (annotCore as CPDFRedactAnnotation).SetQuardRects(coreRectList);
  411. break;
  412. default:
  413. break;
  414. }
  415. int Left = (int)pageSelectRectList.Select(x => x.Left).Min();
  416. int Top = (int)pageSelectRectList.Select(x => x.Top).Min();
  417. int Right = (int)pageSelectRectList.Select(x => x.Right).Max();
  418. int Bottom = (int)pageSelectRectList.Select(x => x.Bottom).Max();
  419. annotCore.SetRect(new CRect(Left, Bottom, Right, Top));
  420. //if (annotCore.GetIsLocked() != underlineArgs.Locked)
  421. //{
  422. // annotCore.SetIsLocked(underlineArgs.Locked);
  423. //}
  424. annotCore.UpdateAp();
  425. switch (annotType)
  426. {
  427. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  428. {
  429. HighlightAnnotHistory highlightAnnotHistory = new HighlightAnnotHistory();
  430. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  431. highlightAnnotHistory.CurrentParam = (HighlightParam)annotParam;
  432. highlightAnnotHistory.PDFDoc = cPDFDocument;
  433. highlightAnnotHistory.Action = HistoryAction.Add;
  434. historyGroup.Histories.Add(highlightAnnotHistory);
  435. }
  436. break;
  437. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  438. {
  439. UnderlineAnnotHistory underlineAnnotHistory = new UnderlineAnnotHistory();
  440. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  441. underlineAnnotHistory.CurrentParam = (UnderlineParam)annotParam;
  442. underlineAnnotHistory.PDFDoc = cPDFDocument;
  443. underlineAnnotHistory.Action = HistoryAction.Add;
  444. historyGroup.Histories.Add(underlineAnnotHistory);
  445. }
  446. break;
  447. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  448. {
  449. SquigglyAnnotHistory squigglyAnnotHistory = new SquigglyAnnotHistory();
  450. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  451. squigglyAnnotHistory.CurrentParam = (SquigglyParam)annotParam;
  452. squigglyAnnotHistory.PDFDoc = cPDFDocument;
  453. squigglyAnnotHistory.Action = HistoryAction.Add;
  454. historyGroup.Histories.Add(squigglyAnnotHistory);
  455. }
  456. break;
  457. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  458. {
  459. StrikeoutAnnotHistory strikeoutHistory = new StrikeoutAnnotHistory();
  460. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  461. strikeoutHistory.CurrentParam = (StrikeoutParam)annotParam;
  462. strikeoutHistory.PDFDoc = cPDFDocument;
  463. strikeoutHistory.Action = HistoryAction.Add;
  464. historyGroup.Histories.Add(strikeoutHistory);
  465. }
  466. break;
  467. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  468. {
  469. RedactAnnotHistory redactHistory = new RedactAnnotHistory();
  470. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  471. redactHistory.Action = HistoryAction.Add;
  472. redactHistory.CurrentParam = (RedactParam)annotParam;
  473. redactHistory.PDFDoc = cPDFDocument;
  474. redactHistory.Action = HistoryAction.Add;
  475. historyGroup.Histories.Add(redactHistory);
  476. }
  477. break;
  478. default:
  479. break;
  480. }
  481. annotCore.ReleaseAnnot();
  482. }
  483. }
  484. if (historyGroup.Histories.Count > 0)
  485. {
  486. GetCPDFViewer()?.UndoManager?.AddHistory(historyGroup);
  487. }
  488. PDFViewer.UpdateAnnotFrame();
  489. historyData = historyGroup;
  490. return true;
  491. }
  492. public Point GetStartPoint()
  493. {
  494. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  495. return (baseLayer as CreateAnnotTool).GetStartPoint();
  496. }
  497. public Point GetEndPoint()
  498. {
  499. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  500. return (baseLayer as CreateAnnotTool).GetEndPoint();
  501. }
  502. public List<Point> GetInkDrawPoints()
  503. {
  504. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  505. return (baseLayer as CreateAnnotTool).GetInkDrawPoints();
  506. }
  507. public List<Point> GetMeasureDrawPoints()
  508. {
  509. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  510. return (baseLayer as CreateAnnotTool).GetMeasureDrawPoints();
  511. }
  512. public Rect GetDrawAnnotMaxRect()
  513. {
  514. if (UnCheckAnnotViewerModel())
  515. {
  516. return new Rect();
  517. }
  518. Point point = Mouse.GetPosition(this);
  519. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  520. return (baseLayer as CreateAnnotTool).GetMaxRect();
  521. }
  522. public void ClearDrawAnnot()
  523. {
  524. if (UnCheckAnnotViewerModel())
  525. {
  526. return;
  527. }
  528. Point point = Mouse.GetPosition(this);
  529. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  530. (baseLayer as CreateAnnotTool).ClearDraw();
  531. }
  532. protected void UpdateTextPop()
  533. {
  534. if (textBorder != null)
  535. {
  536. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  537. if (currentAnnot == null)
  538. {
  539. return;
  540. }
  541. AnnotLayer annotLayer = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag()) as AnnotLayer;
  542. bool isOk = annotLayer.GetUpdate(ref currentAnnot);
  543. if (!isOk)
  544. {
  545. return;
  546. }
  547. AnnotData annotData = currentAnnot.GetAnnotData();
  548. if (annotData.PaintRect == Rect.Empty)
  549. {
  550. return;
  551. }
  552. //SetFormRotateTransform(textui, annotData);
  553. // Set the width and height of the TextBox, rotation, and other control position information
  554. RotateTransform rotateTrans = new RotateTransform();
  555. rotateTrans.Angle = -90 * annotData.Rotation;
  556. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  557. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  558. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  559. textBorder.Width = rotateRect.Width;
  560. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  561. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  562. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  563. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  564. }
  565. }
  566. protected void BuildPopTextUI(BaseAnnot textAnnot)
  567. {
  568. try
  569. {
  570. if (textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  571. {
  572. AnnotData annotData = textAnnot.GetAnnotData();
  573. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  574. if (textWidget == null)
  575. {
  576. return;
  577. }
  578. textAnnot.CleanDraw();
  579. CRect drawRect = textWidget.GetRect();
  580. double Width = DpiHelper.PDFNumToStandardNum(drawRect.width());
  581. double Height = DpiHelper.PDFNumToStandardNum(drawRect.height());
  582. textui = new TextBox();
  583. textui.Name = "PdfViewerTextBox";
  584. textBorder = new Border();
  585. textBorder.Child = textui;
  586. textBorder.MinWidth = Width * PDFViewer.GetZoom();
  587. textBorder.MinHeight = Height * PDFViewer.GetZoom();
  588. // Calculate the maximum value
  589. double PDFWidth = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Width;
  590. double PDFHeight = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Height;
  591. textBorder.MaxWidth = (PDFWidth - annotData.VisualRect.Left - annotData.CropLeft);
  592. textBorder.MaxHeight = (PDFHeight - annotData.VisualRect.Top - annotData.CropTop);
  593. CTextAttribute textAttribute = textWidget.FreeTextDa;
  594. byte transparency = textWidget.GetTransparency();
  595. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  596. Color textColor = Color.FromArgb(
  597. transparency,
  598. textAttribute.FontColor[0],
  599. textAttribute.FontColor[1],
  600. textAttribute.FontColor[2]);
  601. Color borderColor = Colors.Transparent;
  602. Color backgroundColor = Colors.Transparent;
  603. byte[] colorArray = new byte[3];
  604. if (textWidget.Transparency > 0)
  605. {
  606. borderColor = Color.FromArgb(textWidget.Transparency, textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  607. }
  608. if (textWidget.HasBgColor)
  609. {
  610. backgroundColor = Color.FromArgb(textWidget.Transparency, textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  611. }
  612. textui.Foreground = new SolidColorBrush(textColor);
  613. textui.Background = new SolidColorBrush(backgroundColor);
  614. textBorder.Padding = new Thickness(0);
  615. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  616. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
  617. textui.BorderThickness = new Thickness(0);
  618. textui.Text = textWidget.Content;
  619. string fontName = string.Empty;
  620. string fontFamily = string.Empty;
  621. CPDFFont.GetFamilyStyleName(textWidget.FreeTextDa.FontName, ref fontFamily, ref fontName);
  622. textui.FontFamily = new FontFamily(fontFamily);
  623. textui.AcceptsReturn = true;
  624. textui.TextWrapping = TextWrapping.Wrap;
  625. textui.TextAlignment = TextAlignment.Left;
  626. switch (textWidget.Alignment)
  627. {
  628. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  629. textui.TextAlignment = TextAlignment.Left;
  630. break;
  631. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  632. textui.TextAlignment = TextAlignment.Right;
  633. break;
  634. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  635. textui.TextAlignment = TextAlignment.Center;
  636. break;
  637. default:
  638. break;
  639. }
  640. //SetFormRotateTransform(textui, annotData);
  641. // Set the width and height of the TextBox, rotation, and other control position information
  642. RotateTransform rotateTrans = new RotateTransform();
  643. rotateTrans.Angle = -90 * annotData.Rotation;
  644. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  645. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  646. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  647. textBorder.Width = rotateRect.Width;
  648. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  649. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  650. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  651. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  652. rotateTrans.Angle = 90 * annotData.Rotation;
  653. rotateTrans.CenterX = rotateRect.Width / 2;
  654. rotateTrans.CenterY = rotateRect.Height / 2;
  655. textBorder.RenderTransform = rotateTrans;
  656. textui.Loaded += (object sender, RoutedEventArgs e) =>
  657. {
  658. textui.Focus();
  659. textui.CaretIndex = textui.Text.Length;
  660. textui.SetValue(PopupAttachDataProperty, textAnnot);
  661. };
  662. CPDFViewer viewer = GetCPDFViewer();
  663. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  664. {
  665. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  666. if (currentAnnot != null)
  667. {
  668. AnnotData widgetData = currentAnnot.GetAnnotData();
  669. CPDFFreeTextAnnotation updateFreeText = widgetData.Annot as CPDFFreeTextAnnotation;
  670. if (textui.Text == string.Empty && updateFreeText.GetBorderWidth() == 0)
  671. {
  672. dynamic notifyData = null;
  673. notifyData = new ExpandoObject();
  674. notifyData.Action = HistoryAction.Remove;
  675. notifyData.PageIndex = widgetData.PageIndex;
  676. notifyData.AnnotIndex = widgetData.AnnotIndex;
  677. notifyData.AnnotType = widgetData.AnnotType;
  678. notifyData.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), widgetData.PageIndex, updateFreeText);
  679. updateFreeText.RemoveAnnot();
  680. AnnotChanged?.Invoke(this, notifyData);
  681. }
  682. else
  683. {
  684. FreeTextAnnotHistory history = null;
  685. if (updateFreeText.Content != textui.Text)
  686. {
  687. history = new FreeTextAnnotHistory();
  688. history.PDFDoc = viewer.GetDocument();
  689. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  690. history.Action = HistoryAction.Update;
  691. updateFreeText.SetContent(textui.Text);
  692. }
  693. RotateTransform rotateTranss = new RotateTransform();
  694. rotateTranss.Angle = -90 * annotData.Rotation;
  695. rotateTranss.CenterX = textBorder.ActualWidth / 2;
  696. rotateTranss.CenterY = textBorder.ActualHeight / 2;
  697. Rect textRect = rotateTranss.TransformBounds(new Rect(0, 0, textBorder.ActualWidth, textBorder.ActualHeight));
  698. Rect changeRect = new Rect(
  699. annotData.ClientRect.Left,
  700. annotData.ClientRect.Top,
  701. DpiHelper.StandardNumToPDFNum(textRect.Width / annotData.CurrentZoom),
  702. DpiHelper.StandardNumToPDFNum(textRect.Height / annotData.CurrentZoom));
  703. updateFreeText.SetRect(new CRect(
  704. (float)changeRect.Left,
  705. (float)changeRect.Bottom,
  706. (float)changeRect.Right,
  707. (float)changeRect.Top
  708. ));
  709. updateFreeText.UpdateAp();
  710. if (history != null)
  711. {
  712. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  713. viewer.UndoManager?.AddHistory(history);
  714. viewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, history));
  715. }
  716. viewer.UpdateAnnotFrame();
  717. }
  718. RemovePopTextUI();
  719. }
  720. };
  721. BaseLayer createAnnotTool = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  722. if (createAnnotTool != null)
  723. {
  724. createAnnotTool.Children.Add(textBorder);
  725. createAnnotTool.Arrange();
  726. }
  727. textui.LayoutUpdated += (object sender, EventArgs e) =>
  728. {
  729. createAnnotTool.Arrange();
  730. };
  731. }
  732. }
  733. catch (Exception ex)
  734. {
  735. }
  736. }
  737. public void UpdatePopTextUI(BaseAnnot textAnnot)
  738. {
  739. try
  740. {
  741. if (textui != null && textBorder != null && textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  742. {
  743. AnnotData annotData = textAnnot.GetAnnotData();
  744. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  745. if (textWidget == null)
  746. {
  747. return;
  748. }
  749. textAnnot.CleanDraw();
  750. CTextAttribute textAttribute = textWidget.FreeTextDa;
  751. byte transparency = textWidget.GetTransparency();
  752. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  753. Color textColor = Color.FromArgb(
  754. transparency,
  755. textAttribute.FontColor[0],
  756. textAttribute.FontColor[1],
  757. textAttribute.FontColor[2]);
  758. Color borderColor = Colors.Transparent;
  759. Color backgroundColor = Colors.White;
  760. byte[] colorArray = new byte[3];
  761. if (textWidget.Transparency > 0)
  762. {
  763. borderColor = Color.FromRgb(textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  764. }
  765. if (textWidget.HasBgColor)
  766. {
  767. backgroundColor = Color.FromRgb(textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  768. }
  769. textui.Foreground = new SolidColorBrush(textColor);
  770. textui.Background = new SolidColorBrush(backgroundColor);
  771. textBorder.Padding = new Thickness(0);
  772. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  773. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
  774. textui.BorderThickness = new Thickness(0);
  775. textui.Text = textWidget.Content;
  776. textui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  777. textui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  778. textui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  779. textui.AcceptsReturn = true;
  780. textui.TextWrapping = TextWrapping.Wrap;
  781. //textui.VerticalContentAlignment = VerticalAlignment.Center;
  782. textui.TextAlignment = TextAlignment.Left;
  783. switch (textWidget.Alignment)
  784. {
  785. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  786. textui.TextAlignment = TextAlignment.Left;
  787. break;
  788. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  789. textui.TextAlignment = TextAlignment.Right;
  790. break;
  791. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  792. textui.TextAlignment = TextAlignment.Center;
  793. break;
  794. default:
  795. break;
  796. }
  797. //SetFormRotateTransform(textui, annotData);
  798. // Set the width and height of the TextBox, rotation, and other control position information
  799. RotateTransform rotateTrans = new RotateTransform();
  800. rotateTrans.Angle = -90 * annotData.Rotation;
  801. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  802. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  803. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  804. textBorder.Width = rotateRect.Width;
  805. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  806. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  807. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  808. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  809. rotateTrans.Angle = 90 * annotData.Rotation;
  810. rotateTrans.CenterX = rotateRect.Width / 2;
  811. rotateTrans.CenterY = rotateRect.Height / 2;
  812. textBorder.RenderTransform = rotateTrans;
  813. }
  814. }
  815. catch
  816. {
  817. }
  818. }
  819. public void RemovePopTextUI()
  820. {
  821. if (textBorder == null)
  822. {
  823. return;
  824. }
  825. BaseLayer removeLayer = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  826. removeLayer.Children.Remove(textBorder);
  827. }
  828. public bool HitTestBorder()
  829. {
  830. if (textBorder == null)
  831. {
  832. return false;
  833. }
  834. Point pagePosition = Mouse.GetPosition(textBorder);
  835. HitTestResult hitTestResult = VisualTreeHelper.HitTest(textBorder, pagePosition);
  836. if (hitTestResult != null && hitTestResult.VisualHit != null)
  837. {
  838. return true;
  839. }
  840. return false;
  841. }
  842. }
  843. }