CPDFViewerTool.Annot.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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 TextBox CreateTextBox()
  292. {
  293. if (UnCheckAnnotViewerModel())
  294. {
  295. return null;
  296. }
  297. Point point = Mouse.GetPosition(this);
  298. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  299. TextBox textui = (baseLayer as CreateAnnotTool).CreateTextBox();
  300. return textui;
  301. }
  302. public Rect EndDrawAnnot()
  303. {
  304. if (UnCheckAnnotViewerModel())
  305. {
  306. return new Rect();
  307. }
  308. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  309. return (baseLayer as CreateAnnotTool).EndDraw();
  310. }
  311. private void SetMarkupContent(CPDFMarkupAnnotation markupAnnot, string markupContent)
  312. {
  313. if (markupAnnot == null || markupAnnot.IsValid() == false)
  314. {
  315. return;
  316. }
  317. try
  318. {
  319. DefaultSettingParam defaultParam = GetDefaultSettingParam();
  320. if (defaultParam != null)
  321. {
  322. switch (markupAnnot.Type)
  323. {
  324. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  325. if (string.IsNullOrEmpty(defaultParam.HighlightParamDef.Content) == false)
  326. {
  327. markupContent = defaultParam.HighlightParamDef.Content;
  328. }
  329. break;
  330. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  331. if (string.IsNullOrEmpty(defaultParam.UnderlineParamDef.Content) == false)
  332. {
  333. markupContent = defaultParam.UnderlineParamDef.Content;
  334. }
  335. break;
  336. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  337. if (string.IsNullOrEmpty(defaultParam.SquigglyParamDef.Content) == false)
  338. {
  339. markupContent = defaultParam.SquigglyParamDef.Content;
  340. }
  341. break;
  342. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  343. if (string.IsNullOrEmpty(defaultParam.StrikeoutParamDef.Content) == false)
  344. {
  345. markupContent = defaultParam.StrikeoutParamDef.Content;
  346. }
  347. break;
  348. default:
  349. return;
  350. }
  351. }
  352. if (string.IsNullOrEmpty(markupContent) == false)
  353. {
  354. markupAnnot.SetContent(markupContent);
  355. }
  356. }
  357. catch (Exception ex)
  358. {
  359. }
  360. }
  361. public bool CreateAnnotForSelectText(TextSelectInfo textSelectInfo, C_ANNOTATION_TYPE annotType, out GroupHistory historyData)
  362. {
  363. historyData = null;
  364. if (UnCheckAnnotViewerModel())
  365. {
  366. return false;
  367. }
  368. Dictionary<int, List<Rect>> PagesRectList = textSelectInfo.ConvertToSelectRectDict();
  369. CPDFDocument cPDFDocument = PDFViewer.GetDocument();
  370. GroupHistory historyGroup = new GroupHistory();
  371. foreach (int pageIndex in PagesRectList.Keys)
  372. {
  373. List<Rect> pageSelectRectList = PagesRectList[pageIndex];
  374. if (pageSelectRectList.Count > 0)
  375. {
  376. CPDFPage docPage = cPDFDocument.PageAtIndex(pageIndex);
  377. docPage.ReleaseAllAnnotations();
  378. CPDFAnnotation annotCore = docPage.CreateAnnot(annotType);
  379. annotCore.SetCreationDate(PDFHelp.GetCurrentPdfTime());
  380. annotCore.SetModifyDate(PDFHelp.GetCurrentPdfTime());
  381. if (annotCore == null || annotCore is CPDFWidget)
  382. {
  383. return false;
  384. }
  385. List<CRect> coreRectList = new List<CRect>();
  386. foreach (Rect copyRect in pageSelectRectList)
  387. {
  388. coreRectList.Add(new CRect((float)copyRect.Left, (float)copyRect.Top, (float)copyRect.Right, (float)copyRect.Bottom));
  389. }
  390. CreateDefaultAnnot(annotCore, annotType, null);
  391. string markupContent = textSelectInfo.PageSelectText[pageIndex];
  392. switch (annotType)
  393. {
  394. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  395. (annotCore as CPDFHighlightAnnotation).SetQuardRects(coreRectList);
  396. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  397. break;
  398. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  399. (annotCore as CPDFUnderlineAnnotation).SetQuardRects(coreRectList);
  400. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  401. break;
  402. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  403. (annotCore as CPDFSquigglyAnnotation).SetQuardRects(coreRectList);
  404. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  405. break;
  406. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  407. (annotCore as CPDFStrikeoutAnnotation).SetQuardRects(coreRectList);
  408. SetMarkupContent(annotCore as CPDFMarkupAnnotation, markupContent);
  409. break;
  410. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  411. (annotCore as CPDFRedactAnnotation).SetQuardRects(coreRectList);
  412. break;
  413. default:
  414. break;
  415. }
  416. int Left = (int)pageSelectRectList.Select(x => x.Left).Min();
  417. int Top = (int)pageSelectRectList.Select(x => x.Top).Min();
  418. int Right = (int)pageSelectRectList.Select(x => x.Right).Max();
  419. int Bottom = (int)pageSelectRectList.Select(x => x.Bottom).Max();
  420. annotCore.SetRect(new CRect(Left, Bottom, Right, Top));
  421. //if (annotCore.GetIsLocked() != underlineArgs.Locked)
  422. //{
  423. // annotCore.SetIsLocked(underlineArgs.Locked);
  424. //}
  425. annotCore.UpdateAp();
  426. switch (annotType)
  427. {
  428. case C_ANNOTATION_TYPE.C_ANNOTATION_HIGHLIGHT:
  429. {
  430. HighlightAnnotHistory highlightAnnotHistory = new HighlightAnnotHistory();
  431. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  432. highlightAnnotHistory.CurrentParam = (HighlightParam)annotParam;
  433. highlightAnnotHistory.PDFDoc = cPDFDocument;
  434. highlightAnnotHistory.Action = HistoryAction.Add;
  435. historyGroup.Histories.Add(highlightAnnotHistory);
  436. }
  437. break;
  438. case C_ANNOTATION_TYPE.C_ANNOTATION_UNDERLINE:
  439. {
  440. UnderlineAnnotHistory underlineAnnotHistory = new UnderlineAnnotHistory();
  441. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  442. underlineAnnotHistory.CurrentParam = (UnderlineParam)annotParam;
  443. underlineAnnotHistory.PDFDoc = cPDFDocument;
  444. underlineAnnotHistory.Action = HistoryAction.Add;
  445. historyGroup.Histories.Add(underlineAnnotHistory);
  446. }
  447. break;
  448. case C_ANNOTATION_TYPE.C_ANNOTATION_SQUIGGLY:
  449. {
  450. SquigglyAnnotHistory squigglyAnnotHistory = new SquigglyAnnotHistory();
  451. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  452. squigglyAnnotHistory.CurrentParam = (SquigglyParam)annotParam;
  453. squigglyAnnotHistory.PDFDoc = cPDFDocument;
  454. squigglyAnnotHistory.Action = HistoryAction.Add;
  455. historyGroup.Histories.Add(squigglyAnnotHistory);
  456. }
  457. break;
  458. case C_ANNOTATION_TYPE.C_ANNOTATION_STRIKEOUT:
  459. {
  460. StrikeoutAnnotHistory strikeoutHistory = new StrikeoutAnnotHistory();
  461. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  462. strikeoutHistory.CurrentParam = (StrikeoutParam)annotParam;
  463. strikeoutHistory.PDFDoc = cPDFDocument;
  464. strikeoutHistory.Action = HistoryAction.Add;
  465. historyGroup.Histories.Add(strikeoutHistory);
  466. }
  467. break;
  468. case C_ANNOTATION_TYPE.C_ANNOTATION_REDACT:
  469. {
  470. RedactAnnotHistory redactHistory = new RedactAnnotHistory();
  471. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  472. redactHistory.Action = HistoryAction.Add;
  473. redactHistory.CurrentParam = (RedactParam)annotParam;
  474. redactHistory.PDFDoc = cPDFDocument;
  475. redactHistory.Action = HistoryAction.Add;
  476. historyGroup.Histories.Add(redactHistory);
  477. }
  478. break;
  479. default:
  480. break;
  481. }
  482. annotCore.ReleaseAnnot();
  483. }
  484. }
  485. if (historyGroup.Histories.Count > 0)
  486. {
  487. GetCPDFViewer()?.UndoManager?.AddHistory(historyGroup);
  488. }
  489. PDFViewer.UpdateAnnotFrame();
  490. historyData = historyGroup;
  491. return true;
  492. }
  493. public Point GetStartPoint()
  494. {
  495. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  496. return (baseLayer as CreateAnnotTool).GetStartPoint();
  497. }
  498. public Point GetEndPoint()
  499. {
  500. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  501. return (baseLayer as CreateAnnotTool).GetEndPoint();
  502. }
  503. public List<Point> GetInkDrawPoints()
  504. {
  505. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  506. return (baseLayer as CreateAnnotTool).GetInkDrawPoints();
  507. }
  508. public List<Point> GetMeasureDrawPoints()
  509. {
  510. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  511. return (baseLayer as CreateAnnotTool).GetMeasureDrawPoints();
  512. }
  513. public Rect GetDrawAnnotMaxRect()
  514. {
  515. if (UnCheckAnnotViewerModel())
  516. {
  517. return new Rect();
  518. }
  519. Point point = Mouse.GetPosition(this);
  520. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  521. return (baseLayer as CreateAnnotTool).GetMaxRect();
  522. }
  523. public void ClearDrawAnnot()
  524. {
  525. if (UnCheckAnnotViewerModel())
  526. {
  527. return;
  528. }
  529. Point point = Mouse.GetPosition(this);
  530. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  531. (baseLayer as CreateAnnotTool).ClearDraw();
  532. }
  533. protected void UpdateTextPop()
  534. {
  535. if (textBorder != null)
  536. {
  537. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  538. if (currentAnnot == null)
  539. {
  540. return;
  541. }
  542. AnnotLayer annotLayer = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag()) as AnnotLayer;
  543. bool isOk = annotLayer.GetUpdate(ref currentAnnot);
  544. if (!isOk)
  545. {
  546. return;
  547. }
  548. AnnotData annotData = currentAnnot.GetAnnotData();
  549. if (annotData.PaintRect == Rect.Empty)
  550. {
  551. return;
  552. }
  553. //SetFormRotateTransform(textui, annotData);
  554. // Set the width and height of the TextBox, rotation, and other control position information
  555. RotateTransform rotateTrans = new RotateTransform();
  556. rotateTrans.Angle = -90 * annotData.Rotation;
  557. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  558. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  559. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  560. textBorder.Width = rotateRect.Width;
  561. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  562. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  563. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  564. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  565. }
  566. }
  567. protected void BuildPopTextUI(BaseAnnot textAnnot)
  568. {
  569. try
  570. {
  571. if (textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  572. {
  573. AnnotData annotData = textAnnot.GetAnnotData();
  574. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  575. if (textWidget == null)
  576. {
  577. return;
  578. }
  579. textAnnot.CleanDraw();
  580. CRect drawRect = textWidget.GetRect();
  581. double Width = DpiHelper.PDFNumToStandardNum(drawRect.width());
  582. double Height = DpiHelper.PDFNumToStandardNum(drawRect.height());
  583. textui = new TextBox();
  584. textui.Name = "PdfViewerTextBox";
  585. textBorder = new Border();
  586. textBorder.Child = textui;
  587. textBorder.MinWidth = Width * PDFViewer.GetZoom();
  588. textBorder.MinHeight = Height * PDFViewer.GetZoom();
  589. // Calculate the maximum value
  590. double PDFWidth = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Width;
  591. double PDFHeight = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Height;
  592. textBorder.MaxWidth = (PDFWidth - annotData.VisualRect.Left - annotData.CropLeft);
  593. textBorder.MaxHeight = (PDFHeight - annotData.VisualRect.Top - annotData.CropTop);
  594. CTextAttribute textAttribute = textWidget.FreeTextDa;
  595. byte transparency = textWidget.GetTransparency();
  596. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  597. Color textColor = Color.FromArgb(
  598. transparency,
  599. textAttribute.FontColor[0],
  600. textAttribute.FontColor[1],
  601. textAttribute.FontColor[2]);
  602. Color borderColor = Colors.Transparent;
  603. Color backgroundColor = Colors.Transparent;
  604. byte[] colorArray = new byte[3];
  605. if (textWidget.Transparency > 0)
  606. {
  607. borderColor = Color.FromArgb(textWidget.Transparency, textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  608. }
  609. if (textWidget.HasBgColor)
  610. {
  611. backgroundColor = Color.FromArgb(textWidget.Transparency, textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  612. }
  613. textui.Foreground = new SolidColorBrush(textColor);
  614. textui.Background = new SolidColorBrush(backgroundColor);
  615. textBorder.Padding = new Thickness(0);
  616. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  617. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
  618. textui.BorderThickness = new Thickness(0);
  619. textui.Text = textWidget.Content;
  620. string fontName = string.Empty;
  621. string fontFamily = string.Empty;
  622. CPDFFont.GetFamilyStyleName(textWidget.FreeTextDa.FontName, ref fontFamily, ref fontName);
  623. textui.FontFamily = new FontFamily(fontFamily);
  624. textui.AcceptsReturn = true;
  625. textui.TextWrapping = TextWrapping.Wrap;
  626. textui.TextAlignment = TextAlignment.Left;
  627. switch (textWidget.Alignment)
  628. {
  629. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  630. textui.TextAlignment = TextAlignment.Left;
  631. break;
  632. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  633. textui.TextAlignment = TextAlignment.Right;
  634. break;
  635. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  636. textui.TextAlignment = TextAlignment.Center;
  637. break;
  638. default:
  639. break;
  640. }
  641. //SetFormRotateTransform(textui, annotData);
  642. // Set the width and height of the TextBox, rotation, and other control position information
  643. RotateTransform rotateTrans = new RotateTransform();
  644. rotateTrans.Angle = -90 * annotData.Rotation;
  645. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  646. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  647. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  648. textBorder.Width = rotateRect.Width;
  649. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  650. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  651. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  652. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  653. rotateTrans.Angle = 90 * annotData.Rotation;
  654. rotateTrans.CenterX = rotateRect.Width / 2;
  655. rotateTrans.CenterY = rotateRect.Height / 2;
  656. textBorder.RenderTransform = rotateTrans;
  657. textui.Loaded += (object sender, RoutedEventArgs e) =>
  658. {
  659. textui.Focus();
  660. textui.CaretIndex = textui.Text.Length;
  661. textui.SetValue(PopupAttachDataProperty, textAnnot);
  662. };
  663. CPDFViewer viewer = GetCPDFViewer();
  664. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  665. {
  666. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  667. if (currentAnnot != null)
  668. {
  669. AnnotData widgetData = currentAnnot.GetAnnotData();
  670. CPDFFreeTextAnnotation updateFreeText = widgetData.Annot as CPDFFreeTextAnnotation;
  671. if (textui.Text == string.Empty && updateFreeText.GetBorderWidth() == 0)
  672. {
  673. dynamic notifyData = null;
  674. notifyData = new ExpandoObject();
  675. notifyData.Action = HistoryAction.Remove;
  676. notifyData.PageIndex = widgetData.PageIndex;
  677. notifyData.AnnotIndex = widgetData.AnnotIndex;
  678. notifyData.AnnotType = widgetData.AnnotType;
  679. notifyData.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), widgetData.PageIndex, updateFreeText);
  680. updateFreeText.RemoveAnnot();
  681. AnnotChanged?.Invoke(this, notifyData);
  682. }
  683. else
  684. {
  685. FreeTextAnnotHistory history = null;
  686. if (updateFreeText.Content != textui.Text)
  687. {
  688. history = new FreeTextAnnotHistory();
  689. history.PDFDoc = viewer.GetDocument();
  690. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  691. history.Action = HistoryAction.Update;
  692. updateFreeText.SetContent(textui.Text);
  693. }
  694. RotateTransform rotateTranss = new RotateTransform();
  695. rotateTranss.Angle = -90 * annotData.Rotation;
  696. rotateTranss.CenterX = textBorder.ActualWidth / 2;
  697. rotateTranss.CenterY = textBorder.ActualHeight / 2;
  698. Rect textRect = rotateTranss.TransformBounds(new Rect(0, 0, textBorder.ActualWidth, textBorder.ActualHeight));
  699. Rect changeRect = new Rect(
  700. annotData.ClientRect.Left,
  701. annotData.ClientRect.Top,
  702. DpiHelper.StandardNumToPDFNum(textRect.Width / annotData.CurrentZoom),
  703. DpiHelper.StandardNumToPDFNum(textRect.Height / annotData.CurrentZoom));
  704. updateFreeText.SetRect(new CRect(
  705. (float)changeRect.Left,
  706. (float)changeRect.Bottom,
  707. (float)changeRect.Right,
  708. (float)changeRect.Top
  709. ));
  710. updateFreeText.UpdateAp();
  711. if (history != null)
  712. {
  713. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  714. viewer.UndoManager?.AddHistory(history);
  715. viewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, history));
  716. }
  717. viewer.UpdateAnnotFrame();
  718. }
  719. RemovePopTextUI();
  720. }
  721. };
  722. BaseLayer createAnnotTool = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  723. if (createAnnotTool != null)
  724. {
  725. createAnnotTool.Children.Add(textBorder);
  726. createAnnotTool.Arrange();
  727. }
  728. textui.LayoutUpdated += (object sender, EventArgs e) =>
  729. {
  730. createAnnotTool.Arrange();
  731. };
  732. PopupControlInfo attachInfo=new PopupControlInfo();
  733. attachInfo.Annot = textAnnot;
  734. attachInfo.PopUpControl=textui;
  735. PopUpControlHandler?.Invoke(this, attachInfo);
  736. }
  737. }
  738. catch (Exception ex)
  739. {
  740. }
  741. }
  742. public void UpdatePopTextUI(BaseAnnot textAnnot)
  743. {
  744. try
  745. {
  746. if (textui != null && textBorder != null && textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  747. {
  748. AnnotData annotData = textAnnot.GetAnnotData();
  749. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  750. if (textWidget == null)
  751. {
  752. return;
  753. }
  754. textAnnot.CleanDraw();
  755. CTextAttribute textAttribute = textWidget.FreeTextDa;
  756. byte transparency = textWidget.GetTransparency();
  757. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  758. Color textColor = Color.FromArgb(
  759. transparency,
  760. textAttribute.FontColor[0],
  761. textAttribute.FontColor[1],
  762. textAttribute.FontColor[2]);
  763. Color borderColor = Colors.Transparent;
  764. Color backgroundColor = Colors.White;
  765. byte[] colorArray = new byte[3];
  766. if (textWidget.Transparency > 0)
  767. {
  768. borderColor = Color.FromRgb(textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  769. }
  770. if (textWidget.HasBgColor)
  771. {
  772. backgroundColor = Color.FromRgb(textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  773. }
  774. textui.Foreground = new SolidColorBrush(textColor);
  775. textui.Background = new SolidColorBrush(backgroundColor);
  776. textBorder.Padding = new Thickness(0);
  777. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  778. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
  779. textui.BorderThickness = new Thickness(0);
  780. textui.Text = textWidget.Content;
  781. textui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  782. textui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  783. textui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  784. textui.AcceptsReturn = true;
  785. textui.TextWrapping = TextWrapping.Wrap;
  786. //textui.VerticalContentAlignment = VerticalAlignment.Center;
  787. textui.TextAlignment = TextAlignment.Left;
  788. switch (textWidget.Alignment)
  789. {
  790. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  791. textui.TextAlignment = TextAlignment.Left;
  792. break;
  793. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  794. textui.TextAlignment = TextAlignment.Right;
  795. break;
  796. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  797. textui.TextAlignment = TextAlignment.Center;
  798. break;
  799. default:
  800. break;
  801. }
  802. //SetFormRotateTransform(textui, annotData);
  803. // Set the width and height of the TextBox, rotation, and other control position information
  804. RotateTransform rotateTrans = new RotateTransform();
  805. rotateTrans.Angle = -90 * annotData.Rotation;
  806. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  807. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  808. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  809. textBorder.Width = rotateRect.Width;
  810. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  811. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  812. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  813. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  814. rotateTrans.Angle = 90 * annotData.Rotation;
  815. rotateTrans.CenterX = rotateRect.Width / 2;
  816. rotateTrans.CenterY = rotateRect.Height / 2;
  817. textBorder.RenderTransform = rotateTrans;
  818. }
  819. }
  820. catch
  821. {
  822. }
  823. }
  824. public void RemovePopTextUI()
  825. {
  826. if (textBorder == null)
  827. {
  828. return;
  829. }
  830. BaseLayer removeLayer = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  831. removeLayer.Children.Remove(textBorder);
  832. }
  833. public bool HitTestBorder()
  834. {
  835. if (textBorder == null)
  836. {
  837. return false;
  838. }
  839. Point pagePosition = Mouse.GetPosition(textBorder);
  840. HitTestResult hitTestResult = VisualTreeHelper.HitTest(textBorder, pagePosition);
  841. if (hitTestResult != null && hitTestResult.VisualHit != null)
  842. {
  843. return true;
  844. }
  845. return false;
  846. }
  847. }
  848. }