CPDFViewerTool.Annot.cs 42 KB

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