CPDFViewerTool.Annot.cs 43 KB

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