AnnotToolContentViewModel.Command.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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. {
  358. if (PDFViewer.MouseMode == MouseModes.PanTool && propertyPanel.IsAddLink == false && propertyPanel.IsLocationLink == false)
  359. {
  360. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  361. }
  362. }
  363. }
  364. }
  365. }
  366. //在注释工具的状态下,右键菜单
  367. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  368. {
  369. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  370. return;
  371. switch (e.CommandType)
  372. {
  373. case CommandType.Context:
  374. if (e.AnnotEventArgsList.Count > 0)
  375. {
  376. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  377. {
  378. //绑定标记密文处右键菜单
  379. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  380. }
  381. else
  382. {
  383. if (e.AnnotEventArgsList.Count == 1)
  384. {
  385. var selectedAnnot = e.AnnotEventArgsList[0];
  386. PopMenuCheckedLineDash(selectedAnnot);
  387. switch (selectedAnnot.EventType)
  388. {
  389. case AnnotArgsType.AnnotHighlight:
  390. case AnnotArgsType.AnnotUnderline:
  391. case AnnotArgsType.AnnotStrikeout:
  392. case AnnotArgsType.AnnotSquiggly:
  393. if (selectedAnnot.EventType == AnnotArgsType.AnnotHighlight)
  394. {
  395. colorContent.ItemSource = AnnotColorList.GetHighlightColorList();
  396. }
  397. else
  398. {
  399. colorContent.ItemSource = AnnotColorList.GetBorderColorList();
  400. }
  401. e.PopupMenu = HightAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  402. break;
  403. case AnnotArgsType.AnnotFreehand:
  404. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  405. break;
  406. case AnnotArgsType.AnnotFreeText:
  407. e.PopupMenu = FreeTextAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  408. break;
  409. case AnnotArgsType.AnnotSticky:
  410. e.PopupMenu = StrickNoteAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  411. break;
  412. case AnnotArgsType.AnnotSquare:
  413. case AnnotArgsType.AnnotCircle:
  414. ShapeAnnotPopMenu.SetVisual("ShapeDirect", false);
  415. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  416. break;
  417. case AnnotArgsType.AnnotLine:
  418. ShapeAnnotPopMenu.SetVisual("ShapeDirect", true);
  419. e.PopupMenu = ShapeAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  420. break;
  421. case AnnotArgsType.AnnotLink:
  422. e.PopupMenu = LinkAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  423. break;
  424. case AnnotArgsType.AnnotStamp:
  425. e.PopupMenu = StampAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  426. break;
  427. }
  428. }
  429. else
  430. {
  431. bool isHigh = true;//是否为高亮
  432. foreach (var item in e.AnnotEventArgsList)
  433. {
  434. if (isHightAnnot(item) == false)
  435. {
  436. isHigh = false;
  437. break;
  438. }
  439. }
  440. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  441. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  442. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  443. }
  444. }
  445. if (e.PopupMenu != null)
  446. {
  447. e.Handle = true;
  448. }
  449. }
  450. else
  451. {
  452. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  453. {
  454. e.PopupMenu = NoneSelectAnnotContextMenu(sender, e);
  455. if (e.PopupMenu != null)
  456. {
  457. e.Handle = true;
  458. }
  459. }
  460. else
  461. {
  462. e.PopupMenu = ViewerContextMenu(sender);
  463. if (e.PopupMenu != null)
  464. {
  465. e.Handle = true;
  466. }
  467. }
  468. }
  469. break;
  470. }
  471. }
  472. //右键菜单,选中虚实线按钮状态
  473. private void PopMenuCheckedLineDash(AnnotHandlerEventArgs annot)
  474. {
  475. if(annot != null)
  476. {
  477. bool isSolidLineDash = true;
  478. if (annot.EventType == AnnotArgsType.AnnotFreehand)
  479. {
  480. var freeHand = annot as FreehandAnnotArgs;
  481. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(freeHand.LineDash);
  482. FreeHandAnnotPopMenu.SetIsChecked(isSolidLineDash ? "FreeHandSolid" : "FreeHandDash", true);
  483. }
  484. else if(annot.EventType == AnnotArgsType.AnnotSquare)
  485. {
  486. var square = annot as SquareAnnotArgs;
  487. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(square.LineDash);
  488. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  489. }
  490. else if (annot.EventType == AnnotArgsType.AnnotCircle)
  491. {
  492. var circle = annot as CircleAnnotArgs;
  493. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(circle.LineDash);
  494. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  495. }
  496. else if (annot.EventType == AnnotArgsType.AnnotLine)
  497. {
  498. var line = annot as LineAnnotArgs;
  499. isSolidLineDash = AnnotPropertyPanel.IsSolidStyle(line.LineDash);
  500. ShapeAnnotPopMenu.SetIsChecked(isSolidLineDash ? "ShapeSolid" : "ShapeDash", true);
  501. }
  502. }
  503. }
  504. /// <summary>
  505. /// 内容选择工具
  506. /// </summary>
  507. /// <param name="sender"></param>
  508. /// <param name="e"></param>
  509. private void PDFViewer_SnapshotCommandHandler(object sender, SnapshotCommandArgs e)
  510. {
  511. SnapshotEditToolArgs snapToolArgs = (SnapshotEditToolArgs)PDFViewer.ToolManager.CurrentAnnotArgs;
  512. SnapshotEditMenuViewModel.SnapToolArgs = snapToolArgs;
  513. SnapshotEditMenuViewModel.PDFViewer = PDFViewer;
  514. SnapshotEditMenuViewModel.SnapToolEvent -= SnapshotEditMenuViewModel_SnapToolEvent; ;
  515. SnapshotEditMenuViewModel.SnapToolEvent += SnapshotEditMenuViewModel_SnapToolEvent;
  516. var popMenu = App.Current.FindResource("SnapshotContextMenu") as ContextMenu;
  517. if (e.HitSnapshotTool && e.SnapshotRect.Width > 0 && e.SnapshotRect.Height > 0)
  518. {
  519. e.PopupMenu = popMenu;
  520. e.Handle = true;
  521. if (popMenu != null && popMenu.Items.Count == 5)
  522. {
  523. //复制
  524. MenuItem menuItem = popMenu.Items[0] as MenuItem;
  525. menuItem.CommandTarget = PDFViewer;
  526. menuItem.Command = SnapshotEditMenuViewModel.SnapCopyCommand;
  527. //导出
  528. menuItem = popMenu.Items[1] as MenuItem;
  529. menuItem.CommandTarget = PDFViewer;
  530. if (menuItem.Items.Count == 3)
  531. {
  532. MenuItem menuItem1 = menuItem.Items[0] as MenuItem;
  533. menuItem1.CommandTarget = PDFViewer;
  534. menuItem1.Command = SnapshotEditMenuViewModel.ExportPNGCommand;
  535. menuItem1 = menuItem.Items[1] as MenuItem;
  536. menuItem1.CommandTarget = PDFViewer;
  537. menuItem1.Command = SnapshotEditMenuViewModel.ExportJPGCommand;
  538. menuItem1 = menuItem.Items[2] as MenuItem;
  539. menuItem1.CommandTarget = PDFViewer;
  540. menuItem1.Command = SnapshotEditMenuViewModel.ExportPDFCommand;
  541. }
  542. //裁剪
  543. menuItem = popMenu.Items[2] as MenuItem;
  544. menuItem.CommandTarget = PDFViewer;
  545. menuItem.Command = SnapshotEditMenuViewModel.CroppingCommand;
  546. //缩放至所选区域
  547. menuItem = popMenu.Items[3] as MenuItem;
  548. menuItem.CommandTarget = PDFViewer;
  549. menuItem.Visibility = Visibility.Collapsed;
  550. //menuItem.Command = snapshotEditMenuViewModel.CroppingCommand;
  551. //打印
  552. menuItem = popMenu.Items[4] as MenuItem;
  553. menuItem.CommandTarget = PDFViewer;
  554. menuItem.Command = SnapshotEditMenuViewModel.PrintCommand;
  555. }
  556. }
  557. }
  558. private void SnapshotEditMenuViewModel_SnapToolEvent(object sender, KeyValuePair<string, object> e)
  559. {
  560. switch (e.Key)
  561. {
  562. case "CloseSnap":
  563. {
  564. #region to do
  565. //var item = PDFViewerTab.SelectedItem as TabItem;
  566. //if (item == null)
  567. //{
  568. // ClearSelectedToolPanel();
  569. // return;
  570. //}
  571. //Grid grid = item.Content as Grid;
  572. //if (grid == null || grid.Children.Count == 0)
  573. //{
  574. // ClearSelectedToolPanel();
  575. // return;
  576. //}
  577. //PDFViewerCtrl pdfViewer = grid.Children[0] as PDFViewerCtrl;
  578. //if (pdfViewer == null)
  579. //{
  580. // ClearSelectedToolPanel();
  581. // return;
  582. //}
  583. #endregion to do
  584. switch (PDFViewer.MouseMode)
  585. {
  586. case MouseModes.SelectTextTool:
  587. break;
  588. default:
  589. BtnSelecttoolIsChecked = false;
  590. BtnHandIsChecked = true;
  591. PDFViewer.EnableZoom(true);
  592. PDFViewer.EnableScroll(true);
  593. break;
  594. }
  595. }
  596. break;
  597. default:
  598. break;
  599. }
  600. }
  601. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  602. {
  603. if (e != null && e.Count > 0)
  604. {
  605. for (int i = 0; i < e.Count; i++)
  606. {
  607. AnnotEditEvent editEvent = e[i];
  608. switch (editEvent.EditAction)
  609. {
  610. case ActionType.Add:
  611. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  612. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  613. {
  614. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  615. if (viewModel != null)
  616. {
  617. int pageindex = editEvent.PageIndex;
  618. int annotindex = editEvent.AnnotIndex;
  619. viewModel.UpdateAddedAnnot(pageindex, annotindex);
  620. }
  621. }
  622. var annot = e[0].EditAnnotArgs;
  623. if (
  624. annot.EventType == AnnotArgsType.AnnotSquare ||
  625. annot.EventType == AnnotArgsType.AnnotCircle ||
  626. annot.EventType == AnnotArgsType.AnnotLine ||
  627. annot.EventType == AnnotArgsType.AnnotStamp
  628. )
  629. {
  630. PDFViewer.ClearSelectAnnots();
  631. PDFViewer.SelectAnnotation(annot.PageIndex, annot.AnnotIndex);
  632. }
  633. break;
  634. case ActionType.Del:
  635. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  636. if (isTabItemAnnotation)
  637. {
  638. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  639. if (viewModel != null)
  640. {
  641. int pageindex = editEvent.PageIndex;
  642. int annotindex = editEvent.AnnotIndex;
  643. viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  644. }
  645. }
  646. break;
  647. case ActionType.Modify:
  648. isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  649. if (bOTAContent.TabItemAnnotation.IsSelected)
  650. {
  651. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  652. if (viewModel != null)
  653. {
  654. int pageindex = editEvent.PageIndex;
  655. int annotindex = editEvent.AnnotIndex;
  656. viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  657. }
  658. }
  659. break;
  660. case ActionType.TextEdit:
  661. break;
  662. default:
  663. break;
  664. }
  665. }
  666. }
  667. }
  668. private void PDFViewer_WidgetClickHander(object sender, WidgetArgs e)
  669. {
  670. }
  671. private void PDFViewer_AnnotHoverHandler(object sender, AnnotHoverData e)
  672. {
  673. if (e != null && e.DrawContext != null)
  674. {
  675. Rect hoverRect = new Rect(
  676. e.PaintRect.Left - 2,
  677. e.PaintRect.Top - 2,
  678. e.PaintRect.Width + 4,
  679. e.PaintRect.Height + 4);
  680. Pen hoverPen = new Pen(new SolidColorBrush(Color.FromArgb(0xff, 0x11, 0x8A, 0xff)), 1);
  681. hoverPen.DashStyle = DashStyles.Dash;
  682. e.DrawContext?.DrawRectangle(null, hoverPen, hoverRect);
  683. //Todo:目前ComPDFKit.Viewer没有e.Annot参数
  684. //便签,显示ToolTip内容
  685. //if (e.Annot != null && e.Annot.Type == C_ANNOTATION_TYPE.C_ANNOTATION_TEXT)
  686. //{
  687. // if (PDFViewer.ToolTip == null)
  688. // {
  689. // var content = e.Annot.GetContent();
  690. // if (string.IsNullOrEmpty(content) == false)
  691. // {
  692. // ToolTip TipChild = new ToolTip();
  693. // TipChild.Style = App.Current.Resources["FlowToolTip"] as Style;
  694. // TipChild.MaxWidth = 246;
  695. // TipChild.Content = content;
  696. // TipChild.Visibility = Visibility.Visible;
  697. // TipChild.IsOpen = true;
  698. // TipChild.Placement = PlacementMode.Right;
  699. // var rec = e.Annot.GetRect();
  700. // TipChild.PlacementRectangle = new Rect(rec.left, rec.top, rec.right, rec.bottom);
  701. // TipChild.PlacementTarget = PDFViewer.Parent as ContentControl;
  702. // TipChild.Placement = PlacementMode.MousePoint;
  703. // PDFViewer.ToolTip = TipChild;
  704. // oldRect = hoverRect;
  705. // PDFViewer.MouseMove -= PDFViewer_MouseMove;
  706. // PDFViewer.MouseMove += PDFViewer_MouseMove;
  707. // return;
  708. // }
  709. // }
  710. //}
  711. }
  712. CloseAnnotToolTip();
  713. }
  714. Rect oldRect = new Rect(0, 0, 0, 0);
  715. private void CloseAnnotToolTip()
  716. {
  717. if (PDFViewer.ToolTip != null && PDFViewer.ToolTip is ToolTip)
  718. {
  719. ToolTip oldTips = (ToolTip)PDFViewer.ToolTip;
  720. oldTips.IsOpen = false;
  721. oldTips.Visibility = Visibility.Collapsed;
  722. PDFViewer.ToolTip = null;
  723. PDFViewer.MouseMove -= PDFViewer_MouseMove;
  724. oldRect = new Rect(0, 0, 0, 0);
  725. }
  726. }
  727. private void PDFViewer_MouseMove(object sender, MouseEventArgs e)
  728. {
  729. if (sender != null && PDFViewer.ToolTip != null && PDFViewer.Parent != null)
  730. {
  731. var newPoint = e.GetPosition(PDFViewer.Parent as ContentControl);
  732. var isOutw = newPoint.X > (oldRect.X + oldRect.Width + 4);
  733. var isOuth = newPoint.Y > (oldRect.Y + oldRect.Height + 4);
  734. if (newPoint.X < oldRect.X || newPoint.Y < oldRect.Y || isOutw || isOuth)
  735. {
  736. CloseAnnotToolTip();
  737. }
  738. }
  739. }
  740. #endregion PDFViewer事件
  741. #region BindingEvent事件
  742. private void AnnotProperty_DefaultStored(object sender, object e)
  743. {
  744. }
  745. private void AnnotPropertyPanel_AnnotTypeChanged(object sender, Dictionary<AnnotArgsType, object> e)
  746. {
  747. if (e != null)
  748. {
  749. AnnotHandlerEventArgs annotArgs = null;
  750. foreach (AnnotArgsType argsType in e.Keys)
  751. {
  752. switch (argsType)
  753. {
  754. case AnnotArgsType.AnnotSquare:
  755. annotArgs = GetRect();
  756. break;
  757. case AnnotArgsType.AnnotCircle:
  758. annotArgs = GetCircle();
  759. break;
  760. case AnnotArgsType.AnnotLine:
  761. var LineTag = e[argsType] as string;
  762. annotArgs = GetArrowLine(LineTag);
  763. break;
  764. }
  765. if (annotArgs != null)
  766. {
  767. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  768. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  769. PDFViewer.SetToolParam(annotArgs);
  770. }
  771. ShowPropertyPanel();
  772. }
  773. }
  774. }
  775. private void AnnotPropertyPanel_DataChanged(object sender, Dictionary<AnnotArgsType, object> e)
  776. {
  777. if (e != null)
  778. {
  779. foreach (AnnotArgsType argsType in e.Keys)
  780. {
  781. switch (argsType)
  782. {
  783. case AnnotArgsType.AnnotHighlight:
  784. if (e[argsType] is Color)
  785. {
  786. HighLightColor = new SolidColorBrush((Color)e[argsType]);
  787. }
  788. if (e[argsType] is double)
  789. {
  790. HighLightOpacity = (double)e[argsType];
  791. }
  792. break;
  793. case AnnotArgsType.AnnotUnderline:
  794. if (e[argsType] is Color)
  795. {
  796. UnderLineColor = new SolidColorBrush((Color)e[argsType]);
  797. }
  798. if (e[argsType] is double)
  799. {
  800. underLineOpacity = (double)e[argsType];
  801. }
  802. break;
  803. case AnnotArgsType.AnnotSquiggly:
  804. if (e[argsType] is Color)
  805. {
  806. SquigglyColor = new SolidColorBrush((Color)e[argsType]);
  807. }
  808. if (e[argsType] is double)
  809. {
  810. SquigglyOpacity = (double)e[argsType];
  811. }
  812. break;
  813. case AnnotArgsType.AnnotStrikeout:
  814. if (e[argsType] is Color)
  815. {
  816. StrikeoutColor = new SolidColorBrush((Color)e[argsType]);
  817. }
  818. if (e[argsType] is double)
  819. {
  820. StrikeoutOpacity = (double)e[argsType];
  821. }
  822. break;
  823. case AnnotArgsType.AnnotFreehand:
  824. if (e[argsType] is FreehandAnnotArgs)
  825. {
  826. var annot = e[argsType] as FreehandAnnotArgs;
  827. if (annot != null)
  828. {
  829. if (propertyPanel != null)
  830. {
  831. //属性面板,切换橡皮擦后,再回到画笔时,需要恢复最近画笔的属性值
  832. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  833. propertyPanel.AnnotEvent = AnnotEvent;
  834. var AnnotEvents = new List<AnnotAttribEvent>();
  835. AnnotEvents.Add(AnnotEvent);
  836. propertyPanel.AnnotEvents = AnnotEvents;
  837. propertyPanel.annot = annot;
  838. //手绘注释工具按钮的属性
  839. // FreehandPath.Opacity = annot.Transparency;
  840. // FreehandPath.Fill = new SolidColorBrush(annot.InkColor);
  841. }
  842. PDFViewer.SetToolParam(annot);
  843. }
  844. }
  845. break;
  846. case AnnotArgsType.AnnotErase:
  847. if (e[argsType] is ToggleButton)
  848. {
  849. ToggleButton clickBtn = e[argsType] as ToggleButton;
  850. if (clickBtn.IsChecked == true)
  851. {
  852. if (clickBtn.Tag.ToString() == "PenBtn")
  853. {
  854. CustomIconToggleBtn btn = new CustomIconToggleBtn();
  855. btn.Tag = "Freehand"; btn.IsChecked = true;
  856. BtnMyTools_Click(btn);
  857. break;
  858. }
  859. EraseArgs eraseArgs = new EraseArgs();
  860. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  861. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  862. eraseArgs.Thickness = 10;
  863. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotErase);
  864. if (annotProperty != null)
  865. {
  866. eraseArgs.Thickness = annotProperty.Thickness;
  867. }
  868. PDFViewer.ClearSelectAnnots(false);
  869. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  870. PDFViewer.SetToolParam(eraseArgs);
  871. List<AnnotHandlerEventArgs> eraseArgsList = new List<AnnotHandlerEventArgs>();
  872. if (eraseArgs != null)
  873. eraseArgsList.Add(eraseArgs);
  874. AddToPropertyPanel("FreehandAnnotProperty", "Freehand", eraseArgsList);
  875. }
  876. }
  877. break;
  878. }
  879. }
  880. }
  881. }
  882. #endregion BindingEvent事件
  883. }
  884. }