FillAndSignContentViewModel.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. using ComPDFKitViewer.AnnotEvent;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.Model;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Regions;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using PDF_Office.ViewModels.Tools;
  15. using ComPDFKitViewer;
  16. using PDF_Office.Helper;
  17. using PDF_Office.Properties;
  18. using PDFSettings;
  19. using System.Windows.Media;
  20. using ComPDFKit.PDFDocument.Action;
  21. using ComPDFKit.PDFDocument;
  22. using System.Diagnostics;
  23. using ComPDFKit.PDFAnnotation;
  24. using PDF_Office.ViewModels.BOTA;
  25. using PDF_Office.Views.BOTA;
  26. using System.Windows.Forms;
  27. using Control = System.Windows.Controls.Control;
  28. using Microsoft.Office.Interop.Word;
  29. using Point = System.Windows.Point;
  30. using System.Windows.Ink;
  31. using PDF_Office.EventAggregators;
  32. using System.Management.Instrumentation;
  33. using Prism.Events;
  34. using System.Windows.Input;
  35. using ContextMenu = System.Windows.Controls.ContextMenu;
  36. using MenuItem = System.Windows.Controls.MenuItem;
  37. using System.Drawing;
  38. using Color = System.Windows.Media.Color;
  39. using Brush = System.Windows.Media.Brush;
  40. using FontFamily = System.Windows.Media.FontFamily;
  41. namespace PDF_Office.ViewModels.FillAndSign
  42. {
  43. public class FillAndSignContentViewModel : BindableBase, INavigationAware
  44. {
  45. #region
  46. private CPDFViewer PDFViewer;
  47. private AnnotPropertyPanel propertyPanel = new AnnotPropertyPanel();
  48. private ViewContentViewModel viewContentViewModel;
  49. private bool isRightMenuAddAnnot = false;
  50. private IEventAggregator events;
  51. public List<List<Point>> ShapePoints = new List<List<Point>>();
  52. private string Shape = "HookShape";
  53. private PopMenu FreeHandAnnotPopMenu;
  54. private PopMenu MultiAnnotPopMenu;
  55. private IRegionManager regions { get; set; }
  56. private Dictionary<string, AnnotArgsType> ToolExpandDict = new Dictionary<string, AnnotArgsType>();
  57. public static bool IsEdit = false;
  58. public Brush SelectColor = new SolidColorBrush(Colors.GreenYellow);
  59. public double FillOpacity = 1;
  60. public double LineWidth = 1;
  61. public double LineWidthMultiple = 1;
  62. #endregion
  63. #region Command
  64. public DelegateCommand<RoutedEventArgs> CheckCommand { get; set; }
  65. /// <summary>
  66. /// 按钮名称和属性面板映射字典
  67. /// </summary>
  68. public Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  69. #endregion
  70. public FillAndSignContentViewModel(IRegionManager regionManager, IEventAggregator events)
  71. {
  72. regions = regionManager;
  73. this.events = events;
  74. CheckCommand = new DelegateCommand<RoutedEventArgs>(CheckedEvent);
  75. ToolExpandDict.Add("Freetext", AnnotArgsType.AnnotFreeText);
  76. InitDictionary();
  77. InitSelectFreeHandAnnotMenu();
  78. }
  79. private void CheckedEvent(RoutedEventArgs e)
  80. {
  81. IsEdit = false;
  82. var control = e.OriginalSource as Control;
  83. //NavigateToProperty(control.Name);
  84. //不创建注释,属于注释模板
  85. bool isTemplateAnnot = false;
  86. bool isSnapshotEdit = false;
  87. AnnotHandlerEventArgs annotArgs = null;
  88. var tag = control.Name;
  89. FindAnnotTypeKey(control.Name, ref annotArgs);
  90. if (control.Name == "RbtnSign") { isTemplateAnnot = true; }
  91. if (tag == "SnapshotEdit")
  92. {
  93. isSnapshotEdit = true;
  94. }
  95. else if (tag == "Signature" || tag == "Stamp")
  96. {
  97. isTemplateAnnot = true;
  98. }
  99. if (annotArgs != null)
  100. {
  101. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  102. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  103. PDFViewer.SetToolParam(annotArgs);
  104. isRightMenuAddAnnot = false;
  105. }
  106. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  107. if (isTemplateAnnot == false && annotArgs == null)
  108. {
  109. PDFViewer.SetMouseMode(MouseModes.PanTool);
  110. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  111. }
  112. else
  113. {
  114. if (isSnapshotEdit == true)
  115. {
  116. ShowPropertyPanel(false);
  117. isSnapshotEdit = false;
  118. }
  119. else
  120. {
  121. ShowPropertyPanel(true);
  122. }
  123. }
  124. }
  125. //注释工具
  126. private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false)
  127. {
  128. LineWidthMultiple = 1;
  129. switch (tag)
  130. {
  131. case "RbtnDate":
  132. case "RbtnText"://文本
  133. annotArgs = GetFreetext();
  134. break;
  135. case "Freetext"://文本
  136. annotArgs = GetFreetext();
  137. break;
  138. case "RbtnSign"://签名
  139. annotArgs = GetSignature();
  140. PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
  141. break;
  142. case "RbtnTick"://勾
  143. Shape = "HookShape";
  144. ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };
  145. annotArgs = GetStamp();
  146. break;
  147. case "RbtnFork"://叉
  148. Shape = "ForkShape";
  149. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(12.8, 12.8) }, new List<Point> { new Point(12.8, 3.20001), new Point(3.20005, 12.8) } };
  150. annotArgs = GetStamp();
  151. break;
  152. case "RbtnRectangle"://矩形
  153. Shape = "RectShape";
  154. ShapePoints = new List<List<Point>> { new List<Point> { new Point(0.19995, 5), new Point(28, 5), new Point(28, 27), new Point(0.19995, 27), new Point(0.19995, 5) } };
  155. annotArgs = GetStamp();
  156. break;
  157. case "RbtnLine"://下划线
  158. Shape = "LineShape";
  159. ShapePoints = new List<List<Point>> { new List<Point> { new Point(12.19995, 10), new Point(36, 10) } };
  160. annotArgs = GetStamp();
  161. break;
  162. case "RbtnPoint":
  163. Shape = "DotShape";
  164. ShapePoints = new List<List<Point>> { new List<Point> { new Point(12.19995, 10), new Point(12.19995, 10) } };
  165. LineWidthMultiple = 4;
  166. annotArgs = GetStamp();
  167. break;
  168. default://图章
  169. break;
  170. }
  171. }
  172. /// <summary>
  173. /// 签名
  174. /// </summary>
  175. /// <returns></returns>
  176. private AnnotHandlerEventArgs GetSignature()
  177. {
  178. AddToPropertyPanel("SignatureAnnotProperty");
  179. return null;
  180. }
  181. /// <summary>
  182. /// 图章
  183. /// </summary>
  184. /// <returns></returns>
  185. private AnnotHandlerEventArgs GetStamp(List<AnnotHandlerEventArgs> selectedArgs = null)
  186. {
  187. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  188. stampAnnotArgs.SetInkData(ShapePoints, LineWidth* LineWidthMultiple, (SelectColor as SolidColorBrush).Color);
  189. //PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  190. //PDFViewer.SetToolParam(stampArgs);
  191. //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  192. //stampAnnotArgs.Opacity = 1;
  193. //stampAnnotArgs.StampText = "APPROVED";
  194. //stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  195. // stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  196. stampAnnotArgs.IsContinueMode = true;
  197. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  198. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  199. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp);
  200. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  201. if (stampAnnotArgs != null)
  202. stampAnnotArgsList.Add(stampAnnotArgs);
  203. //if (selectedArgs != null) {
  204. //var annot = selectedArgs[0];
  205. //if (annot != null)
  206. //{
  207. // foreach (var item in selectedArgs)
  208. // {
  209. // selectedArgs[selectedArgs.IndexOf(item)] = stampAnnotArgs;
  210. // }
  211. // AddToPropertyPanel("ShapFillProperty", "", selectedArgs, annotAttribsList);
  212. // return stampAnnotArgs;
  213. //}
  214. // }
  215. AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList);
  216. return stampAnnotArgs;
  217. }
  218. public void SetStamp()
  219. {
  220. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  221. stampAnnotArgs.SetInkData(ShapePoints, LineWidth* LineWidthMultiple, (SelectColor as SolidColorBrush).Color);
  222. //PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  223. //PDFViewer.SetToolParam(stampArgs);
  224. //StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  225. //stampAnnotArgs.Opacity = 1;
  226. //stampAnnotArgs.StampText = "APPROVED";
  227. //stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  228. stampAnnotArgs.IsContinueMode = true;
  229. stampAnnotArgs.Opacity = FillOpacity;
  230. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  231. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  232. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotStamp);
  233. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  234. if (stampAnnotArgs != null)
  235. stampAnnotArgsList.Add(stampAnnotArgs);
  236. AddToPropertyPanel("ShapFillProperty", "", stampAnnotArgsList, annotAttribsList);
  237. bool isTemplateAnnot = false;
  238. bool isSnapshotEdit = false;
  239. AnnotHandlerEventArgs annotArgs = stampAnnotArgs;
  240. if (annotArgs != null)
  241. {
  242. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  243. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  244. PDFViewer.SetToolParam(annotArgs);
  245. isRightMenuAddAnnot = false;
  246. }
  247. //当不是注释模板,且无创建注释时,属性面板显示为空内容
  248. if (isTemplateAnnot == false && annotArgs == null)
  249. {
  250. PDFViewer.SetMouseMode(MouseModes.PanTool);
  251. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  252. }
  253. else
  254. {
  255. if (isSnapshotEdit == true)
  256. {
  257. ShowPropertyPanel(false);
  258. isSnapshotEdit = false;
  259. }
  260. else
  261. {
  262. ShowPropertyPanel(true);
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// 手绘
  268. /// </summary>
  269. /// <param name="selectedArgs"></param>
  270. /// <returns></returns>
  271. private AnnotHandlerEventArgs GetFreehand(List<AnnotHandlerEventArgs> selectedArgs = null)
  272. {
  273. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  274. List<AnnotHandlerEventArgs> newSelectedArgs= new List<AnnotHandlerEventArgs>();
  275. FreehandAnnotArgs freehandArgs = null;
  276. if (selectedArgs == null || selectedArgs.Count == 0)
  277. {
  278. freehandArgs = new FreehandAnnotArgs();
  279. var annotate = Settings.Default.AppProperties.Annotate;
  280. if (annotate != null)
  281. {
  282. freehandArgs.InkColor = annotate.FreeHandColor;
  283. }
  284. else
  285. {
  286. freehandArgs.InkColor = Color.FromRgb(0x38, 0xE0, 0x2E);
  287. }
  288. freehandArgs.RawPointList= new List<List<Point>> { new List<Point> { new Point(0.599976, 7.0286), new Point(5.57775, 11.8), new Point(13.4, 1.40002) } };
  289. freehandArgs.Transparency = 1;
  290. freehandArgs.LineWidth = 2;
  291. if (freehandArgs != null)
  292. {
  293. selectedArgs = new List<AnnotHandlerEventArgs>();
  294. selectedArgs.Add(freehandArgs);
  295. }
  296. }
  297. else
  298. {
  299. freehandArgs = selectedArgs[0] as FreehandAnnotArgs;
  300. foreach (var item in selectedArgs)
  301. {
  302. //if (ListPoint((item as FreehandAnnotArgs).RawPointList, ShapePoints)){
  303. foreach (var point in viewContentViewModel.FillAndSign)
  304. {
  305. if ((item as FreehandAnnotArgs).PageIndex == point.X && (item as FreehandAnnotArgs).AnnotIndex == point.Y)
  306. {
  307. newSelectedArgs.Add(item);
  308. }
  309. }
  310. }
  311. freehandArgs.RawPointList = ShapePoints;
  312. freehandArgs.InkColor = (SelectColor as SolidColorBrush).Color;
  313. freehandArgs.Transparency = 1;
  314. freehandArgs.LineWidth = LineWidth;
  315. annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  316. annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  317. annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  318. annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  319. annotAttribsList[AnnotAttrib.Path]= freehandArgs.RawPointList;
  320. AddToPropertyPanel("ShapFillProperty", "Freehand", newSelectedArgs, annotAttribsList);
  321. //GetStamp();
  322. return freehandArgs;
  323. }
  324. //annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  325. //annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  326. //annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  327. //annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  328. //AddToPropertyPanel("ShapFillProperty", "Freehand", selectedArgs, annotAttribsList);
  329. return freehandArgs;
  330. }
  331. private bool ListPoint(List<List<Point>> leftpoints, List<List<Point>> rightpoints)
  332. {
  333. if (leftpoints.Count == rightpoints.Count)
  334. {
  335. if (leftpoints[0][0].X == rightpoints[0][0].X) { return true; }
  336. }
  337. return false;
  338. }
  339. /// <summary>
  340. /// 文本
  341. /// </summary>
  342. /// <param name="selectedfreetextArgs"></param>
  343. /// <returns></returns>
  344. private AnnotHandlerEventArgs GetFreetext(List<AnnotHandlerEventArgs> selectedArgs = null)
  345. {
  346. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  347. FreeTextAnnotArgs freetextArgs = null;
  348. TextAlignment textAlignment;
  349. if (selectedArgs == null || selectedArgs.Count == 0)
  350. {
  351. freetextArgs = new FreeTextAnnotArgs();
  352. freetextArgs.Align = TextAlignment.Left;
  353. freetextArgs.BgColor = Colors.Transparent;
  354. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  355. freetextArgs.FontColor = Colors.Black;
  356. freetextArgs.FontSize = 14;
  357. freetextArgs.Transparency = 1;
  358. freetextArgs.LineColor = Colors.Black;
  359. freetextArgs.LineWidth = 0;
  360. freetextArgs.TextContent = string.Empty;
  361. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  362. if (annotProperty != null)
  363. {
  364. freetextArgs.Align = annotProperty.TextAlign;
  365. freetextArgs.BgColor = annotProperty.BackgroundColor;
  366. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  367. freetextArgs.FontColor = annotProperty.ForgoundColor;
  368. freetextArgs.FontSize = annotProperty.FontSize;
  369. freetextArgs.Transparency = annotProperty.Opacity;
  370. freetextArgs.LineColor = annotProperty.BorderColor;
  371. freetextArgs.LineWidth = annotProperty.Thickness;
  372. freetextArgs.TextContent = annotProperty.NoteText;
  373. freetextArgs.FontWeight = annotProperty.FontWeight;
  374. freetextArgs.FontStyle = annotProperty.FontStyle;
  375. }
  376. int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
  377. if (align == 0)
  378. textAlignment = TextAlignment.Left;
  379. else if (align == 1)
  380. textAlignment = TextAlignment.Center;
  381. else
  382. textAlignment = TextAlignment.Right;
  383. if (freetextArgs != null)
  384. {
  385. selectedArgs = new List<AnnotHandlerEventArgs>();
  386. selectedArgs.Add(freetextArgs);
  387. }
  388. }
  389. else
  390. {
  391. freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
  392. textAlignment = freetextArgs.Align;
  393. }
  394. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  395. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  396. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  397. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  398. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  399. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  400. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  401. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  402. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  403. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  404. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  405. annotAttribsList[AnnotAttrib.Text] = freetextArgs.TextContent;
  406. AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList);
  407. return freetextArgs;
  408. }
  409. /// <summary>
  410. /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线
  411. /// </summary>
  412. /// <param name="viewContent">对应的注释面板</param>
  413. /// <param name="toolTag">导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线</param>
  414. /// <param name="annot">注释</param>
  415. /// <param name="annotAttribsList">更改注释属性的键值对,更改值后会自动记录undoRedo容器里</param>
  416. private void AddToPropertyPanel(string viewContent, string toolTag = null, List<AnnotHandlerEventArgs> annots = null, Dictionary<AnnotAttrib, object> annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null, bool isUpData = false)
  417. {
  418. if (annots != null&& annots.Count!=0)
  419. {
  420. propertyPanel.annotlists = annots;
  421. propertyPanel.annot = annots[0];
  422. }
  423. else
  424. {
  425. propertyPanel.annotlists = null;
  426. propertyPanel.annot = null;
  427. return;
  428. }
  429. if (annotAttribsList != null && annotAttribsList.Count != 0)
  430. {
  431. if (annots.Count > 1)
  432. {
  433. if (propertyPanel.AnnotEvents == null)
  434. propertyPanel.AnnotEvents = new List<AnnotAttribEvent>();
  435. propertyPanel.AnnotEvents.Clear();
  436. foreach (var itemAnnot in annots)
  437. {
  438. var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList);
  439. propertyPanel.AnnotEvents.Add(eventitem);
  440. }
  441. }
  442. propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList);
  443. if (annots[0] is LinkAnnotArgs && annotAttribEvent != null)
  444. {
  445. propertyPanel.AnnotEvent = annotAttribEvent;
  446. }
  447. }
  448. if (toolTag == "Freetext")
  449. {
  450. propertyPanel.SetIsTextFill(true);
  451. }
  452. else
  453. {
  454. propertyPanel.SetIsTextFill(false);
  455. }
  456. if (string.IsNullOrEmpty(viewContent) == false)
  457. {
  458. if (isUpData) { return; }
  459. if (viewContent == "ShapFillProperty") { NavigateToProperty(viewContent, propertyPanel); return; }
  460. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  461. }
  462. }
  463. private void NavigateToProperty(string btnName, AnnotPropertyPanel annotPropertyPanel)
  464. {
  465. NavigationParameters values = new NavigationParameters();
  466. values.Add(ParameterNames.PDFViewer, PDFViewer);
  467. values.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  468. values.Add("Shape", Shape);
  469. values.Add("FillAndSignContentViewModel", this);
  470. regions.RequestNavigate(RegionNames.PropertyRegionName, btnName, values);
  471. viewContentViewModel.IsPropertyOpen = true;
  472. }
  473. /// <summary>
  474. /// 初始化按钮名称-属性面板字典
  475. /// </summary>
  476. private void InitDictionary()
  477. {
  478. btnToProperty["RbtnText"] = "TextFillProperty";
  479. btnToProperty["RbtnTick"] = "ShapFillProperty";
  480. btnToProperty["RbtnFork"] = "ShapFillProperty";
  481. btnToProperty["RbtnRectangle"] = "ShapFillProperty";
  482. btnToProperty["RbtnLine"] = "ShapFillProperty";
  483. btnToProperty["RbtnPoint"] = "ShapFillProperty";
  484. btnToProperty["RbtnDate"] = "DateFillProperty";
  485. btnToProperty["RbtnSign"] = "";
  486. }
  487. /// <summary>
  488. /// 手绘
  489. /// </summary>
  490. private void InitSelectFreeHandAnnotMenu()
  491. {
  492. var popMenu = new ContextMenu();
  493. PopMenu pop = new PopMenu(popMenu);
  494. var menuItem = new MenuItem();
  495. menuItem.Name = "FreeHandCopy";
  496. menuItem.Header = "复制";
  497. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Copy);
  498. menuItem = new MenuItem();
  499. menuItem.Name = "FreeHandCut";
  500. menuItem.Header = "剪切";
  501. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Cut);
  502. menuItem = new MenuItem();
  503. menuItem.Name = "FreeHandPaste";
  504. menuItem.Header = "粘贴";
  505. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Paste);
  506. menuItem = new MenuItem();
  507. menuItem.Name = "FreeHandDelete";
  508. menuItem.Header = "删除";
  509. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Delete);
  510. pop.AddItem(GetSeparator());
  511. FreeHandAnnotPopMenu = pop;
  512. }
  513. private Separator GetSeparator()
  514. {
  515. Separator separator = new Separator();
  516. separator.Height = 1;
  517. separator.BorderBrush = new SolidColorBrush(Color.FromArgb(0x33, 0x00, 0x00, 0x00));
  518. separator.BorderThickness = new Thickness(1);
  519. return separator;
  520. }
  521. private void BindingPDFViewerHandler()
  522. {
  523. //来自PDFViewer的响应事件
  524. if (PDFViewer != null)
  525. {
  526. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  527. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  528. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  529. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  530. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  531. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  532. }
  533. }
  534. private void UnBindingPDFViewerHandler()
  535. {
  536. if (PDFViewer != null)
  537. {
  538. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  539. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  540. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  541. }
  542. }
  543. //在注释工具的状态下,右键菜单
  544. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  545. {
  546. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  547. return;
  548. switch (e.CommandType)
  549. {
  550. case CommandType.Context:
  551. if (e.AnnotEventArgsList.Count > 0)
  552. {
  553. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  554. {
  555. //绑定标记密文处右键菜单
  556. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  557. }
  558. else
  559. {
  560. if (e.AnnotEventArgsList.Count == 1)
  561. {
  562. var selectedAnnot = e.AnnotEventArgsList[0];
  563. switch (selectedAnnot.EventType)
  564. {
  565. case AnnotArgsType.AnnotFreehand:
  566. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  567. break;
  568. }
  569. }
  570. else
  571. {
  572. bool isHigh = true;//是否为高亮
  573. foreach (var item in e.AnnotEventArgsList)
  574. {
  575. if (isHightAnnot(item) == false)
  576. {
  577. isHigh = false;
  578. break;
  579. }
  580. }
  581. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  582. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  583. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  584. }
  585. }
  586. if (e.PopupMenu != null)
  587. {
  588. e.Handle = true;
  589. }
  590. }
  591. else
  592. {
  593. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  594. {
  595. if (e.PopupMenu != null)
  596. {
  597. e.Handle = true;
  598. }
  599. }
  600. else
  601. {
  602. e.PopupMenu = ViewerContextMenu();
  603. if (e.PopupMenu != null)
  604. {
  605. e.Handle = true;
  606. }
  607. }
  608. }
  609. break;
  610. }
  611. }
  612. private ContextMenu ViewerContextMenu()
  613. {
  614. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  615. ViewerContextMenu_Loaded(contextMenu, null);
  616. return contextMenu;
  617. }
  618. private void ViewerContextMenu_Loaded(object sender, RoutedEventArgs e,Visibility visibility =Visibility.Collapsed)
  619. {
  620. ContextMenu contextMenu = sender as ContextMenu;
  621. if (contextMenu.Items.Count > 0)
  622. {
  623. int index = PDFViewer.CurrentIndex;
  624. //检测是否已存在相同数据
  625. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  626. foreach (var item in contextMenu.Items)
  627. {
  628. if (item is Separator separator) {
  629. separator.Visibility = visibility;
  630. }
  631. if (item is MenuItem menuItem1)
  632. {
  633. //if (menuItem1.Tag.ToString() == "DisplayAnnot" || menuItem1.Tag.ToString() == "HiddenAnnot")
  634. //{
  635. // SetMenuItemVisibility(menuItem1, "DisplayAnnot", "HiddenAnnot", isHiddenAnnot);
  636. //}
  637. switch (menuItem1.Tag.ToString())
  638. {
  639. case "Copy":
  640. //粘贴
  641. //if (!ApplicationCommands.Paste.CanExecute(null, (UIElement)sender))
  642. //{
  643. // menuItem1.IsEnabled = false;
  644. // menuItem1.Opacity = 0.5;
  645. //}
  646. //else
  647. //{
  648. // menuItem1.IsEnabled = true;
  649. // menuItem1.Opacity = 1;
  650. //}
  651. menuItem1.CommandTarget = (UIElement)PDFViewer;
  652. menuItem1.Command = ApplicationCommands.Paste;
  653. break;
  654. case "AddAnnotation":
  655. menuItem1.Visibility = visibility;
  656. break;
  657. case "HiddenAnnot":
  658. menuItem1.Visibility = visibility;
  659. break;
  660. case "DisplayAnnot":
  661. menuItem1.Visibility = visibility;
  662. break;
  663. case "AddBookMark":
  664. menuItem1.Visibility = visibility;
  665. break;
  666. case "DelBookMark":
  667. menuItem1.Visibility = visibility;
  668. break;
  669. case "ToolMode":
  670. menuItem1.Visibility = visibility;
  671. break;
  672. case "ReadModel":
  673. menuItem1.Visibility = visibility;
  674. break;
  675. case "UnReadModel":
  676. menuItem1.Visibility = visibility;
  677. break;
  678. case "ViewZoom":
  679. menuItem1.Visibility = visibility;
  680. break;
  681. case "PageDisplay":
  682. menuItem1.Visibility = visibility;
  683. break;
  684. case "Select":
  685. menuItem1.Click -= Select_Click;
  686. menuItem1.Click += Select_Click;
  687. break;
  688. case "Print":
  689. menuItem1.Command = viewContentViewModel.PrintCommand;
  690. break;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. private void Select_Click(object sender, RoutedEventArgs e)
  697. {
  698. if (viewContentViewModel.OpenBOTA == false)
  699. {
  700. viewContentViewModel.OpenBOTA = true;
  701. }
  702. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  703. if (isBook == false)
  704. {
  705. bOTAContent.TabItemSearch.IsSelected = true;
  706. }
  707. }
  708. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  709. {
  710. bool isTabItem = false;
  711. bOTAContentViewModel = null;
  712. bOTAContent = null;
  713. if (regions.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  714. {
  715. var views = regions.Regions[viewContentViewModel.BOTARegionName].Views;
  716. var model = views.FirstOrDefault(q => q is BOTAContent);
  717. if (model is BOTAContent bOTAContent1)
  718. {
  719. bOTAContent = bOTAContent1;
  720. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  721. if (bOTAContentViewModel.CurrentBar == tabItemText)
  722. {
  723. isTabItem = true;
  724. }
  725. }
  726. //foreach (var item in views)
  727. //{
  728. // if (item is BOTAContent bOTAContent1)
  729. // {
  730. // bOTAContent = bOTAContent1;
  731. // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  732. // if (bOTAContentViewModel.CurrentBar == tabItemText)
  733. // {
  734. // isTabItem = true;
  735. // break;
  736. // }
  737. // }
  738. //}
  739. }
  740. return isTabItem;
  741. }
  742. private bool isHightAnnot(AnnotHandlerEventArgs annot)
  743. {
  744. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  745. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  746. annot.EventType == AnnotArgsType.AnnotHighlight ||
  747. annot.EventType == AnnotArgsType.AnnotStrikeout
  748. )
  749. {
  750. return true;
  751. }
  752. else
  753. {
  754. return false;
  755. }
  756. }
  757. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  758. {
  759. if (e != null && e.Count > 0)
  760. {
  761. for (int i = 0; i < e.Count; i++)
  762. {
  763. AnnotEditEvent editEvent = e[i];
  764. switch (editEvent.EditAction)
  765. {
  766. case ActionType.Add:
  767. int pageindex = editEvent.PageIndex;
  768. int annotindex = editEvent.AnnotIndex;
  769. viewContentViewModel.FillAndSign.Add(new Point(pageindex, annotindex));
  770. break;
  771. }
  772. }
  773. }
  774. }
  775. //选中和非选中注释,右键菜单
  776. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  777. {
  778. if (e != null)
  779. {
  780. var annot = e.AnnotItemsList[0];
  781. if (annot != null)
  782. {
  783. if (e.AnnotItemsList.Count == 1)
  784. {
  785. //IsEdit = false;
  786. //IsAnnotCreateReset:是否为创建注释的状态
  787. if (e.IsAnnotCreateReset == false)
  788. {
  789. GetSelectedAnnots(e);
  790. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  791. //PDFViewer.SetToolParam(annot);
  792. }
  793. else
  794. {
  795. //TODO: 设计已重新调整为(仅限高亮注释):修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  796. if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  797. annot.EventType != AnnotArgsType.AnnotUnderline &&
  798. annot.EventType != AnnotArgsType.AnnotHighlight &&
  799. annot.EventType != AnnotArgsType.AnnotSquiggly &&
  800. annot.EventType != AnnotArgsType.AnnotLink &&
  801. annot.EventType != AnnotArgsType.AnnotFreehand &&
  802. annot.EventType != AnnotArgsType.AnnotSticky
  803. )
  804. {
  805. if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  806. {
  807. foreach (var item in ToolExpandDict)
  808. {
  809. if (item.Value == e.AnnotItemsList[0].EventType)
  810. {
  811. annot = null;//新建注释时,回到默认值
  812. FindAnnotTypeKey(item.Key, ref annot);
  813. break;
  814. }
  815. }
  816. }
  817. }
  818. //else
  819. //PDFViewer.SetToolParam(annot);
  820. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  821. if (isRightMenuAddAnnot)
  822. {
  823. ShowPropertyPanel(false);
  824. }
  825. else
  826. {
  827. ShowPropertyPanel();
  828. }
  829. }
  830. }
  831. else
  832. {
  833. bool isDifferentAnnotTyle = false;
  834. var lastAnnot = annot;
  835. foreach (var item in e.AnnotItemsList)
  836. {
  837. if (lastAnnot.EventType != item.EventType)
  838. {
  839. if (isShapAnnot(annot) == true && isShapAnnot(item) == true)
  840. {
  841. lastAnnot = item;
  842. continue;
  843. }
  844. lastAnnot = item;
  845. isDifferentAnnotTyle = true;
  846. break;
  847. }
  848. }
  849. if (isDifferentAnnotTyle)
  850. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  851. else
  852. GetSelectedAnnots(e);
  853. }
  854. }
  855. }
  856. else
  857. {
  858. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  859. }
  860. }
  861. //private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  862. //{
  863. // if (e != null && e.Count > 0)
  864. // {
  865. // for (int i = 0; i < e.Count; i++)
  866. // {
  867. // AnnotEditEvent editEvent = e[i];
  868. // switch (editEvent.EditAction)
  869. // {
  870. // case ActionType.Add:
  871. // // bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  872. // if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  873. // {
  874. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  875. // if (viewModel != null)
  876. // {
  877. // int pageindex = editEvent.PageIndex;
  878. // int annotindex = editEvent.AnnotIndex;
  879. // viewModel.UpdateAddedAnnot(pageindex, annotindex);
  880. // }
  881. // }
  882. // break;
  883. // case ActionType.Del:
  884. // //isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  885. // if (isTabItemAnnotation)
  886. // {
  887. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  888. // if (viewModel != null)
  889. // {
  890. // int pageindex = editEvent.PageIndex;
  891. // int annotindex = editEvent.AnnotIndex;
  892. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  893. // }
  894. // }
  895. // break;
  896. // case ActionType.Modify:
  897. // isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  898. // if (bOTAContent.TabItemAnnotation.IsSelected)
  899. // {
  900. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  901. // if (viewModel != null)
  902. // {
  903. // int pageindex = editEvent.PageIndex;
  904. // int annotindex = editEvent.AnnotIndex;
  905. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  906. // }
  907. // }
  908. // break;
  909. // case ActionType.TextEdit:
  910. // break;
  911. // default:
  912. // break;
  913. // }
  914. // }
  915. // }
  916. //}
  917. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  918. {
  919. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  920. annot.EventType == AnnotArgsType.AnnotSquare ||
  921. annot.EventType == AnnotArgsType.AnnotLine
  922. )
  923. {
  924. return true;
  925. }
  926. else
  927. {
  928. return false;
  929. }
  930. }
  931. private void GetSelectedAnnots(AnnotAttribEvent e)
  932. {
  933. IsEdit = true;
  934. var annot = e.AnnotItemsList[0];
  935. switch (annot.EventType)
  936. {
  937. case AnnotArgsType.AnnotFreeText:
  938. GetFreetext(e.AnnotItemsList);
  939. break;
  940. case AnnotArgsType.AnnotFreehand:
  941. GetFreehand(e.AnnotItemsList);
  942. break;
  943. }
  944. }
  945. private void ShowPropertyPanel(bool show = true)
  946. {
  947. viewContentViewModel.IsPropertyOpen = show;
  948. }
  949. #region Navigation
  950. public bool IsNavigationTarget(NavigationContext navigationContext)
  951. {
  952. return true;
  953. }
  954. public void OnNavigatedFrom(NavigationContext navigationContext)
  955. {
  956. UnBindingPDFViewerHandler();
  957. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  958. ViewerContextMenu_Loaded(contextMenu, null,Visibility.Visible);
  959. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  960. }
  961. public void OnNavigatedTo(NavigationContext navigationContext)
  962. {
  963. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  964. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  965. BindingPDFViewerHandler();
  966. IsEdit = false;
  967. }
  968. #endregion
  969. }
  970. }