AnnotToolContentViewModel.Command.cs 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument.Action;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.AnnotEvent;
  6. using ComPDFKitViewer.PdfViewer;
  7. using PDF_Office.CustomControl;
  8. using PDF_Office.EventAggregators;
  9. using PDF_Office.Helper;
  10. using PDF_Office.Properties;
  11. using PDF_Office.ViewModels.BOTA;
  12. using PDF_Office.Views.BOTA;
  13. using PDFSettings;
  14. using Prism.Mvvm;
  15. using Prism.Regions;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Text;
  20. using System.Threading.Tasks;
  21. using System.Windows;
  22. using System.Windows.Controls;
  23. using System.Windows.Controls.Primitives;
  24. using System.Windows.Media;
  25. using System.Diagnostics;
  26. using PDF_Office.Model.AnnotPanel;
  27. using System.Windows.Input;
  28. namespace PDF_Office.ViewModels.Tools
  29. {
  30. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  31. {
  32. #region 事件绑定和解绑
  33. private void BindingEvent()
  34. {
  35. //属性面板与注释工具栏绑定:属性面板的属性变化,会同步注释工具栏的属性值
  36. //比如在属性面板里更改注释颜色,会同时更新工具栏对应的工具颜色
  37. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  38. propertyPanel.DataChanged += AnnotPropertyPanel_DataChanged;
  39. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  40. propertyPanel.DefaultStored += AnnotProperty_DefaultStored;
  41. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  42. propertyPanel.AnnotTypeChanged += AnnotPropertyPanel_AnnotTypeChanged;
  43. //快捷键
  44. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  45. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  46. }
  47. private void UnBindingEvent()
  48. {
  49. propertyPanel.DataChanged -= AnnotPropertyPanel_DataChanged;
  50. propertyPanel.DefaultStored -= AnnotProperty_DefaultStored;
  51. propertyPanel.AnnotTypeChanged -= AnnotPropertyPanel_AnnotTypeChanged;
  52. //快捷盘解绑
  53. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  54. }
  55. private void BindingPDFViewerHandler()
  56. {
  57. //来自PDFViewer的响应事件
  58. if (PDFViewer != null)
  59. {
  60. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  61. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  62. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  63. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  64. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  65. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  66. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  67. PDFViewer.WidgetClickHander += PDFViewer_WidgetClickHander;
  68. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  69. PDFViewer.SnapshotCommandHandler += PDFViewer_SnapshotCommandHandler;
  70. PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler;
  71. PDFViewer.PDFActionHandler += PDFViewer_PDFActionHandler;
  72. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  73. PDFViewer.AnnotHoverHandler += PDFViewer_AnnotHoverHandler;
  74. }
  75. }
  76. private void PDFViewer_PDFActionHandler(object sender, ComPDFKit.PDFDocument.Action.CPDFAction action)
  77. {
  78. if (action == null)
  79. {
  80. return;
  81. }
  82. switch (action.ActionType)
  83. {
  84. case C_ACTION_TYPE.ACTION_TYPE_GOTO:
  85. if (PDFViewer != null)
  86. {
  87. CPDFGoToAction gotoAction = action as CPDFGoToAction;
  88. CPDFDestination dest = gotoAction.GetDestination(PDFViewer.Document);
  89. if (dest != null)
  90. {
  91. PDFViewer.GoToPage(dest.PageIndex, new System.Windows.Point(0, 0));
  92. }
  93. }
  94. break;
  95. case C_ACTION_TYPE.ACTION_TYPE_URI:
  96. {
  97. CPDFUriAction uriAction = action as CPDFUriAction;
  98. string uri = uriAction.GetUri();
  99. try
  100. {
  101. if (!string.IsNullOrEmpty(uri))
  102. {
  103. Process.Start(uri);
  104. }
  105. }
  106. catch (Exception ex)
  107. {
  108. }
  109. }
  110. break;
  111. }
  112. }
  113. private void UnBindingPDFViewerHandler()
  114. {
  115. if (PDFViewer != null)
  116. {
  117. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  118. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  119. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  120. PDFViewer.WidgetClickHander -= PDFViewer_WidgetClickHander;
  121. PDFViewer.SnapshotCommandHandler -= PDFViewer_SnapshotCommandHandler;
  122. PDFViewer.PDFActionHandler -= PDFViewer_PDFActionHandler;
  123. PDFViewer.AnnotHoverHandler -= PDFViewer_AnnotHoverHandler;
  124. }
  125. }
  126. #endregion 事件绑定和解绑
  127. #region 与触发事件调用相关的函数
  128. //是否为形状注释
  129. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  130. {
  131. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  132. annot.EventType == AnnotArgsType.AnnotSquare ||
  133. annot.EventType == AnnotArgsType.AnnotLine
  134. )
  135. {
  136. return true;
  137. }
  138. else
  139. {
  140. return false;
  141. }
  142. }
  143. //是否为高亮注释
  144. private bool isHightAnnot(AnnotHandlerEventArgs annot)
  145. {
  146. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  147. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  148. annot.EventType == AnnotArgsType.AnnotHighlight ||
  149. annot.EventType == AnnotArgsType.AnnotStrikeout
  150. )
  151. {
  152. return true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. private void GetSelectedAnnots(AnnotAttribEvent e)
  160. {
  161. var annot = e.AnnotItemsList[0];
  162. switch (annot.EventType)
  163. {
  164. case AnnotArgsType.AnnotHighlight:
  165. GetHighLight(e.AnnotItemsList);
  166. break;
  167. case AnnotArgsType.AnnotUnderline:
  168. GetUnderLine(e.AnnotItemsList);
  169. break;
  170. case AnnotArgsType.AnnotStrikeout:
  171. GetStrikeout(e.AnnotItemsList);
  172. break;
  173. case AnnotArgsType.AnnotSquiggly:
  174. GetSquiggly(e.AnnotItemsList);
  175. break;
  176. case AnnotArgsType.AnnotFreehand:
  177. GetFreehand(e.AnnotItemsList);
  178. break;
  179. case AnnotArgsType.AnnotFreeText:
  180. GetFreetext(e.AnnotItemsList);
  181. break;
  182. case AnnotArgsType.AnnotSquare:
  183. GetRect(e.AnnotItemsList);
  184. break;
  185. case AnnotArgsType.AnnotCircle:
  186. GetCircle(e.AnnotItemsList);
  187. break;
  188. case AnnotArgsType.AnnotLine:
  189. bool isLine = true;
  190. if (e.Attribs.ContainsKey(AnnotAttrib.LineStart))
  191. {
  192. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineStart] != C_LINE_TYPE.LINETYPE_NONE)
  193. {
  194. isLine = false;
  195. }
  196. }
  197. if (e.Attribs.ContainsKey(AnnotAttrib.LineEnd))
  198. {
  199. if ((C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_UNKNOWN && (C_LINE_TYPE)e.Attribs[AnnotAttrib.LineEnd] != C_LINE_TYPE.LINETYPE_NONE)
  200. {
  201. isLine = false;
  202. }
  203. }
  204. if (isLine)
  205. GetArrowLine("Line", e.AnnotItemsList);
  206. else
  207. GetArrowLine("Arrow", e.AnnotItemsList);
  208. break;
  209. case AnnotArgsType.AnnotLink:
  210. //viewContentViewModel.IsCreateLink = false;
  211. //e.IsAnnotCreateReset= true;
  212. if (btnLinkIsChecked == false)
  213. {
  214. propertyPanel.IsAddLink = false;
  215. }
  216. GetLink(e.AnnotItemsList, e);
  217. break;
  218. case AnnotArgsType.AnnotSticky:
  219. GetStickyNote(e.AnnotItemsList);
  220. customStickyPopup.GetCurrentAnnot = e.AnnotItemsList[0] as StickyAnnotArgs;
  221. customStickyPopup.GetPDFViewer = PDFViewer;
  222. break;
  223. case AnnotArgsType.AnnotStamp:
  224. GetStamp();
  225. break;
  226. }
  227. }
  228. #endregion 与触发事件调用相关的函数
  229. #region PDFViewer事件
  230. //选中和非选中注释
  231. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  232. {
  233. if (e != null)
  234. {
  235. var annot = e.AnnotItemsList[0];
  236. if (annot != null)
  237. {
  238. if (e.AnnotItemsList.Count == 1)
  239. {
  240. //IsAnnotCreateReset:是否为创建注释的状态
  241. if (e.IsAnnotCreateReset == false)
  242. {
  243. GetSelectedAnnots(e);
  244. }
  245. else
  246. {
  247. switch (annot.EventType)
  248. {
  249. case AnnotArgsType.AnnotLink:
  250. //viewContentViewModel.IsCreateLink = false;
  251. GetLink(e.AnnotItemsList, e);
  252. PDFViewer.SetToolParam(annot);
  253. break;
  254. }
  255. //TODO: 修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  256. //注释代码原因:设计已经重新调整逻辑:所有注释创建后,编辑属性,所有属性会作用于下次创建的注释中。
  257. //if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  258. //annot.EventType != AnnotArgsType.AnnotUnderline &&
  259. //annot.EventType != AnnotArgsType.AnnotHighlight &&
  260. //annot.EventType != AnnotArgsType.AnnotSquiggly &&
  261. //annot.EventType != AnnotArgsType.AnnotLink &&
  262. //annot.EventType != AnnotArgsType.AnnotFreehand &&
  263. //annot.EventType != AnnotArgsType.AnnotSticky &&
  264. //)
  265. //{
  266. // if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  267. // {
  268. // var strLineAnnotTag = "";
  269. // if (e.AnnotItemsList[0] is LineAnnotArgs)
  270. // {
  271. // var lineAnnot = e.AnnotItemsList[0] as LineAnnotArgs;
  272. // if (lineAnnot.HeadLineType == C_LINE_TYPE.LINETYPE_NONE && lineAnnot.TailLineType == C_LINE_TYPE.LINETYPE_NONE)
  273. // {
  274. // strLineAnnotTag = "Line";
  275. // }
  276. // else
  277. // {
  278. // strLineAnnotTag = "Arrow";
  279. // }
  280. // }
  281. // foreach (var item in ToolExpandDict)
  282. // {
  283. // if (item.Value == e.AnnotItemsList[0].EventType)
  284. // {
  285. // annot = null;//新建注释时,回到默认值
  286. // if (string.IsNullOrEmpty(strLineAnnotTag))
  287. // {
  288. // FindAnnotTypeKey(item.Key, ref annot);
  289. // break;
  290. // }
  291. // else
  292. // {
  293. // if (strLineAnnotTag == item.Key)
  294. // {
  295. // FindAnnotTypeKey(item.Key, ref annot);
  296. // break;
  297. // }
  298. // }
  299. // }
  300. // }
  301. // PDFViewer.SetToolParam(annot);
  302. // }
  303. //}
  304. #region TO DO
  305. //设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
  306. //if (isRightMenuAddAnnot && annot.EventType!=AnnotArgsType.AnnotLink)
  307. //{
  308. // ShowPropertyPanel(false);
  309. //}
  310. //else
  311. //{
  312. // ShowPropertyPanel();
  313. //}
  314. #endregion TO DO
  315. }
  316. }
  317. else
  318. {
  319. bool isDifferentAnnotTyle = false;
  320. var lastAnnot = annot;
  321. foreach (var item in e.AnnotItemsList)
  322. {
  323. if (lastAnnot.EventType != item.EventType)
  324. {
  325. if ((isShapAnnot(annot) == true && isShapAnnot(item) == true) || (isHightAnnot(annot) == true && isHightAnnot(item) == true))
  326. {
  327. lastAnnot = item;
  328. continue;
  329. }
  330. lastAnnot = item;
  331. isDifferentAnnotTyle = true;
  332. break;
  333. }
  334. }
  335. if (isDifferentAnnotTyle)
  336. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  337. else
  338. GetSelectedAnnots(e);
  339. }
  340. }
  341. //注释列表同步选中
  342. var list = e.GetPageAnnotsIndex();
  343. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  344. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true && bOTAContent.TabItemAnnotation.IsSelected == true && list != null && list.Count > 0)
  345. {
  346. if (list.Keys.Count == 0)
  347. {
  348. return;
  349. }
  350. var pageindex = new List<int>(list.Keys);
  351. List<int> annotes = new List<int>();
  352. list.TryGetValue(pageindex[0], out annotes);
  353. int annoteindex = annotes[0];
  354. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  355. if (viewModel != null)
  356. {
  357. viewModel.ScrollToAnnot(pageindex[0], annoteindex, annotation.AnnotationList);
  358. }
  359. }
  360. }
  361. else
  362. {
  363. if (BtnLinkIsChecked == false)
  364. {
  365. if (PDFViewer.MouseMode != MouseModes.AnnotCreate)
  366. {
  367. if (PDFViewer.MouseMode == MouseModes.PanTool && propertyPanel.IsAddLink == false && propertyPanel.IsLocationLink == false)
  368. {
  369. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  370. }
  371. }
  372. }
  373. }
  374. }
  375. //在注释工具的状态下,右键菜单
  376. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  377. {
  378. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  379. return;
  380. switch (e.CommandType)
  381. {
  382. case CommandType.Context:
  383. if (e.AnnotEventArgsList.Count > 0)
  384. {
  385. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  386. {
  387. //绑定标记密文处右键菜单
  388. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  389. }
  390. else
  391. {
  392. if (e.AnnotEventArgsList.Count == 1)
  393. {
  394. var selectedAnnot = e.AnnotEventArgsList[0];
  395. PopMenuCheckedLineDash(selectedAnnot);
  396. PopMenuCheckedFontFamily(selectedAnnot);
  397. PopMenuCheckedTextAglin(selectedAnnot);
  398. switch (selectedAnnot.EventType)
  399. {
  400. case AnnotArgsType.AnnotHighlight:
  401. case AnnotArgsType.AnnotUnderline:
  402. case AnnotArgsType.AnnotStrikeout:
  403. case AnnotArgsType.AnnotSquiggly:
  404. if (selectedAnnot.EventType == AnnotArgsType.AnnotHighlight)
  405. {
  406. colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Highlight);
  407. }
  408. else
  409. {
  410. colorContent.ItemSource = AnnotColorList.GetColorList(ColorSelectorType.Border);
  411. }
  412. e.PopupMenu = HightAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  413. break;
  414. case AnnotArgsType.AnnotFreehand:
  415. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  416. break;
  417. case AnnotArgsType.AnnotFreeText:
  418. e.PopupMenu = FreeTextAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  419. break;
  420. case AnnotArgsType.AnnotSticky:
  421. e.PopupMenu = StrickNoteAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  422. break;
  423. case AnnotArgsType.AnnotSquare:
  424. case AnnotArgsType.AnnotCircle:
  425. ShapeAnnotPopMenu.SetVisual("ShapeDirect", false);
  426. ShapeAnnotPopMenu.SetVisual("ShapeFillColor", true);
  427. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  428. break;
  429. case AnnotArgsType.AnnotLine:
  430. ShapeAnnotPopMenu.SetVisual("ShapeFillColor", false);
  431. ShapeAnnotPopMenu.SetVisual("ShapeDirect", true);
  432. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  433. break;
  434. case AnnotArgsType.AnnotLink:
  435. e.PopupMenu = LinkAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  436. break;
  437. case AnnotArgsType.AnnotStamp:
  438. e.PopupMenu = StampAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  439. break;
  440. }
  441. }
  442. else
  443. {
  444. bool isHigh = true;//是否为高亮
  445. foreach (var item in e.AnnotEventArgsList)
  446. {
  447. if (isHightAnnot(item) == false)
  448. {
  449. isHigh = false;
  450. break;
  451. }
  452. }
  453. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  454. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  455. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  456. }
  457. }
  458. if (e.PopupMenu != null)
  459. {
  460. e.Handle = true;
  461. }
  462. }
  463. else
  464. {
  465. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  466. {
  467. e.PopupMenu = SelectedTextOrImageContextMenu(sender, e);
  468. if (e.PopupMenu != null)
  469. {
  470. e.Handle = true;
  471. }
  472. }
  473. else
  474. {
  475. e.PopupMenu = ViewerContextMenu(sender);
  476. if (e.PopupMenu != null)
  477. {
  478. e.Handle = true;
  479. }
  480. }
  481. }
  482. break;
  483. }
  484. }
  485. //右键菜单,选中字体样式按钮状态
  486. private void PopMenuCheckedFontFamily(AnnotHandlerEventArgs annot)
  487. {
  488. if (annot != null)
  489. {
  490. if (annot.EventType == AnnotArgsType.AnnotFreeText)
  491. {
  492. var freeText = annot as FreeTextAnnotArgs;
  493. FreeTextAnnotPopMenu.SetIsChecked(freeText.FontFamily.ToString(), true);
  494. }
  495. }
  496. }
  497. //右键菜单,选中文本内容对齐按钮状态
  498. private void PopMenuCheckedTextAglin(AnnotHandlerEventArgs annot)
  499. {
  500. if (annot != null)
  501. {
  502. if (annot.EventType == AnnotArgsType.AnnotFreeText)
  503. {
  504. var freeText = annot as FreeTextAnnotArgs;
  505. switch (freeText.Align)
  506. {
  507. case TextAlignment.Left:
  508. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", true);
  509. break;
  510. case TextAlignment.Center:
  511. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", true);
  512. break;
  513. case TextAlignment.Right:
  514. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", true);
  515. break;
  516. case TextAlignment.Justify:
  517. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", true);
  518. break;
  519. default:
  520. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinLeft", false);
  521. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinCenter", false);
  522. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinRight", false);
  523. FreeTextAnnotPopMenu.SetIsChecked("FreeTextAglinJustify", false);
  524. break;
  525. }
  526. }
  527. }
  528. }
  529. //右键菜单,选中虚实线按钮状态
  530. private void PopMenuCheckedLineDash(AnnotHandlerEventArgs annot)
  531. {
  532. if (annot != null)
  533. {
  534. bool isSolidLineDash = true;
  535. if (annot.EventType == AnnotArgsType.AnnotFreehand)
  536. {
  537. var freeHand = annot as FreehandAnnotArgs;
  538. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(freeHand.LineDash);
  539. FreeHandAnnotPopMenu.SetIsChecked(isSolidLineDash ? "FreeHandSolid" : "FreeHandDash", true);
  540. }
  541. else if (annot.EventType == AnnotArgsType.AnnotSquare)
  542. {
  543. var square = annot as SquareAnnotArgs;
  544. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(square.LineDash);
  545. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  546. }
  547. else if (annot.EventType == AnnotArgsType.AnnotCircle)
  548. {
  549. var circle = annot as CircleAnnotArgs;
  550. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(circle.LineDash);
  551. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  552. }
  553. else if (annot.EventType == AnnotArgsType.AnnotLine)
  554. {
  555. var line = annot as LineAnnotArgs;
  556. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(line.LineDash);
  557. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  558. }
  559. }
  560. }
  561. /// <summary>
  562. /// 内容选择工具
  563. /// </summary>
  564. /// <param name="sender"></param>
  565. /// <param name="e"></param>
  566. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  567. {
  568. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  569. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  570. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  571. SnapshotEditMenuViewModel.SnapToolEvent -= SnapshotEditMenuViewModel_SnapToolEvent; ;
  572. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent;
  573. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  574. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  575. {
  576. e.PopupMenu = popMenu;
  577. e.Handle = true;
  578. if (popMenu != null && popMenu.Items.Count == 5)
  579. {
  580. //复制
  581. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  582. menuItem.CommandTarget = PDFViewer;
  583. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  584. //导出
  585. menuItem = popMenu.Items[1] as MenuItem;
  586. menuItem.CommandTarget = PDFViewer;
  587. if (menuItem.Items.Count == 3)
  588. {
  589. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  590. menuItem1.CommandTarget = PDFViewer;
  591. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  592. menuItem1 = menuItem.Items[1] as MenuItem;
  593. menuItem1.CommandTarget = PDFViewer;
  594. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  595. menuItem1 = menuItem.Items[2] as MenuItem;
  596. menuItem1.CommandTarget = PDFViewer;
  597. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  598. }
  599. //裁剪
  600. menuItem = popMenu.Items[2] as MenuItem;
  601. menuItem.CommandTarget = PDFViewer;
  602. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  603. //缩放至所选区域
  604. menuItem = popMenu.Items[3] as MenuItem;
  605. menuItem.CommandTarget = PDFViewer;
  606. menuItem.Visibility = Visibility.Collapsed;
  607. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  608. //打印
  609. menuItem = popMenu.Items[4] as MenuItem;
  610. menuItem.CommandTarget = PDFViewer;
  611. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  612. }
  613. }
  614. }
  615. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  616. {
  617. switch (e.Key)
  618. {
  619. case "CloseSnap":
  620. {
  621. #region to do
  622. //var item = PDFViewerTab.SelectedItem as TabItem;
  623. //if (item == null)
  624. //{
  625. // ClearSelectedToolPanel();
  626. // return;
  627. //}
  628. //Grid grid = item.Content as Grid;
  629. //if (grid == null || grid.Children.Count == 0)
  630. //{
  631. // ClearSelectedToolPanel();
  632. // return;
  633. //}
  634. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  635. //if (pdfViewer == null)
  636. //{
  637. // ClearSelectedToolPanel();
  638. // return;
  639. //}
  640. #endregion to do
  641. switch (PDFViewer.MouseMode)
  642. {
  643. case MouseModes.SelectTextTool:
  644. break;
  645. default:
  646. BtnSelecttoolIsChecked = false;
  647. BtnHandIsChecked = true;
  648. PDFViewer.EnableZoom(true);
  649. PDFViewer.EnableScroll(true);
  650. break;
  651. }
  652. }
  653. break;
  654. default:
  655. break;
  656. }
  657. }
  658. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  659. {
  660. if (e != null && e.Count > 0)
  661. {
  662. for (int i = 0; i < e.Count; i++)
  663. {
  664. AnnotEditEvent editEvent = e[i];
  665. switch (editEvent.EditAction)
  666. {
  667. case ActionType.Add:
  668. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  669. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  670. {
  671. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  672. if (viewModel != null)
  673. {
  674. int pageindex = editEvent.PageIndex;
  675. int annotindex = editEvent.AnnotIndex;
  676. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  677. }
  678. }
  679. var annot = e[0].EditAnnotArgs;
  680. if (
  681. annot.EventType == AnnotArgsType.AnnotSquare ||
  682. annot.EventType == AnnotArgsType.AnnotCircle ||
  683. annot.EventType == AnnotArgsType.AnnotLine ||
  684. annot.EventType == AnnotArgsType.AnnotStamp
  685. )
  686. {
  687. PDFViewer.ClearSelectAnnots();
  688. PDFViewer.SelectAnnotation(annot.PageIndex, annot.AnnotIndex);
  689. }
  690. break;
  691. case ActionType.Del:
  692. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  693. if (isTabItemAnnotation)
  694. {
  695. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  696. if (viewModel != null)
  697. {
  698. int pageindex = editEvent.PageIndex;
  699. int annotindex = editEvent.AnnotIndex;
  700. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  701. }
  702. }
  703. break;
  704. case ActionType.Modify:
  705. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  706. if (bOTAContent.TabItemAnnotation.IsSelected)
  707. {
  708. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  709. if (viewModel != null)
  710. {
  711. int pageindex = editEvent.PageIndex;
  712. int annotindex = editEvent.AnnotIndex;
  713. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  714. }
  715. }
  716. break;
  717. case ActionType.TextEdit:
  718. break;
  719. default:
  720. break;
  721. }
  722. }
  723. //空白处 右键菜单 添加注释 为单次添加,不连续添加
  724. if (IsNoSelectMenu)
  725. {
  726. IsNoSelectMenu = false;
  727. PDFViewer.SetMouseMode(MouseModes.PanTool);
  728. }
  729. }
  730. }
  731. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  732. {
  733. }
  734. private void PDFViewer_AnnotHoverHandler(object sender, AnnotHoverData e)
  735. {
  736. if (e != null && e.DrawContext != null)
  737. {
  738. Rect hoverRect = new Rect(
  739. e.PaintRect.Left - 2,
  740. e.PaintRect.Top - 2,
  741. e.PaintRect.Width + 4,
  742. e.PaintRect.Height + 4);
  743. Pen hoverPen = new Pen(new SolidColorBrush(Color.FromArgb(0xff, 0x11, 0x8A, 0xff)), 1);
  744. hoverPen.DashStyle = DashStyles.Dash;
  745. e.DrawContext?.DrawRectangle(null, hoverPen, hoverRect);
  746. //Todo:目前ComPDFKit.Viewer没有e.Annot参数
  747. //便签,显示ToolTip内容
  748. //if (e.Annot != null && e.Annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  749. //{
  750. // if (PDFViewer.ToolTip == null)
  751. // {
  752. // var content = e.Annot.GetContent();
  753. // if (string.IsNullOrEmpty(content) == false)
  754. // {
  755. // ToolTip TipChild = new ToolTip();
  756. // TipChild.Style = App.Current.Resources["FlowToolTip"] as Style;
  757. // TipChild.MaxWidth = 246;
  758. // TipChild.Content = content;
  759. // TipChild.Visibility = Visibility.Visible;
  760. // TipChild.IsOpen = true;
  761. // TipChild.Placement = PlacementMode.Right;
  762. // var rec = e.Annot.GetRect();
  763. // TipChild.PlacementRectangle = new Rect(rec.left, rec.top, rec.right, rec.bottom);
  764. // TipChild.PlacementTarget = PDFViewer.Parent as ContentControl;
  765. // TipChild.Placement = PlacementMode.MousePoint;
  766. // PDFViewer.ToolTip = TipChild;
  767. // oldRect = hoverRect;
  768. // PDFViewer.MouseMove -= PDFViewer_MouseMove;
  769. // PDFViewer.MouseMove += PDFViewer_MouseMove;
  770. // return;
  771. // }
  772. // }
  773. //}
  774. }
  775. CloseAnnotToolTip();
  776. }
  777. private Rect oldRect = new Rect(0, 0, 0, 0);
  778. private void CloseAnnotToolTip()
  779. {
  780. if (PDFViewer.ToolTip != null && PDFViewer.ToolTip is ToolTip)
  781. {
  782. ToolTip oldTips = (ToolTip)PDFViewer.ToolTip;
  783. oldTips.IsOpen = false;
  784. oldTips.Visibility = Visibility.Collapsed;
  785. PDFViewer.ToolTip = null;
  786. PDFViewer.MouseMove -= PDFViewer_MouseMove;
  787. oldRect = new Rect(0, 0, 0, 0);
  788. }
  789. }
  790. private void PDFViewer_MouseMove(object sender, MouseEventArgs e)
  791. {
  792. if (sender != null && PDFViewer.ToolTip != null && PDFViewer.Parent != null)
  793. {
  794. var newPoint = e.GetPosition(PDFViewer.Parent as ContentControl);
  795. var isOutw = newPoint.X > (oldRect.X + oldRect.Width + 4);
  796. var isOuth = newPoint.Y > (oldRect.Y + oldRect.Height + 4);
  797. if (newPoint.X < oldRect.X || newPoint.Y < oldRect.Y || isOutw || isOuth)
  798. {
  799. CloseAnnotToolTip();
  800. }
  801. }
  802. }
  803. #endregion PDFViewer事件
  804. #region BindingEvent事件
  805. private void AnnotProperty_DefaultStored(object sender, object e)
  806. {
  807. }
  808. //同一属性面板,不同的注释类型
  809. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  810. {
  811. if (e != null)
  812. {
  813. AnnotHandlerEventArgs annotArgs = null;
  814. foreach (AnnotArgsType argsType in e.Keys)
  815. {
  816. switch (argsType)
  817. {
  818. case AnnotArgsType.AnnotSquare:
  819. annotArgs = GetRect();
  820. break;
  821. case AnnotArgsType.AnnotCircle:
  822. annotArgs = GetCircle();
  823. break;
  824. case AnnotArgsType.AnnotLine:
  825. var LineTag = e[argsType] as string;
  826. annotArgs = GetArrowLine(LineTag);
  827. break;
  828. }
  829. if (annotArgs != null)
  830. {
  831. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  832. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  833. PDFViewer.SetToolParam(annotArgs);
  834. }
  835. ShowPropertyPanel();
  836. }
  837. }
  838. }
  839. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  840. {
  841. if (e != null)
  842. {
  843. foreach (AnnotArgsType argsType in e.Keys)
  844. {
  845. switch (argsType)
  846. {
  847. case AnnotArgsType.AnnotHighlight:
  848. if (e[argsType] is Color)
  849. {
  850. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  851. }
  852. if (e[argsType] is double)
  853. {
  854. HighLightOpacity = (double)e[argsType];
  855. }
  856. break;
  857. case AnnotArgsType.AnnotUnderline:
  858. if (e[argsType] is Color)
  859. {
  860. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  861. }
  862. if (e[argsType] is double)
  863. {
  864. underLineOpacity = (double)e[argsType];
  865. }
  866. break;
  867. case AnnotArgsType.AnnotSquiggly:
  868. if (e[argsType] is Color)
  869. {
  870. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  871. }
  872. if (e[argsType] is double)
  873. {
  874. SquigglyOpacity = (double)e[argsType];
  875. }
  876. break;
  877. case AnnotArgsType.AnnotStrikeout:
  878. if (e[argsType] is Color)
  879. {
  880. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  881. }
  882. if (e[argsType] is double)
  883. {
  884. StrikeoutOpacity = (double)e[argsType];
  885. }
  886. break;
  887. case AnnotArgsType.AnnotFreehand:
  888. if (e[argsType] is FreehandAnnotArgs)
  889. {
  890. var annot = e[argsType] as FreehandAnnotArgs;
  891. if (annot != null)
  892. {
  893. if (propertyPanel != null)
  894. {
  895. //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值
  896. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  897. propertyPanel.AnnotEvent = AnnotEvent;
  898. var AnnotEvents = new List<AnnotAttribEvent>();
  899. AnnotEvents.Add(AnnotEvent);
  900. propertyPanel.AnnotEvents = AnnotEvents;
  901. propertyPanel.annot = annot;
  902. //手绘注释工具按钮的属性
  903. // FreehandPath.Opacity = annot.Transparency;
  904. // FreehandPath.Fill = new SolidColorBrush(annot.InkColor);
  905. }
  906. PDFViewer.SetToolParam(annot);
  907. }
  908. }
  909. break;
  910. case AnnotArgsType.AnnotErase:
  911. if (e[argsType] is ToggleButton)
  912. {
  913. ToggleButton clickBtn = e[argsType] as ToggleButton;
  914. if (clickBtn.IsChecked == true)
  915. {
  916. if (clickBtn.Tag.ToString() == "PenBtn")
  917. {
  918. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  919. btn.Tag = "Freehand"; btn.IsChecked = true;
  920. BtnMyTools_Click(btn);
  921. break;
  922. }
  923. EraseArgs eraseArgs = new EraseArgs();
  924. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  925. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  926. eraseArgs.Thickness = 10;
  927. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  928. if (annotProperty != null)
  929. {
  930. eraseArgs.Thickness = annotProperty.Thickness;
  931. }
  932. PDFViewer.ClearSelectAnnots(false);
  933. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  934. PDFViewer.SetToolParam(eraseArgs);
  935. List<AnnotHandlerEventArgs> eraseArgsList = new List<AnnotHandlerEventArgs>();
  936. if (eraseArgs != null)
  937. eraseArgsList.Add(eraseArgs);
  938. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgsList);
  939. }
  940. }
  941. break;
  942. case AnnotArgsType.AnnotSquare:
  943. case AnnotArgsType.AnnotCircle:
  944. case AnnotArgsType.AnnotLine:
  945. case AnnotArgsType.AnnotFreeText:
  946. //创建注释后,修改注释属性,可用于下次创建的注释
  947. if (propertyPanel != null && propertyPanel.annot != null && PDFViewer.MouseMode == MouseModes.AnnotCreate)
  948. {
  949. PDFViewer.SetToolParam(propertyPanel.annot);
  950. }
  951. break;
  952. }
  953. }
  954. }
  955. }
  956. #endregion BindingEvent事件
  957. }
  958. }