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