FillAndSignContentViewModel.cs 48 KB

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