AnnotToolContentViewModel.Command.cs 49 KB

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