CPDFViewerTool.Annot.cs 42 KB

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