AnnotToolContentViewModel.Command.cs 48 KB

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