CPDFViewerTool.Annot.cs 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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, index);
  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.Bottom, (float)copyRect.Right, (float)copyRect.Top));
  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. case C_ANNOTATION_TYPE.C_ANNOTATION_LINK:
  521. {
  522. LinkAnnotHistory linkHistory = new LinkAnnotHistory();
  523. AnnotParam annotParam = ParamConverter.AnnotConverter(cPDFDocument, annotCore);
  524. linkHistory.Action = HistoryAction.Add;
  525. linkHistory.CurrentParam = (LinkParam)annotParam;
  526. linkHistory.PDFDoc = cPDFDocument;
  527. linkHistory.Action = HistoryAction.Add;
  528. historyGroup.Histories.Add(linkHistory);
  529. }
  530. break;
  531. default:
  532. break;
  533. }
  534. annotCore.ReleaseAnnot();
  535. }
  536. }
  537. if (historyGroup.Histories.Count > 0)
  538. {
  539. GetCPDFViewer()?.UndoManager?.AddHistory(historyGroup);
  540. }
  541. PDFViewer.UpdateAnnotFrame();
  542. historyData = historyGroup;
  543. return true;
  544. }
  545. public Point GetStartPoint()
  546. {
  547. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  548. return (baseLayer as CreateAnnotTool).GetStartPoint();
  549. }
  550. public Point GetEndPoint()
  551. {
  552. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  553. return (baseLayer as CreateAnnotTool).GetEndPoint();
  554. }
  555. public List<Point> GetInkDrawPoints()
  556. {
  557. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  558. return (baseLayer as CreateAnnotTool).GetInkDrawPoints();
  559. }
  560. public List<Point> GetMeasureDrawPoints()
  561. {
  562. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  563. return (baseLayer as CreateAnnotTool).GetMeasureDrawPoints();
  564. }
  565. public Rect GetDrawAnnotMaxRect()
  566. {
  567. if (UnCheckAnnotViewerModel())
  568. {
  569. return new Rect();
  570. }
  571. Point point = Mouse.GetPosition(this);
  572. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  573. return (baseLayer as CreateAnnotTool).GetMaxRect();
  574. }
  575. public void ClearDrawAnnot()
  576. {
  577. if (UnCheckAnnotViewerModel())
  578. {
  579. return;
  580. }
  581. Point point = Mouse.GetPosition(this);
  582. BaseLayer baseLayer = PDFViewer.GetViewForTag(createAnnotTag);
  583. (baseLayer as CreateAnnotTool).ClearDraw();
  584. }
  585. protected void UpdateTextPop()
  586. {
  587. if (textBorder != null)
  588. {
  589. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  590. if (currentAnnot == null)
  591. {
  592. return;
  593. }
  594. AnnotLayer annotLayer = PDFViewer.GetViewForTag(PDFViewer.GetAnnotViewTag()) as AnnotLayer;
  595. bool isOk = annotLayer.GetUpdate(ref currentAnnot);
  596. if (!isOk)
  597. {
  598. return;
  599. }
  600. AnnotData annotData = currentAnnot.GetAnnotData();
  601. if (annotData.PaintRect == Rect.Empty)
  602. {
  603. return;
  604. }
  605. //SetFormRotateTransform(textui, annotData);
  606. // Set the width and height of the TextBox, rotation, and other control position information
  607. RotateTransform rotateTrans = new RotateTransform();
  608. rotateTrans.Angle = -90 * annotData.Rotation;
  609. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  610. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  611. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  612. textBorder.Width = rotateRect.Width;
  613. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  614. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  615. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  616. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  617. }
  618. }
  619. protected void BuildPopTextUI(BaseAnnot textAnnot)
  620. {
  621. try
  622. {
  623. if (textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  624. {
  625. AnnotData annotData = textAnnot.GetAnnotData();
  626. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  627. if (textWidget == null)
  628. {
  629. return;
  630. }
  631. textAnnot.CleanDraw();
  632. CRect drawRect = textWidget.GetRect();
  633. double Width = DpiHelper.PDFNumToStandardNum(drawRect.width());
  634. double Height = DpiHelper.PDFNumToStandardNum(drawRect.height());
  635. textui = new TextBox();
  636. textui.Name = "PdfViewerTextBox";
  637. textBorder = new DashedBorder();
  638. textBorder.Child = textui;
  639. textBorder.MinWidth = Width * PDFViewer.GetZoom();
  640. textBorder.MinHeight = Height * PDFViewer.GetZoom();
  641. // Calculate the maximum value
  642. double PDFWidth = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Width;
  643. double PDFHeight = PDFViewer.GetCurrentRenderPageForIndex(annotData.PageIndex).PaintRect.Height;
  644. textBorder.MaxWidth = Math.Max(Width,(PDFWidth - annotData.VisualRect.Left - annotData.CropLeft));
  645. textBorder.MaxHeight =Math.Max(Height,(PDFHeight - annotData.VisualRect.Top - annotData.CropTop));
  646. CTextAttribute textAttribute = textWidget.FreeTextDa;
  647. byte transparency = textWidget.GetTransparency();
  648. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  649. Color textColor = Color.FromArgb(
  650. transparency,
  651. textAttribute.FontColor[0],
  652. textAttribute.FontColor[1],
  653. textAttribute.FontColor[2]);
  654. Color borderColor = Colors.Transparent;
  655. Color backgroundColor = Colors.Transparent;
  656. byte[] colorArray = new byte[3];
  657. if (textWidget.Transparency > 0)
  658. {
  659. borderColor = Color.FromArgb(textWidget.Transparency, textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  660. }
  661. if (textWidget.HasBgColor)
  662. {
  663. backgroundColor = Color.FromArgb(textWidget.Transparency, textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  664. }
  665. textui.Foreground = new SolidColorBrush(textColor);
  666. textui.Background = new SolidColorBrush(backgroundColor);
  667. textBorder.Padding = new Thickness(0);
  668. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  669. double rawWidth = textWidget.GetBorderWidth();
  670. double drawWidth = DpiHelper.PDFNumToStandardNum(rawWidth * annotData.CurrentZoom);
  671. textBorder.BorderThickness = new Thickness(drawWidth);
  672. textui.BorderThickness = new Thickness(0);
  673. textui.Text = textWidget.Content;
  674. if (textWidget.BorderStyle != C_BORDER_STYLE.BS_SOLID && textWidget.Dash != null && textWidget.Dash.Length > 0)
  675. {
  676. //补充保存虚线样式
  677. DashedBorder dashBorder = textBorder as DashedBorder;
  678. DoubleCollection dashCollection = new DoubleCollection();
  679. foreach (float num in textWidget.Dash)
  680. {
  681. dashCollection.Add(num);
  682. }
  683. dashBorder?.DrawDashBorder(true, drawWidth, rawWidth, dashCollection);
  684. }
  685. //string fontName = string.Empty;
  686. //string fontFamily = string.Empty;
  687. //CPDFFont.GetFamilyStyleName(textWidget.FreeTextDa.FontName, ref fontFamily, ref fontName);
  688. textui.FontFamily = new FontFamily(textWidget.FreeTextDa.FontName + ",Microsoft YaHei");
  689. textui.AcceptsReturn = true;
  690. textui.TextWrapping = TextWrapping.Wrap;
  691. textui.TextAlignment = TextAlignment.Left;
  692. switch (textWidget.Alignment)
  693. {
  694. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  695. textui.TextAlignment = TextAlignment.Left;
  696. break;
  697. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  698. textui.TextAlignment = TextAlignment.Right;
  699. break;
  700. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  701. textui.TextAlignment = TextAlignment.Center;
  702. break;
  703. default:
  704. break;
  705. }
  706. //SetFormRotateTransform(textui, annotData);
  707. // Set the width and height of the TextBox, rotation, and other control position information
  708. RotateTransform rotateTrans = new RotateTransform();
  709. rotateTrans.Angle = -90 * annotData.Rotation;
  710. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  711. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  712. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  713. textBorder.Width = rotateRect.Width;
  714. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  715. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  716. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  717. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  718. rotateTrans.Angle = 90 * annotData.Rotation;
  719. rotateTrans.CenterX = rotateRect.Width / 2;
  720. rotateTrans.CenterY = rotateRect.Height / 2;
  721. textBorder.RenderTransform = rotateTrans;
  722. textui.Loaded += (object sender, RoutedEventArgs e) =>
  723. {
  724. textui.Focus();
  725. textui.CaretIndex = textui.Text.Length;
  726. textui.SetValue(PopupAttachDataProperty, textAnnot);
  727. };
  728. CPDFViewer viewer = GetCPDFViewer();
  729. textui.LostFocus += (object sender, RoutedEventArgs e) =>
  730. {
  731. BaseAnnot currentAnnot = textui.GetValue(PopupAttachDataProperty) as BaseAnnot;
  732. if (currentAnnot != null)
  733. {
  734. AnnotData widgetData = currentAnnot.GetAnnotData();
  735. CPDFFreeTextAnnotation updateFreeText = widgetData.Annot as CPDFFreeTextAnnotation;
  736. if (textui.Text == string.Empty && updateFreeText.GetBorderWidth() == 0)
  737. {
  738. dynamic notifyData = null;
  739. notifyData = new ExpandoObject();
  740. notifyData.Action = HistoryAction.Remove;
  741. notifyData.PageIndex = widgetData.PageIndex;
  742. notifyData.AnnotIndex = widgetData.AnnotIndex;
  743. notifyData.AnnotType = widgetData.AnnotType;
  744. notifyData.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), widgetData.PageIndex, updateFreeText);
  745. updateFreeText.RemoveAnnot();
  746. AnnotChanged?.Invoke(this, notifyData);
  747. }
  748. else
  749. {
  750. FreeTextAnnotHistory history = null;
  751. if (updateFreeText.Content != textui.Text)
  752. {
  753. history = new FreeTextAnnotHistory();
  754. history.PDFDoc = viewer.GetDocument();
  755. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  756. history.Action = HistoryAction.Update;
  757. updateFreeText.SetContent(textui.Text);
  758. }
  759. RotateTransform rotateTranss = new RotateTransform();
  760. rotateTranss.Angle = -90 * annotData.Rotation;
  761. rotateTranss.CenterX = textBorder.ActualWidth / 2;
  762. rotateTranss.CenterY = textBorder.ActualHeight / 2;
  763. Rect textRect = rotateTranss.TransformBounds(new Rect(0, 0, textBorder.ActualWidth, textBorder.ActualHeight));
  764. Rect changeRect = new Rect(
  765. annotData.ClientRect.Left,
  766. annotData.ClientRect.Top,
  767. DpiHelper.StandardNumToPDFNum(textRect.Width / annotData.CurrentZoom),
  768. DpiHelper.StandardNumToPDFNum(textRect.Height / annotData.CurrentZoom));
  769. updateFreeText.SetRect(new CRect(
  770. (float)changeRect.Left,
  771. (float)changeRect.Bottom,
  772. (float)changeRect.Right,
  773. (float)changeRect.Top
  774. ));
  775. updateFreeText.UpdateAp();
  776. if (history != null)
  777. {
  778. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(viewer.GetDocument(), updateFreeText.Page.PageIndex, updateFreeText);
  779. viewer.UndoManager?.AddHistory(history);
  780. viewer.UndoManager?.InvokeHistoryChanged(this, new KeyValuePair<ComPDFKitViewer.Helper.UndoAction, IHistory>(ComPDFKitViewer.Helper.UndoAction.Custom, history));
  781. }
  782. viewer.UpdateAnnotFrame();
  783. }
  784. RemovePopTextUI();
  785. }
  786. };
  787. BaseLayer createAnnotTool = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  788. if (createAnnotTool != null)
  789. {
  790. createAnnotTool.Children.Add(textBorder);
  791. createAnnotTool.Arrange();
  792. }
  793. textui.LayoutUpdated += (object sender, EventArgs e) =>
  794. {
  795. createAnnotTool.Arrange();
  796. };
  797. PopupControlInfo attachInfo=new PopupControlInfo();
  798. attachInfo.Annot = textAnnot;
  799. attachInfo.PopUpControl=textui;
  800. PopUpControlHandler?.Invoke(this, attachInfo);
  801. }
  802. }
  803. catch (Exception ex)
  804. {
  805. }
  806. }
  807. public void UpdatePopTextUI(BaseAnnot textAnnot)
  808. {
  809. try
  810. {
  811. if (textui != null && textBorder != null && textAnnot != null && textAnnot.CurrentType == C_ANNOTATION_TYPE.C_ANNOTATION_FREETEXT)
  812. {
  813. AnnotData annotData = textAnnot.GetAnnotData();
  814. CPDFFreeTextAnnotation textWidget = annotData.Annot as CPDFFreeTextAnnotation;
  815. if (textWidget == null)
  816. {
  817. return;
  818. }
  819. textAnnot.CleanDraw();
  820. CTextAttribute textAttribute = textWidget.FreeTextDa;
  821. byte transparency = textWidget.GetTransparency();
  822. textui.FontSize = DpiHelper.PDFNumToStandardNum(textAttribute.FontSize * annotData.CurrentZoom);
  823. Color textColor = Color.FromArgb(
  824. transparency,
  825. textAttribute.FontColor[0],
  826. textAttribute.FontColor[1],
  827. textAttribute.FontColor[2]);
  828. Color borderColor = Colors.Transparent;
  829. Color backgroundColor = Colors.White;
  830. byte[] colorArray = new byte[3];
  831. if (textWidget.Transparency > 0)
  832. {
  833. borderColor = Color.FromRgb(textWidget.LineColor[0], textWidget.LineColor[1], textWidget.LineColor[2]);
  834. }
  835. if (textWidget.HasBgColor)
  836. {
  837. backgroundColor = Color.FromRgb(textWidget.BgColor[0], textWidget.BgColor[1], textWidget.BgColor[2]);
  838. }
  839. textui.Foreground = new SolidColorBrush(textColor);
  840. textui.Background = new SolidColorBrush(backgroundColor);
  841. textBorder.Padding = new Thickness(0);
  842. textBorder.BorderBrush = new SolidColorBrush(borderColor);
  843. textBorder.BorderThickness = new Thickness(DpiHelper.PDFNumToStandardNum(textWidget.GetBorderWidth() * annotData.CurrentZoom));
  844. textui.BorderThickness = new Thickness(0);
  845. if (string.IsNullOrEmpty(textui.Text) == false)
  846. {
  847. textui.CaretIndex = textui.Text.Length;
  848. }
  849. textui.FontFamily = new FontFamily(GetFontName(textAttribute.FontName));
  850. textui.FontWeight = IsBold(textAttribute.FontName) ? FontWeights.Bold : FontWeights.Normal;
  851. textui.FontStyle = IsItalic(textAttribute.FontName) ? FontStyles.Italic : FontStyles.Normal;
  852. textui.AcceptsReturn = true;
  853. textui.TextWrapping = TextWrapping.Wrap;
  854. //textui.VerticalContentAlignment = VerticalAlignment.Center;
  855. textui.TextAlignment = TextAlignment.Left;
  856. switch (textWidget.Alignment)
  857. {
  858. case C_TEXT_ALIGNMENT.ALIGNMENT_LEFT:
  859. textui.TextAlignment = TextAlignment.Left;
  860. break;
  861. case C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT:
  862. textui.TextAlignment = TextAlignment.Right;
  863. break;
  864. case C_TEXT_ALIGNMENT.ALIGNMENT_CENTER:
  865. textui.TextAlignment = TextAlignment.Center;
  866. break;
  867. default:
  868. break;
  869. }
  870. //SetFormRotateTransform(textui, annotData);
  871. // Set the width and height of the TextBox, rotation, and other control position information
  872. RotateTransform rotateTrans = new RotateTransform();
  873. rotateTrans.Angle = -90 * annotData.Rotation;
  874. rotateTrans.CenterX = annotData.PaintRect.Width / 2;
  875. rotateTrans.CenterY = annotData.PaintRect.Height / 2;
  876. Rect rotateRect = rotateTrans.TransformBounds(annotData.PaintRect);
  877. textBorder.Width = rotateRect.Width;
  878. textui.MinHeight = Math.Max(0, textui.FontSize + 8);
  879. textui.MaxHeight = Math.Max(0, annotData.PaintOffset.Bottom - annotData.PaintRect.Top - 8);
  880. textBorder.SetValue(Canvas.LeftProperty, annotData.PaintRect.Left + rotateTrans.CenterX - rotateRect.Width / 2);
  881. textBorder.SetValue(Canvas.TopProperty, annotData.PaintRect.Top + rotateTrans.CenterY - rotateRect.Height / 2);
  882. rotateTrans.Angle = 90 * annotData.Rotation;
  883. rotateTrans.CenterX = rotateRect.Width / 2;
  884. rotateTrans.CenterY = rotateRect.Height / 2;
  885. textBorder.RenderTransform = rotateTrans;
  886. }
  887. }
  888. catch
  889. {
  890. }
  891. }
  892. public void RemovePopTextUI()
  893. {
  894. if (textBorder == null)
  895. {
  896. return;
  897. }
  898. BaseLayer removeLayer = PDFViewer?.GetView(createAnnotTag) as CreateAnnotTool;
  899. removeLayer.Children.Remove(textBorder);
  900. }
  901. public bool HitTestBorder()
  902. {
  903. if (textBorder == null)
  904. {
  905. return false;
  906. }
  907. Point pagePosition = Mouse.GetPosition(textBorder);
  908. HitTestResult hitTestResult = VisualTreeHelper.HitTest(textBorder, pagePosition);
  909. if (hitTestResult != null && hitTestResult.VisualHit != null)
  910. {
  911. return true;
  912. }
  913. return false;
  914. }
  915. }
  916. }