AnnotToolContentViewModel.Command.cs 51 KB

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