AnnotToolContentViewModel.Command.cs 51 KB

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