AnnotToolContentViewModel.Command.cs 51 KB

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