FillAndSignContentViewModel.cs 48 KB

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