AnnotToolContentViewModel.cs 69 KB

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