FillAndSignContentViewModel.cs 46 KB

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