AnnotToolContentViewModel.Command.cs 42 KB

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