FillAndSignContentViewModel.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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(5, 5), new Point(28, 5) }, new List<Point> { new Point(28, 5), new Point(28, 27) }, new List<Point> { new Point(28, 27), new Point(5, 27) }, new List<Point> { new Point(5, 27) ,new Point(5, 5)} };
  155. annotArgs = GetStamp();
  156. break;
  157. case "RbtnLine"://下划线
  158. Shape = "LineShape";
  159. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.20001), new Point(28, 3.20001) } };
  160. annotArgs = GetStamp();
  161. break;
  162. case "RbtnPoint":
  163. Shape = "DotShape";
  164. ShapePoints = new List<List<Point>> { new List<Point> { new Point(3.19995, 3.19995), new Point(3.19995, 3.19995) } };
  165. LineWidthMultiple = 5;
  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. annotAttribsList[AnnotAttrib.Width] = freehandArgs.Width;
  321. annotAttribsList[AnnotAttrib.Height] = freehandArgs.Height;
  322. AddToPropertyPanel("ShapFillProperty", "Freehand", newSelectedArgs, annotAttribsList);
  323. //GetStamp();
  324. return freehandArgs;
  325. }
  326. //annotAttribsList[AnnotAttrib.Color] = freehandArgs.InkColor;
  327. //annotAttribsList[AnnotAttrib.Transparency] = freehandArgs.Transparency;
  328. //annotAttribsList[AnnotAttrib.Thickness] = freehandArgs.LineWidth;
  329. //annotAttribsList[AnnotAttrib.NoteText] = freehandArgs.Content;
  330. //AddToPropertyPanel("ShapFillProperty", "Freehand", selectedArgs, annotAttribsList);
  331. return freehandArgs;
  332. }
  333. private bool ListPoint(List<List<Point>> leftpoints, List<List<Point>> rightpoints)
  334. {
  335. if (leftpoints.Count == rightpoints.Count)
  336. {
  337. if (leftpoints[0][0].X == rightpoints[0][0].X) { return true; }
  338. }
  339. return false;
  340. }
  341. /// <summary>
  342. /// 文本
  343. /// </summary>
  344. /// <param name="selectedfreetextArgs"></param>
  345. /// <returns></returns>
  346. private AnnotHandlerEventArgs GetFreetext(List<AnnotHandlerEventArgs> selectedArgs = null)
  347. {
  348. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  349. FreeTextAnnotArgs freetextArgs = null;
  350. TextAlignment textAlignment;
  351. if (selectedArgs == null || selectedArgs.Count == 0)
  352. {
  353. freetextArgs = new FreeTextAnnotArgs();
  354. freetextArgs.Align = TextAlignment.Left;
  355. freetextArgs.BgColor = Colors.Transparent;
  356. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  357. freetextArgs.FontColor = Colors.Black;
  358. freetextArgs.FontSize = 14;
  359. freetextArgs.Transparency = 1;
  360. freetextArgs.LineColor = Colors.Black;
  361. freetextArgs.LineWidth = 0;
  362. freetextArgs.TextContent = string.Empty;
  363. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  364. if (annotProperty != null)
  365. {
  366. freetextArgs.Align = annotProperty.TextAlign;
  367. freetextArgs.BgColor = annotProperty.BackgroundColor;
  368. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  369. freetextArgs.FontColor = annotProperty.ForgoundColor;
  370. freetextArgs.FontSize = annotProperty.FontSize;
  371. freetextArgs.Transparency = annotProperty.Opacity;
  372. freetextArgs.LineColor = annotProperty.BorderColor;
  373. freetextArgs.LineWidth = annotProperty.Thickness;
  374. freetextArgs.TextContent = annotProperty.NoteText;
  375. freetextArgs.FontWeight = annotProperty.FontWeight;
  376. freetextArgs.FontStyle = annotProperty.FontStyle;
  377. }
  378. int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
  379. if (align == 0)
  380. textAlignment = TextAlignment.Left;
  381. else if (align == 1)
  382. textAlignment = TextAlignment.Center;
  383. else
  384. textAlignment = TextAlignment.Right;
  385. if (freetextArgs != null)
  386. {
  387. selectedArgs = new List<AnnotHandlerEventArgs>();
  388. selectedArgs.Add(freetextArgs);
  389. }
  390. }
  391. else
  392. {
  393. freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
  394. textAlignment = freetextArgs.Align;
  395. }
  396. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  397. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  398. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  399. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  400. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  401. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  402. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  403. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  404. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  405. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  406. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  407. annotAttribsList[AnnotAttrib.Text] = freetextArgs.TextContent;
  408. AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList);
  409. return freetextArgs;
  410. }
  411. /// <summary>
  412. /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线
  413. /// </summary>
  414. /// <param name="viewContent">对应的注释面板</param>
  415. /// <param name="toolTag">导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线</param>
  416. /// <param name="annot">注释</param>
  417. /// <param name="annotAttribsList">更改注释属性的键值对,更改值后会自动记录undoRedo容器里</param>
  418. private void AddToPropertyPanel(string viewContent, string toolTag = null, List<AnnotHandlerEventArgs> annots = null, Dictionary<AnnotAttrib, object> annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null, bool isUpData = false)
  419. {
  420. if (annots != null&& annots.Count!=0)
  421. {
  422. propertyPanel.annotlists = annots;
  423. propertyPanel.annot = annots[0];
  424. }
  425. else
  426. {
  427. propertyPanel.annotlists = null;
  428. propertyPanel.annot = null;
  429. }
  430. if (annotAttribsList != null && annotAttribsList.Count != 0)
  431. {
  432. if (annots.Count > 1)
  433. {
  434. if (propertyPanel.AnnotEvents == null)
  435. propertyPanel.AnnotEvents = new List<AnnotAttribEvent>();
  436. propertyPanel.AnnotEvents.Clear();
  437. foreach (var itemAnnot in annots)
  438. {
  439. var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList);
  440. propertyPanel.AnnotEvents.Add(eventitem);
  441. }
  442. }
  443. propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList);
  444. if (annots[0] is LinkAnnotArgs && annotAttribEvent != null)
  445. {
  446. propertyPanel.AnnotEvent = annotAttribEvent;
  447. }
  448. }
  449. if (toolTag == "Freetext")
  450. {
  451. propertyPanel.SetIsTextFill(true);
  452. }
  453. else
  454. {
  455. propertyPanel.SetIsTextFill(false);
  456. }
  457. if (string.IsNullOrEmpty(viewContent) == false)
  458. {
  459. if (isUpData) { return; }
  460. if (viewContent == "ShapFillProperty") { NavigateToProperty(viewContent, propertyPanel); return; }
  461. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  462. }
  463. }
  464. private void NavigateToProperty(string btnName, AnnotPropertyPanel annotPropertyPanel)
  465. {
  466. NavigationParameters values = new NavigationParameters();
  467. values.Add(ParameterNames.PDFViewer, PDFViewer);
  468. values.Add(ParameterNames.PropertyPanelContentViewModel, annotPropertyPanel);
  469. values.Add("Shape", Shape);
  470. values.Add("FillAndSignContentViewModel", this);
  471. regions.RequestNavigate(RegionNames.PropertyRegionName, btnName, values);
  472. viewContentViewModel.IsPropertyOpen = true;
  473. }
  474. /// <summary>
  475. /// 初始化按钮名称-属性面板字典
  476. /// </summary>
  477. private void InitDictionary()
  478. {
  479. btnToProperty["RbtnText"] = "TextFillProperty";
  480. btnToProperty["RbtnTick"] = "ShapFillProperty";
  481. btnToProperty["RbtnFork"] = "ShapFillProperty";
  482. btnToProperty["RbtnRectangle"] = "ShapFillProperty";
  483. btnToProperty["RbtnLine"] = "ShapFillProperty";
  484. btnToProperty["RbtnPoint"] = "ShapFillProperty";
  485. btnToProperty["RbtnDate"] = "DateFillProperty";
  486. btnToProperty["RbtnSign"] = "";
  487. }
  488. /// <summary>
  489. /// 手绘
  490. /// </summary>
  491. private void InitSelectFreeHandAnnotMenu()
  492. {
  493. var popMenu = new ContextMenu();
  494. PopMenu pop = new PopMenu(popMenu);
  495. var menuItem = new MenuItem();
  496. menuItem.Name = "FreeHandCopy";
  497. menuItem.Header = "复制";
  498. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Copy);
  499. menuItem = new MenuItem();
  500. menuItem.Name = "FreeHandCut";
  501. menuItem.Header = "剪切";
  502. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Cut);
  503. menuItem = new MenuItem();
  504. menuItem.Name = "FreeHandPaste";
  505. menuItem.Header = "粘贴";
  506. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Paste);
  507. menuItem = new MenuItem();
  508. menuItem.Name = "FreeHandDelete";
  509. menuItem.Header = "删除";
  510. pop.BindingEvent(pop.AddItem(menuItem), ApplicationCommands.Delete);
  511. pop.AddItem(GetSeparator());
  512. FreeHandAnnotPopMenu = pop;
  513. }
  514. private Separator GetSeparator()
  515. {
  516. Separator separator = new Separator();
  517. separator.Height = 1;
  518. separator.BorderBrush = new SolidColorBrush(Color.FromArgb(0x33, 0x00, 0x00, 0x00));
  519. separator.BorderThickness = new Thickness(1);
  520. return separator;
  521. }
  522. private void BindingPDFViewerHandler()
  523. {
  524. //来自PDFViewer的响应事件
  525. if (PDFViewer != null)
  526. {
  527. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  528. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  529. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  530. PDFViewer.AnnotCommandHandler += PDFViewer_AnnotCommandHandler;
  531. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  532. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  533. }
  534. }
  535. private void UnBindingPDFViewerHandler()
  536. {
  537. if (PDFViewer != null)
  538. {
  539. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  540. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  541. PDFViewer.AnnotCommandHandler -= PDFViewer_AnnotCommandHandler;
  542. }
  543. }
  544. //在注释工具的状态下,右键菜单
  545. private void PDFViewer_AnnotCommandHandler(object sender, AnnotCommandArgs e)
  546. {
  547. if (e.AnnotEventArgsList == null || (PDFViewer != null && PDFViewer.MouseMode == MouseModes.FormEditTool))
  548. return;
  549. switch (e.CommandType)
  550. {
  551. case CommandType.Context:
  552. if (e.AnnotEventArgsList.Count > 0)
  553. {
  554. if (App.mainWindowViewModel.SelectedItem.IsInReadctonMode && e.AnnotEventArgsList[0].EventType == AnnotArgsType.AnnotRedaction)
  555. {
  556. //绑定标记密文处右键菜单
  557. events.GetEvent<RedactionCommandEvent>().Publish(new RedactionCommandEventArgs() { UniCode = App.mainWindowViewModel.SelectedItem.Unicode, Sender = sender, args = e });
  558. }
  559. else
  560. {
  561. if (e.AnnotEventArgsList.Count == 1)
  562. {
  563. var selectedAnnot = e.AnnotEventArgsList[0];
  564. switch (selectedAnnot.EventType)
  565. {
  566. case AnnotArgsType.AnnotFreehand:
  567. e.PopupMenu = FreeHandAnnotPopMenu.OpenMenu(selectedAnnot, sender);
  568. break;
  569. }
  570. }
  571. else
  572. {
  573. bool isHigh = true;//是否为高亮
  574. foreach (var item in e.AnnotEventArgsList)
  575. {
  576. if (isHightAnnot(item) == false)
  577. {
  578. isHigh = false;
  579. break;
  580. }
  581. }
  582. MultiAnnotPopMenu.SetVisual("MultiCopy", !isHigh);
  583. MultiAnnotPopMenu.SetVisual("MultiCut", !isHigh);
  584. e.PopupMenu = MultiAnnotPopMenu.OpenMenu(e.AnnotEventArgsList, sender);//SelectMultiAnnotMenu(e.AnnotEventArgsList, isHigh);
  585. }
  586. }
  587. if (e.PopupMenu != null)
  588. {
  589. e.Handle = true;
  590. }
  591. }
  592. else
  593. {
  594. if (e.PressOnSelectedText || e.CommandTarget == TargetType.ImageSelection)
  595. {
  596. if (e.PopupMenu != null)
  597. {
  598. e.Handle = true;
  599. }
  600. }
  601. else
  602. {
  603. e.PopupMenu = ViewerContextMenu();
  604. if (e.PopupMenu != null)
  605. {
  606. e.Handle = true;
  607. }
  608. }
  609. }
  610. break;
  611. }
  612. }
  613. private ContextMenu ViewerContextMenu()
  614. {
  615. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  616. ViewerContextMenu_Loaded(contextMenu, null);
  617. return contextMenu;
  618. }
  619. private void ViewerContextMenu_Loaded(object sender, RoutedEventArgs e,Visibility visibility =Visibility.Collapsed)
  620. {
  621. ContextMenu contextMenu = sender as ContextMenu;
  622. if (contextMenu.Items.Count > 0)
  623. {
  624. int index = PDFViewer.CurrentIndex;
  625. //检测是否已存在相同数据
  626. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == index);
  627. foreach (var item in contextMenu.Items)
  628. {
  629. if (item is Separator separator) {
  630. separator.Visibility = visibility;
  631. }
  632. if (item is MenuItem menuItem1)
  633. {
  634. //if (menuItem1.Tag.ToString() == "DisplayAnnot" || menuItem1.Tag.ToString() == "HiddenAnnot")
  635. //{
  636. // SetMenuItemVisibility(menuItem1, "DisplayAnnot", "HiddenAnnot", isHiddenAnnot);
  637. //}
  638. switch (menuItem1.Tag.ToString())
  639. {
  640. case "Copy":
  641. //粘贴
  642. //if (!ApplicationCommands.Paste.CanExecute(null, (UIElement)sender))
  643. //{
  644. // menuItem1.IsEnabled = false;
  645. // menuItem1.Opacity = 0.5;
  646. //}
  647. //else
  648. //{
  649. // menuItem1.IsEnabled = true;
  650. // menuItem1.Opacity = 1;
  651. //}
  652. menuItem1.CommandTarget = (UIElement)PDFViewer;
  653. menuItem1.Command = ApplicationCommands.Paste;
  654. break;
  655. case "AddAnnotation":
  656. menuItem1.Visibility = visibility;
  657. break;
  658. case "HiddenAnnot":
  659. menuItem1.Visibility = visibility;
  660. break;
  661. case "DisplayAnnot":
  662. menuItem1.Visibility = visibility;
  663. break;
  664. case "AddBookMark":
  665. menuItem1.Visibility = visibility;
  666. break;
  667. case "DelBookMark":
  668. menuItem1.Visibility = visibility;
  669. break;
  670. case "ToolMode":
  671. menuItem1.Visibility = visibility;
  672. break;
  673. case "ReadModel":
  674. menuItem1.Visibility = visibility;
  675. break;
  676. case "UnReadModel":
  677. menuItem1.Visibility = visibility;
  678. break;
  679. case "ViewZoom":
  680. menuItem1.Visibility = visibility;
  681. break;
  682. case "PageDisplay":
  683. menuItem1.Visibility = visibility;
  684. break;
  685. case "Select":
  686. menuItem1.Click -= Select_Click;
  687. menuItem1.Click += Select_Click;
  688. break;
  689. case "Print":
  690. menuItem1.Command = viewContentViewModel.PrintCommand;
  691. break;
  692. }
  693. }
  694. }
  695. }
  696. }
  697. private void Select_Click(object sender, RoutedEventArgs e)
  698. {
  699. if (viewContentViewModel.OpenBOTA == false)
  700. {
  701. viewContentViewModel.OpenBOTA = true;
  702. }
  703. bool isBook = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemSearch");
  704. if (isBook == false)
  705. {
  706. bOTAContent.TabItemSearch.IsSelected = true;
  707. }
  708. }
  709. private bool IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, string tabItemText)
  710. {
  711. bool isTabItem = false;
  712. bOTAContentViewModel = null;
  713. bOTAContent = null;
  714. if (regions.Regions.ContainsRegionWithName(viewContentViewModel.BOTARegionName))
  715. {
  716. var views = regions.Regions[viewContentViewModel.BOTARegionName].Views;
  717. var model = views.FirstOrDefault(q => q is BOTAContent);
  718. if (model is BOTAContent bOTAContent1)
  719. {
  720. bOTAContent = bOTAContent1;
  721. bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  722. if (bOTAContentViewModel.CurrentBar == tabItemText)
  723. {
  724. isTabItem = true;
  725. }
  726. }
  727. //foreach (var item in views)
  728. //{
  729. // if (item is BOTAContent bOTAContent1)
  730. // {
  731. // bOTAContent = bOTAContent1;
  732. // bOTAContentViewModel = bOTAContent.DataContext as BOTAContentViewModel;
  733. // if (bOTAContentViewModel.CurrentBar == tabItemText)
  734. // {
  735. // isTabItem = true;
  736. // break;
  737. // }
  738. // }
  739. //}
  740. }
  741. return isTabItem;
  742. }
  743. private bool isHightAnnot(AnnotHandlerEventArgs annot)
  744. {
  745. if (annot.EventType == AnnotArgsType.AnnotUnderline ||
  746. annot.EventType == AnnotArgsType.AnnotSquiggly ||
  747. annot.EventType == AnnotArgsType.AnnotHighlight ||
  748. annot.EventType == AnnotArgsType.AnnotStrikeout
  749. )
  750. {
  751. return true;
  752. }
  753. else
  754. {
  755. return false;
  756. }
  757. }
  758. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  759. {
  760. if (e != null && e.Count > 0)
  761. {
  762. for (int i = 0; i < e.Count; i++)
  763. {
  764. AnnotEditEvent editEvent = e[i];
  765. switch (editEvent.EditAction)
  766. {
  767. case ActionType.Add:
  768. int pageindex = editEvent.PageIndex;
  769. int annotindex = editEvent.AnnotIndex;
  770. viewContentViewModel.FillAndSign.Add(new Point(pageindex, annotindex));
  771. break;
  772. }
  773. }
  774. }
  775. }
  776. //选中和非选中注释,右键菜单
  777. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  778. {
  779. if (e != null)
  780. {
  781. var annot = e.AnnotItemsList[0];
  782. if (annot != null)
  783. {
  784. if (e.AnnotItemsList.Count == 1)
  785. {
  786. //IsEdit = false;
  787. //IsAnnotCreateReset:是否为创建注释的状态
  788. if (e.IsAnnotCreateReset == false)
  789. {
  790. GetSelectedAnnots(e);
  791. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  792. //PDFViewer.SetToolParam(annot);
  793. }
  794. else
  795. {
  796. //TODO: 设计已重新调整为(仅限高亮注释):修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  797. if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  798. annot.EventType != AnnotArgsType.AnnotUnderline &&
  799. annot.EventType != AnnotArgsType.AnnotHighlight &&
  800. annot.EventType != AnnotArgsType.AnnotSquiggly &&
  801. annot.EventType != AnnotArgsType.AnnotLink &&
  802. annot.EventType != AnnotArgsType.AnnotFreehand &&
  803. annot.EventType != AnnotArgsType.AnnotSticky
  804. )
  805. {
  806. if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  807. {
  808. foreach (var item in ToolExpandDict)
  809. {
  810. if (item.Value == e.AnnotItemsList[0].EventType)
  811. {
  812. annot = null;//新建注释时,回到默认值
  813. FindAnnotTypeKey(item.Key, ref annot);
  814. break;
  815. }
  816. }
  817. }
  818. }
  819. //else
  820. //PDFViewer.SetToolParam(annot);
  821. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  822. if (isRightMenuAddAnnot)
  823. {
  824. ShowPropertyPanel(false);
  825. }
  826. else
  827. {
  828. ShowPropertyPanel();
  829. }
  830. }
  831. }
  832. else
  833. {
  834. bool isDifferentAnnotTyle = false;
  835. var lastAnnot = annot;
  836. foreach (var item in e.AnnotItemsList)
  837. {
  838. if (lastAnnot.EventType != item.EventType)
  839. {
  840. if (isShapAnnot(annot) == true && isShapAnnot(item) == true)
  841. {
  842. lastAnnot = item;
  843. continue;
  844. }
  845. lastAnnot = item;
  846. isDifferentAnnotTyle = true;
  847. break;
  848. }
  849. }
  850. if (isDifferentAnnotTyle)
  851. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  852. else
  853. GetSelectedAnnots(e);
  854. }
  855. }
  856. }
  857. else
  858. {
  859. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  860. }
  861. }
  862. //private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  863. //{
  864. // if (e != null && e.Count > 0)
  865. // {
  866. // for (int i = 0; i < e.Count; i++)
  867. // {
  868. // AnnotEditEvent editEvent = e[i];
  869. // switch (editEvent.EditAction)
  870. // {
  871. // case ActionType.Add:
  872. // // bool isTabItemAnnotation = IsBOTATabItemShow(out BOTAContentViewModel bOTAContentViewModel, out BOTAContent bOTAContent, "TabItemAnnotation");
  873. // if (viewContentViewModel.OpenBOTA == true && isTabItemAnnotation == true)
  874. // {
  875. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  876. // if (viewModel != null)
  877. // {
  878. // int pageindex = editEvent.PageIndex;
  879. // int annotindex = editEvent.AnnotIndex;
  880. // viewModel.UpdateAddedAnnot(pageindex, annotindex);
  881. // }
  882. // }
  883. // break;
  884. // case ActionType.Del:
  885. // //isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  886. // if (isTabItemAnnotation)
  887. // {
  888. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  889. // if (viewModel != null)
  890. // {
  891. // int pageindex = editEvent.PageIndex;
  892. // int annotindex = editEvent.AnnotIndex;
  893. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, true);
  894. // }
  895. // }
  896. // break;
  897. // case ActionType.Modify:
  898. // isTabItemAnnotation = IsBOTATabItemShow(out bOTAContentViewModel, out bOTAContent, "TabItemAnnotation");
  899. // if (bOTAContent.TabItemAnnotation.IsSelected)
  900. // {
  901. // AnnotationContentViewModel viewModel = GetAnnotationContentViewModel(bOTAContentViewModel);
  902. // if (viewModel != null)
  903. // {
  904. // int pageindex = editEvent.PageIndex;
  905. // int annotindex = editEvent.AnnotIndex;
  906. // viewModel.UpdateModifiedAnnot(pageindex, annotindex, false);
  907. // }
  908. // }
  909. // break;
  910. // case ActionType.TextEdit:
  911. // break;
  912. // default:
  913. // break;
  914. // }
  915. // }
  916. // }
  917. //}
  918. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  919. {
  920. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  921. annot.EventType == AnnotArgsType.AnnotSquare ||
  922. annot.EventType == AnnotArgsType.AnnotLine
  923. )
  924. {
  925. return true;
  926. }
  927. else
  928. {
  929. return false;
  930. }
  931. }
  932. private void GetSelectedAnnots(AnnotAttribEvent e)
  933. {
  934. IsEdit = true;
  935. var annot = e.AnnotItemsList[0];
  936. switch (annot.EventType)
  937. {
  938. case AnnotArgsType.AnnotFreeText:
  939. GetFreetext(e.AnnotItemsList);
  940. break;
  941. case AnnotArgsType.AnnotFreehand:
  942. GetFreehand(e.AnnotItemsList);
  943. break;
  944. }
  945. }
  946. private void ShowPropertyPanel(bool show = true)
  947. {
  948. viewContentViewModel.IsPropertyOpen = show;
  949. }
  950. #region Navigation
  951. public bool IsNavigationTarget(NavigationContext navigationContext)
  952. {
  953. return true;
  954. }
  955. public void OnNavigatedFrom(NavigationContext navigationContext)
  956. {
  957. UnBindingPDFViewerHandler();
  958. ContextMenu contextMenu = App.Current.FindResource("ViewerContextMenu") as ContextMenu;
  959. ViewerContextMenu_Loaded(contextMenu, null,Visibility.Visible);
  960. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  961. }
  962. public void OnNavigatedTo(NavigationContext navigationContext)
  963. {
  964. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  965. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  966. BindingPDFViewerHandler();
  967. IsEdit = false;
  968. }
  969. #endregion
  970. }
  971. }