AnnotToolContentViewModel.Command.cs 47 KB

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