AnnotToolContentViewModel.Command.cs 46 KB

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