AnnotToolContentViewModel.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666
  1. using ComPDFKit.PDFAnnotation;
  2. using ComPDFKit.PDFDocument;
  3. using ComPDFKitViewer;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer.PdfViewer;
  6. using Microsoft.Win32;
  7. using PDF_Master.CustomControl;
  8. using PDF_Master.EventAggregators;
  9. using PDF_Master.Helper;
  10. using PDF_Master.Model;
  11. using PDF_Master.Model.BOTA;
  12. using PDF_Master.Properties;
  13. using PDF_Master.ViewModels.BOTA;
  14. using PDF_Master.ViewModels.PropertyPanel;
  15. using PDF_Master.ViewModels.PropertyPanel.AnnotPanel;
  16. using PDF_Master.ViewModels.Tools.AnnotManager;
  17. using PDF_Master.Views.BOTA;
  18. using PDF_Master.Views.PropertyPanel.AnnotPanel;
  19. using PDFSettings;
  20. using PDFSettings.Settings;
  21. using Prism.Commands;
  22. using Prism.Events;
  23. using Prism.Mvvm;
  24. using Prism.Regions;
  25. using Prism.Services.Dialogs;
  26. using System;
  27. using System.Collections.Generic;
  28. using System.IO;
  29. using System.Linq;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Threading.Tasks;
  33. using System.Windows;
  34. using System.Windows.Annotations;
  35. using System.Windows.Controls;
  36. using System.Windows.Controls.Primitives;
  37. using System.Windows.Forms;
  38. using System.Windows.Input;
  39. using System.Windows.Media;
  40. using System.Windows.Media.Imaging;
  41. using ContextMenu = System.Windows.Controls.ContextMenu;
  42. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  43. using MenuItem = System.Windows.Controls.MenuItem;
  44. namespace PDF_Master.ViewModels.Tools
  45. {
  46. public sealed partial class AnnotToolContentViewModel : BindableBase, INavigationAware
  47. {
  48. #region 初始化
  49. public AnnotToolContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
  50. {
  51. region = regionManager;
  52. events = eventAggregator;
  53. dialogs = dialogService;
  54. InitCommand();
  55. InitDefaultValue();
  56. InitToolDict();
  57. InitToolTipDict();
  58. InitPopMenu();
  59. SubscribeEvent();
  60. }
  61. //事件聚合器
  62. private void SubscribeEvent()
  63. {
  64. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  65. events.GetEvent<FillAndSignEvent>().Subscribe(FromFillAndSign, e => e.AppUnicode == Unicode);
  66. }
  67. private void InitPopMenu()
  68. {
  69. InitSelectHightAnnotMenu();
  70. InitSelectFreeHandAnnotMenu();
  71. InitSelectFreeTextAnnotMenu();
  72. InitSelectStrickNoteAnnotMenu();
  73. InitSelectShapeAnnotMenu();
  74. InitSelectLinkAnnotMenu();
  75. InitSelectStampAnnotMenu();
  76. InitSelectMultiAnnotMenu();
  77. }
  78. private void InitCommand()
  79. {
  80. MyToolsCommand = new DelegateCommand<CustomIconToggleBtn>(BtnMyTools_Click);
  81. SetAddAnnotationCommand = new DelegateCommand<object>(AddAnnotation_Click);
  82. AddBookMarkCommand = new DelegateCommand(AddBookMark_Click);
  83. PropertyRegionName = Guid.NewGuid().ToString();
  84. HandCommand = new DelegateCommand(Hand_Click);
  85. #region 注释 - 右键菜单
  86. //公共command
  87. AnnotDefaultValue_MenuCommand = new DelegateCommand<object>(AnnotDefaultValues_Menu);
  88. AnnotColorPalette_MenuCommand = new DelegateCommand<object>(AnnotColorPalette_Menu);
  89. AnnotAddNoteText_MenuCommand = new DelegateCommand<object>(AnnotAddNoteText_Menu);
  90. //高亮、下划线、删除
  91. HightAnnotCopyText_MenuCommand = new DelegateCommand<object>(HightAnnotCopyText_Menu);
  92. //手绘
  93. FreeHandLineStyle_MenuCommand = new DelegateCommand<object>(FreeHandLineStyle_Menu);
  94. //文本
  95. FreeTextFontFamily_MenuCommand = new DelegateCommand<object>(FreeTextFontFamily_Menu);
  96. FreeTextAglin_MenuCommand = new DelegateCommand<object>(FreeTextAglin_Menu);
  97. //便签
  98. StrikeNoteEditStrike_MenuCommand = new DelegateCommand<object>(StrikeNoteEditStrike_Menu);
  99. //形状
  100. ShapeLineStyle_MenuCommand = new DelegateCommand<object>(ShapeLineStyle_Menu);
  101. ShapeLineDirect_MenuCommand = new DelegateCommand<object>(ShapeLineDirect_Menu);
  102. //图章
  103. StampExportPicture_MenuCommand = new DelegateCommand<object>(StampExportPicture_Menu);
  104. //链接
  105. Link_MenuCommand = new DelegateCommand<object>(Link_Menu);
  106. #endregion 注释 - 右键菜单
  107. }
  108. #endregion 初始化
  109. #region 注释工具栏 -》Command实现
  110. //手型工具
  111. private void Hand_Click()
  112. {
  113. HandToolIsCheckedEvent(BtnHandIsChecked);
  114. if (BtnHandIsChecked)
  115. {
  116. //其余注释工具 取消选中
  117. StrAnnotToolChecked = "";
  118. BtnLinkIsChecked = false;
  119. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  120. }
  121. }
  122. //手型工具按钮选中或取消
  123. private void HandToolIsCheckedEvent(bool isChecked)
  124. {
  125. if (isChecked == false)
  126. {
  127. //鼠标框选多选注释
  128. if (PDFViewer.MouseMode != MouseModes.AnnotCreate)
  129. {
  130. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  131. var annotArgs = new SelectToolArgs();
  132. PDFViewer.SetToolParam(annotArgs);
  133. }
  134. else
  135. {
  136. PDFViewer.SetMouseMode(MouseModes.PanTool);
  137. }
  138. }
  139. else
  140. {
  141. //鼠标滚动页面
  142. PDFViewer.SetMouseMode(MouseModes.Scroll);
  143. PDFViewer.SetToolParam(new AnnotHandlerEventArgs());
  144. }
  145. }
  146. //点击注释工具
  147. private void BtnMyTools_Click(CustomIconToggleBtn annotBtn)
  148. {
  149. if (annotBtn == null || annotBtn.Tag == null) return;
  150. //不创建注释,属于注释模板
  151. Dictionary<string, bool> dictVar = new Dictionary<string, bool>();
  152. dictVar.Add("isTemplateAnnot", false);
  153. dictVar.Add("isSnapshotEdit", false);
  154. AnnotHandlerEventArgs annotArgs = null;
  155. var tag = annotBtn.Tag.ToString();
  156. if (annotBtn.Name == "BtnShowAnnot")
  157. {
  158. //显示/隐藏注释
  159. PDFViewer.SetDrawAnnot((bool)annotBtn.IsChecked);
  160. return;
  161. }
  162. if (annotBtn.IsChecked == true)
  163. {
  164. propertyPanel.SaveLastAnnot();
  165. MyToolCheckedDoing(dictVar, tag);
  166. FindAnnotTypeKey(tag, ref annotArgs);
  167. if (annotArgs != null)
  168. {
  169. //设置点击页面会创建对应选中注释工具的注释
  170. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  171. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  172. PDFViewer.SetToolParam(annotArgs);
  173. }
  174. }
  175. else
  176. {
  177. //取消选中注释工具按钮后,恢复到未编辑注释的状态
  178. propertyPanel.IsAddLink = false;
  179. propertyPanel.IsLocationLink = false;
  180. PDFViewer.ToolManager.EnableClickCreate = false;
  181. ShowPropertyPanel(false);
  182. }
  183. if (dictVar["isTemplateAnnot"] == false && annotArgs == null)
  184. {
  185. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  186. HandToolIsCheckedEvent(false);
  187. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  188. }
  189. else
  190. {
  191. if (dictVar["isSnapshotEdit"] == true)
  192. {
  193. ShowPropertyPanel(false);
  194. }
  195. else
  196. {
  197. ShowPropertyPanel((bool)annotBtn.IsChecked);
  198. }
  199. }
  200. }
  201. //选中注释工具按钮时,处理的一些系列事情
  202. private void MyToolCheckedDoing(Dictionary<string, bool> dict, string tag)
  203. {
  204. if (BtnHandIsChecked)
  205. BtnHandIsChecked = false;
  206. PDFViewer.ClearSelectAnnots();
  207. //选中内容选择工具按钮
  208. if (tag == "SnapshotEdit")
  209. {
  210. dict["isSnapshotEdit"] = true;
  211. }
  212. else if (tag == "Signature" || tag == "Stamp")
  213. {
  214. //选中签名、图章工具按钮
  215. dict["isTemplateAnnot"] = true;
  216. }
  217. //超链接工具
  218. if (tag != "Link")
  219. BtnLinkIsChecked = false;
  220. else
  221. StrAnnotToolChecked = "Link";
  222. if (tag == "Rect" || tag == "Circle" || tag == "Arrow" || tag == "Line")
  223. {
  224. //新建形状注释,设置默认大小
  225. PDFViewer.ToolManager.EnableClickCreate = true;
  226. PDFViewer.ToolManager.ClickCreateWidth = 140;
  227. PDFViewer.ToolManager.ClickCreateHeight = 140;
  228. }
  229. else
  230. {
  231. PDFViewer.ToolManager.EnableClickCreate = false;
  232. }
  233. }
  234. #endregion 注释工具栏 -》Command实现
  235. #region BOTA
  236. private AnnotationContentViewModel GetAnnotationContentViewModel(BOTAContentViewModel bOTAContentViewModel, out AnnotationContent annotation)
  237. {
  238. AnnotationContentViewModel viewModel = null;
  239. annotation = null;
  240. if (bOTAContentViewModel != null)
  241. {
  242. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  243. {
  244. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  245. foreach (var item in views)
  246. {
  247. if (item is AnnotationContent annotation1)
  248. {
  249. annotation = annotation1;
  250. viewModel = annotation1.DataContext as AnnotationContentViewModel;
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. return viewModel;
  257. }
  258. /// <summary>
  259. /// 判断Bota的TabItem是否已显示
  260. /// </summary>
  261. /// <param name="bOTAContentViewModel"></param>
  262. /// <param name="bOTAContent"></param>
  263. /// <param name="tabItemText"></param>
  264. /// <returns></returns>
  265. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  266. {
  267. bool isTabItem = false;
  268. bOTAContentViewModel = null;
  269. bOTAContent = null;
  270. if (region.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  271. {
  272. var views = region.Regions[viewContentViewModel.BOTARegionName].Views;
  273. var model = views.FirstOrDefault(q => q is BOTAContent);
  274. if (model is BOTAContent bOTAContent1)
  275. {
  276. bOTAContent = bOTAContent1;
  277. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  278. if (bOTAContentViewModel.CurrentBar == tabItemText)
  279. {
  280. isTabItem = true;
  281. }
  282. }
  283. }
  284. return isTabItem;
  285. }
  286. private void Select_Click(object sender, RoutedEventArgs e)
  287. {
  288. if (viewContentViewModel.OpenBOTA == false)
  289. {
  290. viewContentViewModel.OpenBOTA = true;
  291. }
  292. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  293. if (isBook == false)
  294. {
  295. bOTAContent.TabItemSearch.IsSelected = true;
  296. }
  297. }
  298. #endregion BOTA
  299. #region 阅读页右键菜单
  300. /// <summary>
  301. /// 空白处-右键菜单-添加注释
  302. /// </summary>
  303. /// <param name="sender"></param>
  304. private void AddAnnotation_Click(object sender)
  305. {
  306. if (sender is MenuItem menuItem)
  307. {
  308. AnnotHandlerEventArgs annotHandler = null;
  309. string str = menuItem.Tag.ToString();
  310. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  311. propertyPanel.IsAddLink = false;
  312. propertyPanel.IsLocationLink = false;
  313. if (str == AddAnnotType.AnnotFreehand.ToString())
  314. {
  315. annotHandler = GetFreehand();
  316. }
  317. if (str == AddAnnotType.AnnotFreeText.ToString())
  318. {
  319. annotHandler = GetFreetext();
  320. }
  321. if (str == AddAnnotType.AnnotSticky.ToString())
  322. {
  323. annotHandler = GetStickyNote();
  324. }
  325. if (str == AddAnnotType.AnnotSquare.ToString())
  326. {
  327. annotHandler = GetRect();
  328. }
  329. if (str == AddAnnotType.AnnotCircle.ToString())
  330. {
  331. annotHandler = GetCircle();
  332. }
  333. if (str == AddAnnotType.AnnotArrow.ToString())
  334. {
  335. annotHandler = GetArrowLine("Arrow");
  336. }
  337. if (str == AddAnnotType.AnnotLine.ToString())
  338. {
  339. annotHandler = GetArrowLine("Line");
  340. }
  341. if (str == AddAnnotType.AnnotLink.ToString())
  342. {
  343. propertyPanel.IsAddLink = true;
  344. annotHandler = GetLink();
  345. }
  346. if (str == AddAnnotType.AnnotStamp.ToString())
  347. {
  348. annotHandler = GetStamp();
  349. }
  350. if (str == AddAnnotType.AnnotAutograph.ToString())
  351. {
  352. annotHandler = GetSignature();
  353. }
  354. if (annotHandler != null)
  355. {
  356. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  357. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  358. PDFViewer.SetToolParam(annotHandler);
  359. //是否展开右侧面板 =>否(链接、图章、签名除外)
  360. ShowPropertyPanel(false);
  361. if (str == AddAnnotType.AnnotLink.ToString()
  362. || str == AddAnnotType.AnnotStamp.ToString()
  363. || str == AddAnnotType.AnnotAutograph.ToString()
  364. )
  365. {
  366. ShowPropertyPanel(true);
  367. }
  368. IsNoSelectMenu = true;
  369. }
  370. }
  371. }
  372. /// <summary>
  373. /// 选择文本、图片,右键菜单
  374. /// </summary>
  375. /// <param name="sender"></param>
  376. /// <param name="e"></param>
  377. private void AnnotToolMenu_Click(object sender, RoutedEventArgs e)
  378. {
  379. if (sender is MenuItem clickMenu && clickMenu.CommandParameter is AnnotCommandArgs)
  380. {
  381. AnnotCommandArgs annotCommand = clickMenu.CommandParameter as AnnotCommandArgs;
  382. switch (clickMenu.Tag.ToString())
  383. {
  384. case "HighLight"://字体高亮
  385. TextHighlightAnnotArgs highlightArgs = (TextHighlightAnnotArgs)GetHighLight();
  386. highlightArgs.ClientRect = annotCommand.TextRect;
  387. highlightArgs.Content = annotCommand.Text;
  388. highlightArgs.Author = Settings.Default.AppProperties.Description.Author;
  389. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  390. PDFViewer.SetToolParam(highlightArgs);
  391. PDFViewer.SetMouseMode(MouseModes.PanTool);
  392. break;
  393. case "UnderLine"://下划线
  394. TextUnderlineAnnotArgs underlineArgs = (TextUnderlineAnnotArgs)GetUnderLine();
  395. underlineArgs.ClientRect = annotCommand.TextRect;
  396. underlineArgs.Content = annotCommand.Text;
  397. underlineArgs.Author = Settings.Default.AppProperties.Description.Author;
  398. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  399. PDFViewer.SetToolParam(underlineArgs);
  400. PDFViewer.SetMouseMode(MouseModes.PanTool);
  401. break;
  402. case "Strikeout"://删除线
  403. TextStrikeoutAnnotArgs strikeoutArgs = (TextStrikeoutAnnotArgs)GetStrikeout();
  404. strikeoutArgs.ClientRect = annotCommand.TextRect;
  405. strikeoutArgs.Content = annotCommand.Text;
  406. strikeoutArgs.Author = Settings.Default.AppProperties.Description.Author;
  407. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  408. PDFViewer.SetToolParam(strikeoutArgs);
  409. PDFViewer.SetMouseMode(MouseModes.PanTool);
  410. break;
  411. case "Freetext"://文本
  412. FreeTextAnnotArgs textArgs = (FreeTextAnnotArgs)GetFreetext();
  413. textArgs.ClientRect = annotCommand.TextRect;
  414. //textArgs.Transparency = 1;
  415. //textArgs.BgColor = Colors.Transparent;
  416. //textArgs.LineColor = Colors.Red;
  417. //textArgs.LineWidth = 2;
  418. textArgs.TextContent = annotCommand.Text;
  419. PDFViewer.CreatePageAnnot(annotCommand.PageIndex, textArgs);
  420. break;
  421. case "StickyNote"://便签
  422. StickyAnnotArgs stickyAnnotArgs = (StickyAnnotArgs)GetStickyNote();
  423. //stickyAnnotArgs.ClientRect = annotCommand.TextRect;
  424. List<TextSelectNode> textSelectNodes = PDFViewer.GetSelectTextInfo();
  425. //stickyAnnotArgs.ClientRect = new Rect(textSelectNodes[0].StartPoint.X, annotCommand.TextRect.Y - annotCommand.TextRect.Height, annotCommand.TextRect.Width, annotCommand.TextRect.Height);
  426. Rect rect = annotCommand.TextRectList[0];
  427. #region to do 转dpi
  428. //int dpi = DpiHelpers.Dpi;
  429. //double x = rect.X / 72D * dpi;
  430. //double y = (rect.Y - rect.Height) / 72D * dpi;
  431. //double width = rect.Width / 72D * dpi;
  432. //double height = rect.Height / 72D * dpi;
  433. #endregion to do 转dpi
  434. //stickyAnnotArgs.ClientRect = new Rect(x, y, width, height);
  435. //stickyAnnotArgs.ClientRect = new Rect(rect.X, rect.Y - rect.Height, rect.Width, rect.Height);
  436. stickyAnnotArgs.ClientRect = rect;
  437. //PDFViewer.CreatePageAnnot(annotCommand.PageIndex, stickyAnnotArgs);
  438. NavigationParameters param = new NavigationParameters();
  439. param.Add(ParameterNames.PDFViewer, PDFViewer);
  440. param.Add(ParameterNames.StickyAnnotArgs, stickyAnnotArgs);
  441. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  442. param.Add(ParameterNames.AnnotCommandArgs, annotCommand);
  443. System.Windows.Controls.ContentControl window = null;
  444. if (PDFViewer.Parent as ContentControl != null)
  445. {
  446. window = PDFViewer.Parent as ContentControl;
  447. }
  448. else
  449. {
  450. window = App.Current.MainWindow;
  451. }
  452. var point = Mouse.GetPosition(window);
  453. viewContentViewModel.NotePopupVisible = Visibility.Visible;
  454. viewContentViewModel.CanvasNoteLeft = point.X;
  455. viewContentViewModel.CanvasNoteBottom = point.Y;
  456. if (point.Y > window.ActualHeight / 2)
  457. {
  458. viewContentViewModel.CanvasNoteBottom = point.Y - window.ActualHeight / 2;
  459. }
  460. viewContentViewModel.IsNoteAdd = true;
  461. region.RequestNavigate(viewContentViewModel.NotePopupRegionName, "StickyNotePopup", param);
  462. //stickyAnnotArgs.Author = Settings.Default.AppProperties.Description.Author;
  463. //PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  464. //PDFViewer.SetToolParam(stickyAnnotArgs);
  465. //PDFViewer.SetMouseMode(MouseModes.PanTool);
  466. break;
  467. case "Rect"://矩形
  468. SquareAnnotArgs squareAnnotArgs = (SquareAnnotArgs)GetRect();
  469. squareAnnotArgs.ClientRect = annotCommand.TextRect;
  470. PDFViewer.CreatePageAnnot(annotCommand.PageIndex, squareAnnotArgs);
  471. break;
  472. case "Circle"://圆
  473. CircleAnnotArgs circleAnnotArgs = (CircleAnnotArgs)GetCircle();
  474. circleAnnotArgs.ClientRect = annotCommand.TextRect;
  475. PDFViewer.CreatePageAnnot(annotCommand.PageIndex, circleAnnotArgs);
  476. break;
  477. case "Line"://线
  478. LineAnnotArgs lineAnnotArgs = (LineAnnotArgs)GetArrowLine(clickMenu.Tag.ToString());
  479. lineAnnotArgs.ClientRect = annotCommand.TextRect;
  480. lineAnnotArgs.LinePoints.Add(new Point(annotCommand.TextRect.Left, annotCommand.TextRect.Top));
  481. lineAnnotArgs.LinePoints.Add(new Point(annotCommand.TextRect.Right, annotCommand.TextRect.Bottom));
  482. PDFViewer.CreatePageAnnot(annotCommand.PageIndex, lineAnnotArgs);
  483. break;
  484. case "Link"://链接
  485. LinkAnnotArgs linkArgs = new LinkAnnotArgs();
  486. linkArgs.ClientRect = annotCommand.TextRect;
  487. linkArgs.URI = string.Empty;
  488. linkArgs.LinkType = LINK_TYPE.GOTO;
  489. linkArgs.DestIndex = annotCommand.PageIndex;
  490. PDFViewer.CreatePageAnnot(annotCommand.PageIndex, linkArgs);
  491. BtnLinkIsChecked = true;
  492. List<AnnotHandlerEventArgs> lists = new List<AnnotHandlerEventArgs>();
  493. lists.Add(linkArgs);
  494. propertyPanel.IsAddLink = true;
  495. AnnotHandlerEventArgs annotArgs = GetLink(lists);
  496. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  497. PDFViewer.SetToolParam(annotArgs);
  498. viewContentViewModel.IsPropertyOpen = true;
  499. break;
  500. case "OutLine":
  501. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemOutLine");
  502. if (isTabItemAnnotation == false)
  503. {
  504. bOTAContent.TabItemOutLine.IsSelected = true;
  505. }
  506. OutLineControlViewModel outLineControlViewModel = GetOutLineControlViewModel(bOTAContentViewModel, out OutLineControl outLineControl);
  507. if (outLineControlViewModel != null && outLineControl != null)
  508. {
  509. string str = null;
  510. if (string.IsNullOrEmpty(annotCommand.Text))
  511. {
  512. str = (annotCommand.PageIndex + 1).ToString();
  513. }
  514. else
  515. {
  516. str = annotCommand.Text;
  517. }
  518. outLineControl.AddOutlineNode(str);
  519. }
  520. break;
  521. case "ExportPicture":
  522. ExportPicture_MenuItemClick();
  523. break;
  524. }
  525. //ShowPropertyPanel(false);
  526. }
  527. }
  528. private void PageDisplay(ItemCollection items)
  529. {
  530. foreach (var item in items)
  531. {
  532. if (item is MenuItem menuItem)
  533. {
  534. menuItem.IsCheckable = false;
  535. menuItem.Click -= PageDisplay_Click;
  536. menuItem.Click += PageDisplay_Click;
  537. }
  538. }
  539. }
  540. private void PageDisplay_Click(object sender, RoutedEventArgs e)
  541. {
  542. if (sender is MenuItem menuItem)
  543. {
  544. switch (menuItem.Tag.ToString())
  545. {
  546. case "SingleView":
  547. PDFViewer.ChangeViewMode(ViewMode.Single);
  548. break;
  549. case "SingleContinueView":
  550. PDFViewer.ChangeViewMode(ViewMode.SingleContinuous);
  551. break;
  552. case "DoubleView":
  553. PDFViewer.ChangeViewMode(ViewMode.Double);
  554. break;
  555. case "DoubleContinueView":
  556. PDFViewer.ChangeViewMode(ViewMode.DoubleContinuous);
  557. break;
  558. case "BookMode":
  559. PDFViewer.ChangeViewMode(ViewMode.Book);
  560. break;
  561. }
  562. }
  563. }
  564. private void ViewZoom(ItemCollection items)
  565. {
  566. foreach (var item in items)
  567. {
  568. if (item is MenuItem menuItem)
  569. {
  570. //switch (PDFViewer.ModeFit)
  571. //{
  572. // case FitMode.FitWidth:
  573. // break;
  574. // case FitMode.FitHeight:
  575. // break;
  576. // case FitMode.FitSize:
  577. // break;
  578. // case FitMode.FitFree:
  579. // break;
  580. // default:
  581. // break;
  582. //}
  583. menuItem.IsCheckable = false;
  584. menuItem.Click -= ViewZoom_Click;
  585. menuItem.Click += ViewZoom_Click;
  586. }
  587. }
  588. }
  589. private void ViewZoom_Click(object sender, RoutedEventArgs e)
  590. {
  591. if (sender is MenuItem menuItem)
  592. {
  593. switch (menuItem.Tag.ToString())
  594. {
  595. case "FitWidth":
  596. PDFViewer.ChangeFitMode(FitMode.FitWidth);
  597. break;
  598. case "FitHeight":
  599. PDFViewer.ChangeFitMode(FitMode.FitHeight);
  600. break;
  601. case "FitSize":
  602. PDFViewer.ChangeFitMode(FitMode.FitSize);
  603. break;
  604. case "enlarge":
  605. double newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + 0.01), true);
  606. PDFViewer.Zoom(newZoom);
  607. break;
  608. case "narrow":
  609. newZoom = viewContentViewModel.CheckZoomLevel((PDFViewer.ZoomFactor + -0.01), false);
  610. PDFViewer.Zoom(newZoom);
  611. break;
  612. }
  613. }
  614. }
  615. private void UnReadModel_Click(object sender, RoutedEventArgs e)
  616. {
  617. viewContentViewModel.Off_ReadModel();
  618. }
  619. private void ReadModel_Click(object sender, RoutedEventArgs e)
  620. {
  621. viewContentViewModel.On_ReadMode();
  622. }
  623. private void SetToolMode(ItemCollection items)
  624. {
  625. foreach (var item in items)
  626. {
  627. if (item is MenuItem menuItem)
  628. {
  629. menuItem.IsCheckable = false;
  630. menuItem.Click -= ToolMode_Click;
  631. menuItem.Click += ToolMode_Click;
  632. }
  633. }
  634. }
  635. private void ToolMode_Click(object sender, RoutedEventArgs e)
  636. {
  637. if (sender is MenuItem menuItem)
  638. {
  639. switch (menuItem.Tag.ToString())
  640. {
  641. case "default":
  642. HandToolIsCheckedEvent(false);
  643. BtnHandIsChecked = false;
  644. ShowPropertyPanel(false);
  645. break;
  646. case "roll":
  647. HandToolIsCheckedEvent(true);
  648. BtnHandIsChecked = true;
  649. ShowPropertyPanel(false);
  650. break;
  651. case "content":
  652. AnnotHandlerEventArgs annotHandler = GetSnapshotEdit();
  653. BtnSelecttoolIsChecked = true;
  654. if (annotHandler != null)
  655. {
  656. annotHandler.Author = Settings.Default.AppProperties.Description.Author;
  657. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  658. PDFViewer.SetToolParam(annotHandler);
  659. ShowPropertyPanel(false);
  660. }
  661. break;
  662. default:
  663. break;
  664. }
  665. }
  666. }
  667. private void DelBookMark_Click(object sender, RoutedEventArgs e)
  668. {
  669. CheckBOTAState(out BOTAContentViewModel bOTAContentVM, out BOTAContent bOTA);
  670. if (bOTAContentVM != null && bOTA != null)
  671. {
  672. BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentVM, out BookmarkContent bookmark);
  673. int index = PDFViewer.CurrentIndex;
  674. //检测是否已存在相同数据
  675. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  676. if (bookmarkContentViewModel != null && bookmark != null && bOTA != null && list != null)
  677. {
  678. bookmarkContentViewModel.DeleteCommand.Execute(list);
  679. isAddBookMark = bookmarkContentViewModel.isAddBookMark;
  680. }
  681. }
  682. }
  683. private void CheckBOTAState(out BOTAContentViewModel bOTAContentVM, out BOTAContent bOTA)
  684. {
  685. if (viewContentViewModel.OpenBOTA == false)
  686. {
  687. viewContentViewModel.OpenBOTA = true;
  688. }
  689. bool isBook = IsBOTATabItemShow(out bOTAContentVM, out bOTA, "TabItemBookMark");
  690. if (isBook == false)
  691. {
  692. bOTA.TabItemBookMark.IsSelected = true;
  693. }
  694. }
  695. private void AddBookMark_Click()
  696. {
  697. CheckBOTAState(out BOTAContentViewModel bOTAContentVM, out BOTAContent bOTA);
  698. //if (viewContentViewModel.mainViewModel.IsBookMode)
  699. //{
  700. // this.events.GetEvent<ReadModeBOTAShowEvent>().Publish(new ReadModeBOTAShowArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsBOTAShow = true });
  701. //}
  702. if (bOTAContentVM != null && bOTA != null)
  703. {
  704. BookmarkContentViewModel bookmarkContentViewModel = GetBookmarkContentViewModel(bOTAContentVM, out BookmarkContent bookmark);
  705. if (bookmarkContentViewModel != null && bookmark != null && bOTA != null)
  706. {
  707. //bookmarkContentViewModel.AddBookmarkCommand.Execute(bookmark.BookMarkListView);
  708. bookmark.BtnAddBookmark_Click(null, null);
  709. isAddBookMark = bookmarkContentViewModel.isAddBookMark;
  710. return;
  711. }
  712. }
  713. }
  714. private BookmarkContentViewModel GetBookmarkContentViewModel(BOTAContentViewModel bOTAContentViewModel, out BookmarkContent bookmark)
  715. {
  716. BookmarkContentViewModel viewModel = null;
  717. bookmark = null;
  718. if (bOTAContentViewModel != null)
  719. {
  720. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  721. {
  722. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  723. foreach (var item in views)
  724. {
  725. if (item is BookmarkContent bookmark1)
  726. {
  727. bookmark = bookmark1;
  728. viewModel = bookmark1.DataContext as BookmarkContentViewModel;
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. return viewModel;
  735. }
  736. private OutLineControlViewModel GetOutLineControlViewModel(BOTAContentViewModel bOTAContentViewModel, out OutLineControl outLineControl)
  737. {
  738. OutLineControlViewModel viewModel = null;
  739. outLineControl = null;
  740. if (bOTAContentViewModel != null)
  741. {
  742. if (region.Regions.ContainsRegionWithName(bOTAContentViewModel.BOTAContentRegionName))
  743. {
  744. var views = region.Regions[bOTAContentViewModel.BOTAContentRegionName].Views;
  745. foreach (var item in views)
  746. {
  747. if (item is OutLineControl outLineControl1)
  748. {
  749. outLineControl = outLineControl1;
  750. viewModel = outLineControl1.DataContext as OutLineControlViewModel;
  751. break;
  752. }
  753. }
  754. }
  755. }
  756. return viewModel;
  757. }
  758. private void DisplayAnnot_Click(object sender, RoutedEventArgs e)
  759. {
  760. PDFViewer.SetDrawAnnot(true);
  761. BtnShowAnnotIsChecked = true;
  762. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  763. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  764. {
  765. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  766. viewModel.DisplayAnnot();
  767. }
  768. }
  769. private void HiddenAnnot_Click(object sender, RoutedEventArgs e)
  770. {
  771. PDFViewer.SetDrawAnnot(false);
  772. BtnShowAnnotIsChecked = false;
  773. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  774. if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  775. {
  776. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  777. viewModel.HiddenAnnot();
  778. }
  779. }
  780. private void SetAddAnnotation(ItemCollection items)
  781. {
  782. foreach (var item in items)
  783. {
  784. if (item is MenuItem menuItem)
  785. {
  786. string str = menuItem.Tag.ToString();
  787. //if (str == AddAnnotType.AnnotLink.ToString()
  788. //|| str == AddAnnotType.AnnotStamp.ToString()
  789. //|| str == AddAnnotType.AnnotAutograph.ToString()
  790. //)
  791. //{
  792. // InBookModeSetIsEnabled(menuItem);
  793. //}
  794. menuItem.CommandParameter = item;
  795. menuItem.Command = SetAddAnnotationCommand;
  796. }
  797. }
  798. }
  799. private void AddLink_Click(object sender, RoutedEventArgs e)
  800. {
  801. var args = PDFViewer.ToolManager.CurrentAnnotArgs;
  802. BtnLinkIsChecked = true;
  803. }
  804. #endregion 阅读页右键菜单
  805. #region 注释右键菜单事件
  806. //高亮、下划线、删除
  807. private void HightAnnotCopyText_Menu(object obj)
  808. {
  809. if (obj as CusMenuItem != null)
  810. {
  811. var menu = obj as CusMenuItem;
  812. var annot = menu.Parameter as AnnotHandlerEventArgs;
  813. if (annot != null)
  814. {
  815. System.Windows.Clipboard.SetText(annot.Content);
  816. }
  817. }
  818. }
  819. //更改为当前注释属性默认值
  820. private void AnnotDefaultValues_Menu(object obj)
  821. {
  822. if (obj as CusMenuItem != null)
  823. {
  824. var menu = obj as CusMenuItem;
  825. var annot = menu.Parameter as AnnotHandlerEventArgs;
  826. if (annot != null)
  827. {
  828. if (annot is TextHighlightAnnotArgs)
  829. {
  830. var color = (annot as TextHighlightAnnotArgs).Color;
  831. HighLightColor = new SolidColorBrush(color);
  832. Settings.Default.AppProperties.Annotate.HighLightColor = color;
  833. var hightlight = GetAnnotDefault(AnnotArgsType.AnnotHighlight);
  834. hightlight.ForgoundColor = color;
  835. hightlight.Opacity = (annot as TextHighlightAnnotArgs).Transparency;
  836. SettingHelper.SetAnnotDefaultProperty(hightlight);
  837. }
  838. else if (annot is TextUnderlineAnnotArgs)
  839. {
  840. var color = (annot as TextUnderlineAnnotArgs).Color;
  841. UnderLineColor = new SolidColorBrush(color);
  842. Settings.Default.AppProperties.Annotate.UnderLineColor = color;
  843. var Underline = GetAnnotDefault(AnnotArgsType.AnnotUnderline);
  844. Underline.ForgoundColor = color;
  845. Underline.Opacity = (annot as TextUnderlineAnnotArgs).Transparency;
  846. SettingHelper.SetAnnotDefaultProperty(Underline);
  847. }
  848. else if (annot is TextStrikeoutAnnotArgs)
  849. {
  850. var color = (annot as TextStrikeoutAnnotArgs).Color;
  851. StrikeoutColor = new SolidColorBrush(color);
  852. Settings.Default.AppProperties.Annotate.StrikethroughColor = color;
  853. var Strikeout = GetAnnotDefault(AnnotArgsType.AnnotStrikeout);
  854. Strikeout.ForgoundColor = color;
  855. Strikeout.Opacity = (annot as TextStrikeoutAnnotArgs).Transparency;
  856. SettingHelper.SetAnnotDefaultProperty(Strikeout);
  857. }
  858. else if (annot is FreehandAnnotArgs)
  859. {
  860. var freeHand = annot as FreehandAnnotArgs;
  861. var color = freeHand.InkColor;
  862. Settings.Default.AppProperties.Annotate.FreeHandColor = color;
  863. var FreehandDefault = GetAnnotDefault(AnnotArgsType.AnnotFreehand);
  864. FreehandDefault.ForgoundColor = color;
  865. FreehandDefault.Opacity = freeHand.Transparency;
  866. FreehandDefault.Thickness = freeHand.LineWidth;
  867. var isSolid = AnnotTransfer.IsSolidStyle(freeHand.LineDash);
  868. if (isSolid)
  869. {
  870. FreehandDefault.DashArray = null;
  871. }
  872. else
  873. {
  874. FreehandDefault.DashArray = new List<double>();
  875. foreach (var item in freeHand.LineDash.Dashes)
  876. {
  877. FreehandDefault.DashArray.Add(item);
  878. }
  879. }
  880. SettingHelper.SetAnnotDefaultProperty(FreehandDefault);
  881. }
  882. else if (annot is FreeTextAnnotArgs)
  883. {
  884. var freeText = annot as FreeTextAnnotArgs;
  885. Settings.Default.AppProperties.Annotate.TextAnnoteColor = freeText.FontColor;
  886. Settings.Default.AppProperties.Annotate.TextFontFamaily = freeText.FontFamily.ToString();
  887. Settings.Default.AppProperties.Annotate.TextAlign = freeText.Align;
  888. var FreeTextDefalut = GetAnnotDefault(AnnotArgsType.AnnotFreeText);
  889. FreeTextDefalut.ForgoundColor = freeText.FontColor;
  890. FreeTextDefalut.FontFamily = freeText.FontFamily.ToString();
  891. FreeTextDefalut.TextAlign = freeText.Align;
  892. FreeTextDefalut.FontSize = freeText.FontSize;
  893. FreeTextDefalut.FontWeight = freeText.FontWeight;
  894. FreeTextDefalut.FontStyle = freeText.FontStyle;
  895. FreeTextDefalut.BackgroundColor = freeText.BgColor;
  896. FreeTextDefalut.Opacity = (annot as FreeTextAnnotArgs).Transparency;
  897. SettingHelper.SetAnnotDefaultProperty(FreeTextDefalut);
  898. }
  899. else if (annot is StickyAnnotArgs)
  900. {
  901. var color = (annot as StickyAnnotArgs).Color;
  902. Settings.Default.AppProperties.Annotate.NoteAnnoteColor = color;
  903. var StickyDefalut = GetAnnotDefault(AnnotArgsType.AnnotSticky);
  904. StickyDefalut.ForgoundColor = color;
  905. SettingHelper.SetAnnotDefaultProperty(StickyDefalut);
  906. }
  907. else if (annot is SquareAnnotArgs)
  908. {
  909. var square = annot as SquareAnnotArgs;
  910. var bgColor = square.BgColor;
  911. Settings.Default.AppProperties.Annotate.RectangleFillColor = bgColor;
  912. var borderColor = square.LineColor;
  913. Settings.Default.AppProperties.Annotate.RectangleBorderColor = borderColor;
  914. var SquareDefalut = GetAnnotDefault(AnnotArgsType.AnnotSquare);
  915. SquareDefalut.BorderColor = borderColor;
  916. SquareDefalut.BackgroundColor = bgColor;
  917. SquareDefalut.Opacity = square.Transparency;
  918. SquareDefalut.Thickness = square.LineWidth;
  919. var isSolid = AnnotTransfer.IsSolidStyle(square.LineDash);
  920. if (isSolid)
  921. {
  922. SquareDefalut.DashArray = null;
  923. }
  924. else
  925. {
  926. SquareDefalut.DashArray = new List<double>();
  927. foreach (var item in square.LineDash.Dashes)
  928. {
  929. SquareDefalut.DashArray.Add(item);
  930. }
  931. }
  932. SettingHelper.SetAnnotDefaultProperty(SquareDefalut);
  933. }
  934. else if (annot is CircleAnnotArgs)
  935. {
  936. var circle = annot as CircleAnnotArgs;
  937. var bgColor = circle.BgColor;
  938. Settings.Default.AppProperties.Annotate.CircleFillColor = bgColor;
  939. var borderColor = circle.LineColor;
  940. Settings.Default.AppProperties.Annotate.CircleBorderColor = borderColor;
  941. var CircleDefalut = GetAnnotDefault(AnnotArgsType.AnnotCircle);
  942. CircleDefalut.BorderColor = borderColor;
  943. CircleDefalut.BackgroundColor = bgColor;
  944. CircleDefalut.Opacity = circle.Transparency;
  945. CircleDefalut.Thickness = circle.LineWidth;
  946. var isSolid = AnnotTransfer.IsSolidStyle(circle.LineDash);
  947. if (isSolid)
  948. {
  949. CircleDefalut.DashArray = null;
  950. }
  951. else
  952. {
  953. CircleDefalut.DashArray = new List<double>();
  954. foreach (var item in circle.LineDash.Dashes)
  955. {
  956. CircleDefalut.DashArray.Add(item);
  957. }
  958. }
  959. SettingHelper.SetAnnotDefaultProperty(CircleDefalut);
  960. }
  961. else if (annot is LineAnnotArgs)
  962. {
  963. var line = (annot as LineAnnotArgs);
  964. var color = (annot as LineAnnotArgs).LineColor;
  965. Settings.Default.AppProperties.Annotate.LineColor = color;
  966. var lineDefalut = GetAnnotDefault(AnnotArgsType.AnnotLine);
  967. lineDefalut.BorderColor = color;
  968. lineDefalut.Opacity = (annot as LineAnnotArgs).Transparency;
  969. lineDefalut.Thickness = (annot as LineAnnotArgs).LineWidth;
  970. var isSolid = AnnotTransfer.IsSolidStyle(line.LineDash);
  971. if (isSolid)
  972. {
  973. lineDefalut.DashArray = null;
  974. }
  975. else
  976. {
  977. lineDefalut.DashArray = new List<double>();
  978. foreach (var item in line.LineDash.Dashes)
  979. {
  980. lineDefalut.DashArray.Add(item);
  981. }
  982. }
  983. SettingHelper.SetAnnotDefaultProperty(lineDefalut);
  984. }
  985. Settings.Default.Save();
  986. }
  987. }
  988. }
  989. private DefaultAnnotProperty GetAnnotDefault(AnnotArgsType annotArgsType)
  990. {
  991. var Annot = SettingHelper.GetAnnotDefaultProperty(annotArgsType);
  992. if (Annot == null)
  993. {
  994. Annot = new DefaultAnnotProperty();
  995. Annot.AnnotToolType = annotArgsType;
  996. }
  997. return Annot;
  998. }
  999. private System.Windows.Controls.Primitives.Popup popup = null;
  1000. //更改颜色
  1001. private async void AnnotColorPalette_Menu(object obj)
  1002. {
  1003. if (obj as CusMenuItem != null)
  1004. {
  1005. var menu = obj as CusMenuItem;
  1006. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1007. if (annot != null)
  1008. {
  1009. var item = new ColorDropBoxPop();
  1010. item.DataContext = menu;
  1011. item.ColorSelected -= AnnotMenu_ColorSelected;
  1012. item.ColorSelected += AnnotMenu_ColorSelected;
  1013. if (popup == null)
  1014. popup = new System.Windows.Controls.Primitives.Popup();
  1015. ContentControl window = null;
  1016. if (PDFViewer.Parent as ContentControl != null)
  1017. window = PDFViewer.Parent as ContentControl;
  1018. else
  1019. window = App.Current.MainWindow;
  1020. popup.Child = item;
  1021. popup.PlacementRectangle = new Rect(Mouse.GetPosition(window), new Size(item.Width, item.Height));
  1022. popup.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;
  1023. popup.PlacementTarget = window;
  1024. popup.IsOpen = true;
  1025. Window parentWnd = Window.GetWindow(App.Current.MainWindow);
  1026. if (parentWnd != null)
  1027. {
  1028. parentWnd.MouseDown -= parentWnd_MouseDown;
  1029. parentWnd.MouseDown += parentWnd_MouseDown;
  1030. }
  1031. while (popup.IsOpen)
  1032. await Task.Delay(20);
  1033. parentWnd.MouseDown -= parentWnd_MouseDown;
  1034. popup = null;
  1035. }
  1036. }
  1037. }
  1038. private void parentWnd_MouseDown(object sender, MouseButtonEventArgs e)
  1039. {
  1040. var ui = Mouse.DirectlyOver as FrameworkElement;
  1041. if (ui != null)
  1042. {
  1043. if (popup != null)
  1044. {
  1045. popup.IsOpen = false;
  1046. }
  1047. }
  1048. }
  1049. private void AnnotMenu_ColorSelected(object sender, Color e)
  1050. {
  1051. if (sender != null)
  1052. {
  1053. var menu = (sender as FrameworkElement).DataContext as CusMenuItem;
  1054. if (menu == null) return;
  1055. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1056. if (annot != null)
  1057. {
  1058. if (annot is FreehandAnnotArgs || annot is StickyAnnotArgs || annot is LineAnnotArgs)
  1059. {
  1060. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1061. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  1062. AnnotEvent?.UpdateAnnot();
  1063. }
  1064. else if (annot is FreeTextAnnotArgs)
  1065. {
  1066. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1067. if (menu.tag.ToString() == "FontColor")
  1068. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontColor, e);
  1069. else
  1070. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, e);
  1071. AnnotEvent?.UpdateAnnot();
  1072. }
  1073. else if (annot is SquareAnnotArgs || annot is CircleAnnotArgs)
  1074. {
  1075. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1076. if (menu.tag.ToString() == "FillColor")
  1077. AnnotEvent?.UpdateAttrib(AnnotAttrib.FillColor, e);
  1078. else
  1079. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  1080. AnnotEvent?.UpdateAnnot();
  1081. }
  1082. else if (annot is TextHighlightAnnotArgs || annot is TextUnderlineAnnotArgs || annot is TextStrikeoutAnnotArgs)
  1083. {
  1084. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1085. AnnotEvent?.UpdateAttrib(AnnotAttrib.Color, e);
  1086. AnnotEvent?.UpdateAnnot();
  1087. }
  1088. }
  1089. }
  1090. }
  1091. //添加笔记
  1092. private void AnnotAddNoteText_Menu(object obj)
  1093. {
  1094. if (obj as CusMenuItem != null)
  1095. {
  1096. var menu = obj as CusMenuItem;
  1097. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1098. PopAnnotNoteText(annot);
  1099. }
  1100. }
  1101. private void PopAnnotNoteText(AnnotHandlerEventArgs annot)
  1102. {
  1103. if (annot != null)
  1104. {
  1105. AnnotationHandlerEventArgs args = new AnnotationHandlerEventArgs();
  1106. args.ClientRect = annot.ClientRect;
  1107. args.AnnotHandlerEventArgs = annot;
  1108. args.PageIndex = annot.PageIndex;
  1109. args.AnnotIndex = annot.AnnotIndex;
  1110. args.EventType = annot.EventType;
  1111. args.CreateTime = annot.CreateTime;
  1112. args.UpdateTime = annot.UpdateTime;
  1113. args.Content = annot.Content;
  1114. args.MarkupContent = annot.MarkupContent;
  1115. args.Author = annot.Author;
  1116. args.Locked = annot.Locked;
  1117. args.ReadOnly = annot.ReadOnly;
  1118. args.FormField = annot.FormField;
  1119. args.Document = PDFViewer.Document;
  1120. DialogParameters value = new DialogParameters();
  1121. value.Add(ParameterNames.Annotation, args);
  1122. dialogs.ShowDialog(DialogNames.AddAnnotationDialog, value, e =>
  1123. {
  1124. if (e.Result == ButtonResult.OK && e.Parameters != null)
  1125. {
  1126. PDFViewer.UndoManager.CanSave = true;
  1127. if (e.Parameters.ContainsKey(ParameterNames.Annotation) && e.Parameters.ContainsKey(ParameterNames.AnnotEvent))
  1128. {
  1129. AnnotationHandlerEventArgs annotation = e.Parameters.GetValue<AnnotationHandlerEventArgs>(ParameterNames.Annotation);
  1130. //RefreshOneListItem(annotation.AnnotHandlerEventArgs);
  1131. events.GetEvent<RefreshAnnotEvent>().Publish(new RefreshAnnotArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, Annotation = annotation });
  1132. }
  1133. }
  1134. });
  1135. }
  1136. }
  1137. //手绘
  1138. private void FreeHandLineStyle_Menu(object obj)
  1139. {
  1140. if (obj as CusMenuItem != null)
  1141. {
  1142. var menu = obj as CusMenuItem;
  1143. var annot = menu.Parameter as FreehandAnnotArgs;
  1144. if (annot != null)
  1145. {
  1146. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1147. if (menu.tag.ToString() == "Solid")
  1148. {
  1149. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, AnnotTransfer.GetLineDashStyle(true));
  1150. }
  1151. else
  1152. {
  1153. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, AnnotTransfer.GetLineDashStyle(false));
  1154. }
  1155. AnnotEvent?.UpdateAnnot();
  1156. }
  1157. }
  1158. }
  1159. //文本
  1160. private void FreeTextFontFamily_Menu(object obj)
  1161. {
  1162. if (obj as CusMenuItem != null)
  1163. {
  1164. var menu = obj as CusMenuItem;
  1165. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1166. if (annot != null)
  1167. {
  1168. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1169. AnnotEvent?.UpdateAttrib(AnnotAttrib.FontFamily, new FontFamily(menu.tag.ToString()));
  1170. AnnotEvent?.UpdateAnnot();
  1171. }
  1172. }
  1173. }
  1174. private void FreeTextAglin_Menu(object obj)
  1175. {
  1176. if (obj as CusMenuItem != null)
  1177. {
  1178. var menu = obj as CusMenuItem;
  1179. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1180. var tag = menu.control.Tag;
  1181. if (annot != null && tag != null)
  1182. {
  1183. var strTag = tag.ToString();
  1184. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1185. if (strTag == "Left")
  1186. {
  1187. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Left);
  1188. }
  1189. else if (strTag == "Center")
  1190. {
  1191. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Center);
  1192. }
  1193. else if (strTag == "Right")
  1194. {
  1195. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Right);
  1196. }
  1197. else if (strTag == "Justify")
  1198. {
  1199. AnnotEvent?.UpdateAttrib(AnnotAttrib.TextAlign, TextAlignment.Justify);
  1200. }
  1201. AnnotEvent?.UpdateAnnot();
  1202. }
  1203. }
  1204. }
  1205. //便签
  1206. private void StrikeNoteEditStrike_Menu(object obj)
  1207. {
  1208. if (obj as CusMenuItem != null)
  1209. {
  1210. var menu = obj as CusMenuItem;
  1211. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1212. if (annot != null)
  1213. {
  1214. var sticky = annot as StickyAnnotArgs;
  1215. sticky.PopupEditWnd();
  1216. #region 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
  1217. //isRightMenuAddAnnot = false;
  1218. #endregion 设计重新调整,阅读页空白处,右键菜单,添加链接需要显示,其他和pro mac一样的效果,不显示属性栏
  1219. }
  1220. }
  1221. }
  1222. //形状
  1223. private void ShapeLineStyle_Menu(object obj)
  1224. {
  1225. if (obj as CusMenuItem != null)
  1226. {
  1227. var menu = obj as CusMenuItem;
  1228. var annot = menu.Parameter as AnnotHandlerEventArgs;
  1229. var tag = menu.control.Tag;
  1230. if (annot != null && tag != null)
  1231. {
  1232. var strTag = tag.ToString();
  1233. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1234. if (strTag == "Solid")
  1235. {
  1236. var dashStyle = AnnotTransfer.GetLineDashStyle(true);
  1237. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dashStyle);
  1238. }
  1239. else
  1240. {
  1241. var dashStyle = AnnotTransfer.GetLineDashStyle(false);
  1242. AnnotEvent?.UpdateAttrib(AnnotAttrib.LineStyle, dashStyle);
  1243. }
  1244. AnnotEvent?.UpdateAnnot();
  1245. }
  1246. }
  1247. }
  1248. private void ShapeLineDirect_Menu(object obj)
  1249. {
  1250. if (obj as CusMenuItem != null)
  1251. {
  1252. var menu = obj as CusMenuItem;
  1253. var annot = menu.Parameter as LineAnnotArgs;
  1254. var tag = menu.control.Tag;
  1255. if (annot != null && tag != null)
  1256. {
  1257. var strTag = tag.ToString();
  1258. var AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annot, annot.GetAnnotAttrib());
  1259. if (strTag == "Ver")
  1260. {
  1261. annot.SetLineVertical();
  1262. }
  1263. else
  1264. {
  1265. annot.SetLineHorizontal();
  1266. }
  1267. }
  1268. }
  1269. }
  1270. private void Link_Menu(object obj)
  1271. {
  1272. if (obj is CusMenuItem menuItem)
  1273. {
  1274. if (menuItem.Parameter is LinkAnnotArgs linkAnnotArgs)
  1275. {
  1276. var result = PDFViewer.RemovePageAnnot(linkAnnotArgs.PageIndex, linkAnnotArgs.AnnotIndex);
  1277. if (result)
  1278. {
  1279. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  1280. bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  1281. if (isTabItemAnnotation)
  1282. {
  1283. AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel, out AnnotationContent annotation);
  1284. if (viewModel != null)
  1285. {
  1286. viewModel.UpdateModifiedAnnot(linkAnnotArgs.PageIndex, linkAnnotArgs.AnnotIndex, true);
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292. }
  1293. //图章
  1294. private void StampExportPicture_Menu(object obj)
  1295. {
  1296. if (obj as CusMenuItem != null)
  1297. {
  1298. var menu = obj as CusMenuItem;
  1299. var annot = menu.Parameter as StampAnnotArgs;
  1300. if (annot != null)
  1301. {
  1302. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  1303. var format = menu.tag.ToString();
  1304. if (format == "PDF")
  1305. dlg.Filter = "PDF|*.pdf";
  1306. else
  1307. dlg.Filter = format + "|*." + format.ToLower();
  1308. dlg.DefaultExt = "." + menu.tag.ToString().ToLower();
  1309. dlg.FileName = PDFViewer.Document.FileName;
  1310. if (dlg.ShowDialog() == true)
  1311. {
  1312. var stampPicture = dlg.FileName;
  1313. if (menu.tag.ToString() == "PDF")
  1314. {
  1315. var writeStamp = annot.GetStampDrawing();
  1316. var tempDoc = new CPDFViewer();
  1317. tempDoc.CreateDocument();
  1318. if (tempDoc.Document == null)
  1319. {
  1320. AlertsMessage alertsMessage = new AlertsMessage();
  1321. alertsMessage.ShowDialog("", "创建文件失败.", "OK");
  1322. return;
  1323. }
  1324. byte[] buffer;
  1325. var encoder = new PngBitmapEncoder();
  1326. encoder.Frames.Add(BitmapFrame.Create(writeStamp));
  1327. using (var memoryStream = new MemoryStream())
  1328. {
  1329. encoder.Save(memoryStream);
  1330. buffer = memoryStream.ToArray();
  1331. }
  1332. tempDoc.Document.InsertPage(0, writeStamp.PixelWidth, writeStamp.PixelHeight, buffer, CPDFDocumentImageMode.CPDFDocumentImageModeScaleAspectFit);
  1333. var result = tempDoc.Document.WriteToFilePath(dlg.FileName);
  1334. if (result)
  1335. {
  1336. }
  1337. }
  1338. else
  1339. {
  1340. var writeStamp = annot.GetStampDrawing();
  1341. using (FileStream stream5 = new FileStream(stampPicture, FileMode.Create))
  1342. {
  1343. PngBitmapEncoder encoder5 = new PngBitmapEncoder();
  1344. encoder5.Frames.Add(BitmapFrame.Create(writeStamp));
  1345. encoder5.Save(stream5);
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. #endregion 注释右键菜单事件
  1353. #region 快捷键
  1354. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  1355. {
  1356. if (KeyEventsHelper.IsSingleKey(Key.Escape))
  1357. {
  1358. //BtnHandIsChecked = true;
  1359. PDFViewer.SetMouseMode(MouseModes.PanTool);
  1360. PDFViewer.ClearSelectAnnots();
  1361. StrAnnotToolChecked = "";
  1362. ShowPropertyPanel(false);
  1363. }
  1364. else if (KeyEventsHelper.IsModifierKey(ModifierKeys.Shift, Key.None))
  1365. {
  1366. //按住Shift等比缩放
  1367. }
  1368. AnnotToolCtrlAltKey();
  1369. }
  1370. /// <summary>
  1371. /// 按下Ctrl + Alt + 序列键,响应注释工具
  1372. /// </summary>
  1373. private void AnnotToolCtrlAltKey()
  1374. {
  1375. var tuple = GetCtrlAltKey();
  1376. if (tuple != null)
  1377. {
  1378. if (tuple.Item1 == Key.L)
  1379. BtnLinkIsChecked = false;
  1380. AnnotHandlerEventArgs annotArgs = null;
  1381. StrAnnotToolChecked = tuple.Item2;
  1382. FindAnnotTypeKey(tuple.Item2, ref annotArgs);
  1383. if (annotArgs != null)
  1384. {
  1385. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  1386. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  1387. PDFViewer.SetToolParam(annotArgs);
  1388. PDFViewer.ToolManager.EnableClickCreate = false;
  1389. }
  1390. }
  1391. }
  1392. private Tuple<Key, string> GetCtrlAltKey()
  1393. {
  1394. Dictionary<Key, bool> AnnotToolDir = new Dictionary<Key, bool>();
  1395. AnnotToolDir.Add(Key.H, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.H));
  1396. AnnotToolDir.Add(Key.U, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.U));
  1397. AnnotToolDir.Add(Key.S, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.S));
  1398. AnnotToolDir.Add(Key.P, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.P));
  1399. AnnotToolDir.Add(Key.E, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.E));
  1400. AnnotToolDir.Add(Key.T, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.T));
  1401. AnnotToolDir.Add(Key.N, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.N));
  1402. AnnotToolDir.Add(Key.R, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.R));
  1403. AnnotToolDir.Add(Key.O, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.O));
  1404. AnnotToolDir.Add(Key.A, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.A));
  1405. AnnotToolDir.Add(Key.L, KeyEventsHelper.IsModifierKey(ModifierKeys.Control | ModifierKeys.Alt, Key.L));
  1406. Key key = Key.None;
  1407. foreach (var item in AnnotToolDir.Keys)
  1408. {
  1409. if (AnnotToolDir[item] == true)
  1410. {
  1411. key = item;
  1412. break;
  1413. }
  1414. }
  1415. if (key == Key.None)
  1416. return null;
  1417. var tag = "";
  1418. if (key == Key.H || key == Key.U || key == Key.S)
  1419. {
  1420. var str = PDFViewer.GetSelectedText();
  1421. if (string.IsNullOrEmpty(str) == false)
  1422. {
  1423. if (key == Key.H)
  1424. tag = "HighLight";
  1425. else if (key == Key.U)
  1426. tag = "UnderLine";
  1427. else if (key == Key.S)
  1428. tag = "Strikeout";
  1429. }
  1430. else
  1431. {
  1432. return null;
  1433. }
  1434. }
  1435. else
  1436. {
  1437. if (AnnotToolDir[Key.P])
  1438. tag = "Freehand";
  1439. else if (AnnotToolDir[Key.T])
  1440. tag = "Freetext";
  1441. else if (AnnotToolDir[Key.N])
  1442. tag = "StickyNote";
  1443. else if (AnnotToolDir[Key.R])
  1444. tag = "Rect";
  1445. else if (AnnotToolDir[Key.O])
  1446. tag = "Circle";
  1447. else if (AnnotToolDir[Key.A])
  1448. tag = "Arrow";
  1449. else if (AnnotToolDir[Key.L])
  1450. tag = "Line";
  1451. }
  1452. if (tag == "")
  1453. return null;
  1454. return new Tuple<Key, string>(key, tag);
  1455. }
  1456. #endregion 快捷键
  1457. #region Navigation
  1458. public bool IsNavigationTarget(NavigationContext navigationContext)
  1459. {
  1460. return true;
  1461. }
  1462. public void OnNavigatedFrom(NavigationContext navigationContext)
  1463. {
  1464. StrAnnotToolChecked = "";
  1465. UnBindingPDFViewerHandler();
  1466. UnBindingEvent();
  1467. }
  1468. public void OnNavigatedTo(NavigationContext navigationContext)
  1469. {
  1470. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  1471. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  1472. if (viewContentViewModel != null)
  1473. {
  1474. OpenFileInfo = viewContentViewModel.OpenFileInfo;
  1475. }
  1476. BindingPDFViewerHandler();
  1477. BindingEvent();
  1478. LoaedStickyNotePopup();
  1479. BtnHandIsChecked = (PDFViewer.MouseMode == MouseModes.Scroll ? true : false);
  1480. }
  1481. #endregion Navigation
  1482. }
  1483. }