FillAndSignContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. namespace PDF_Office.ViewModels.FillAndSign
  25. {
  26. public class FillAndSignContentViewModel : BindableBase, INavigationAware
  27. {
  28. #region
  29. private CPDFViewer PDFViewer;
  30. private AnnotPropertyPanel propertyPanel = new AnnotPropertyPanel();
  31. private ViewContentViewModel viewContentViewModel;
  32. private bool isRightMenuAddAnnot = false;
  33. private IRegionManager regions { get; set; }
  34. private Dictionary<string, AnnotArgsType> ToolExpandDict = new Dictionary<string, AnnotArgsType>();
  35. #endregion
  36. #region Command
  37. public DelegateCommand<RoutedEventArgs> CheckCommand { get; set; }
  38. /// <summary>
  39. /// 按钮名称和属性面板映射字典
  40. /// </summary>
  41. public Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  42. #endregion
  43. public FillAndSignContentViewModel(IRegionManager regionManager)
  44. {
  45. regions = regionManager;
  46. CheckCommand = new DelegateCommand<RoutedEventArgs>(CheckedEvent);
  47. ToolExpandDict.Add("Freetext", AnnotArgsType.AnnotFreeText);
  48. InitDictionary();
  49. }
  50. private void CheckedEvent(RoutedEventArgs e)
  51. {
  52. var control = e.OriginalSource as Control;
  53. NavigateToProperty(control.Name);
  54. //不创建注释,属于注释模板
  55. //bool isTemplateAnnot = false;
  56. //bool isSnapshotEdit = false;
  57. //AnnotHandlerEventArgs annotArgs = null;
  58. // var tag = control.Name;
  59. // if (control.Name == "RbtnSign") { FindAnnotTypeKey("Signature", ref annotArgs); isTemplateAnnot = true; }
  60. // if (control.Name == "RbtnText") { FindAnnotTypeKey("Freetext", ref annotArgs); }
  61. //if (control.Name == "RbtnDate") { FindAnnotTypeKey("Freetext", ref annotArgs); }
  62. // if (tag == "SnapshotEdit")
  63. // {
  64. // isSnapshotEdit = true;
  65. // }
  66. // else if (tag == "Signature" || tag == "Stamp")
  67. // {
  68. // isTemplateAnnot = true;
  69. // }
  70. // if (annotArgs != null)
  71. // {
  72. // annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  73. // PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  74. // PDFViewer.SetToolParam(annotArgs);
  75. // isRightMenuAddAnnot = false;
  76. // }
  77. ////当不是注释模板,且无创建注释时,属性面板显示为空内容
  78. //if (isTemplateAnnot == false && annotArgs == null)
  79. //{
  80. // PDFViewer.SetMouseMode(MouseModes.PanTool);
  81. // viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  82. //}
  83. //else
  84. //{
  85. // if (isSnapshotEdit == true)
  86. // {
  87. // ShowPropertyPanel(false);
  88. // isSnapshotEdit = false;
  89. // }
  90. // else
  91. // {
  92. // ShowPropertyPanel(true);
  93. // }
  94. //}
  95. }
  96. //注释工具
  97. private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false)
  98. {
  99. switch (tag)
  100. {
  101. case "Freetext"://文本
  102. annotArgs = GetFreetext();
  103. break;
  104. case "Stamp"://图章
  105. annotArgs = GetStamp();
  106. break;
  107. case "Signature"://签名
  108. annotArgs = GetSignature();
  109. PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
  110. break;
  111. }
  112. }
  113. /// <summary>
  114. /// 签名
  115. /// </summary>
  116. /// <returns></returns>
  117. private AnnotHandlerEventArgs GetSignature()
  118. {
  119. AddToPropertyPanel("SignatureAnnotProperty");
  120. return null;
  121. }
  122. /// <summary>
  123. /// 图章
  124. /// </summary>
  125. /// <returns></returns>
  126. private AnnotHandlerEventArgs GetStamp()
  127. {
  128. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  129. stampAnnotArgs.Opacity = 1;
  130. stampAnnotArgs.StampText = "APPROVED";
  131. stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  132. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  133. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  134. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  135. if (stampAnnotArgs != null)
  136. stampAnnotArgsList.Add(stampAnnotArgs);
  137. AddToPropertyPanel("StampAnnotProperty", null, stampAnnotArgsList, annotAttribsList);
  138. return stampAnnotArgs;
  139. }
  140. /// <summary>
  141. /// 文本
  142. /// </summary>
  143. /// <param name="selectedfreetextArgs"></param>
  144. /// <returns></returns>
  145. private AnnotHandlerEventArgs GetFreetext(List<AnnotHandlerEventArgs> selectedArgs = null)
  146. {
  147. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  148. FreeTextAnnotArgs freetextArgs = null;
  149. TextAlignment textAlignment;
  150. if (selectedArgs == null || selectedArgs.Count == 0)
  151. {
  152. freetextArgs = new FreeTextAnnotArgs();
  153. freetextArgs.Align = TextAlignment.Left;
  154. freetextArgs.BgColor = Colors.Transparent;
  155. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  156. freetextArgs.FontColor = Colors.Black;
  157. freetextArgs.FontSize = 14;
  158. freetextArgs.Transparency = 1;
  159. freetextArgs.LineColor = Colors.Black;
  160. freetextArgs.LineWidth = 0;
  161. freetextArgs.TextContent = string.Empty;
  162. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  163. if (annotProperty != null)
  164. {
  165. freetextArgs.Align = annotProperty.TextAlign;
  166. freetextArgs.BgColor = annotProperty.BackgroundColor;
  167. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  168. freetextArgs.FontColor = annotProperty.ForgoundColor;
  169. freetextArgs.FontSize = annotProperty.FontSize;
  170. freetextArgs.Transparency = annotProperty.Opacity;
  171. freetextArgs.LineColor = annotProperty.BorderColor;
  172. freetextArgs.LineWidth = annotProperty.Thickness;
  173. freetextArgs.TextContent = annotProperty.NoteText;
  174. freetextArgs.FontWeight = annotProperty.FontWeight;
  175. freetextArgs.FontStyle = annotProperty.FontStyle;
  176. }
  177. int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
  178. if (align == 0)
  179. textAlignment = TextAlignment.Left;
  180. else if (align == 1)
  181. textAlignment = TextAlignment.Center;
  182. else
  183. textAlignment = TextAlignment.Right;
  184. if (freetextArgs != null)
  185. {
  186. selectedArgs = new List<AnnotHandlerEventArgs>();
  187. selectedArgs.Add(freetextArgs);
  188. }
  189. }
  190. else
  191. {
  192. freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
  193. textAlignment = freetextArgs.Align;
  194. }
  195. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  196. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  197. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  198. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  199. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  200. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  201. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  202. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  203. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  204. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  205. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  206. AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList);
  207. return freetextArgs;
  208. }
  209. /// <summary>
  210. /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线
  211. /// </summary>
  212. /// <param name="viewContent">对应的注释面板</param>
  213. /// <param name="toolTag">导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线</param>
  214. /// <param name="annot">注释</param>
  215. /// <param name="annotAttribsList">更改注释属性的键值对,更改值后会自动记录undoRedo容器里</param>
  216. private void AddToPropertyPanel(string viewContent, string toolTag = null, List<AnnotHandlerEventArgs> annots = null, Dictionary<AnnotAttrib, object> annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null)
  217. {
  218. if (annots != null)
  219. {
  220. propertyPanel.annotlists = annots;
  221. propertyPanel.annot = annots[0];
  222. }
  223. else
  224. {
  225. propertyPanel.annotlists = null;
  226. propertyPanel.annot = null;
  227. }
  228. if (annotAttribsList != null)
  229. {
  230. if (annots.Count > 1)
  231. {
  232. if (propertyPanel.AnnotEvents == null)
  233. propertyPanel.AnnotEvents = new List<AnnotAttribEvent>();
  234. propertyPanel.AnnotEvents.Clear();
  235. foreach (var itemAnnot in annots)
  236. {
  237. var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList);
  238. propertyPanel.AnnotEvents.Add(eventitem);
  239. }
  240. }
  241. propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList);
  242. if (annots[0] is LinkAnnotArgs && annotAttribEvent != null)
  243. {
  244. propertyPanel.AnnotEvent = annotAttribEvent;
  245. }
  246. }
  247. if (string.IsNullOrEmpty(viewContent) == false)
  248. {
  249. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  250. }
  251. }
  252. private void NavigateToProperty(string btnName)
  253. {
  254. NavigationParameters values = new NavigationParameters();
  255. values.Add(ParameterNames.PDFViewer, PDFViewer);
  256. regions.RequestNavigate(RegionNames.PropertyRegionName,btnToProperty[btnName]);
  257. viewContentViewModel.IsPropertyOpen = true;
  258. }
  259. /// <summary>
  260. /// 初始化按钮名称-属性面板字典
  261. /// </summary>
  262. private void InitDictionary()
  263. {
  264. btnToProperty["RbtnText"] = "TextFillProperty";
  265. btnToProperty["RbtnTick"] = "ShapFillProperty";
  266. btnToProperty["RbtnFork"] = "ShapFillProperty";
  267. btnToProperty["RbtnRectangle"] = "ShapFillProperty";
  268. btnToProperty["RbtnLine"] = "ShapFillProperty";
  269. btnToProperty["RbtnPoint"] = "ShapFillProperty";
  270. btnToProperty["RbtnDate"] = "DateFillProperty";
  271. btnToProperty["RbtnSign"] = "";
  272. }
  273. private void BindingPDFViewerHandler()
  274. {
  275. //来自PDFViewer的响应事件
  276. if (PDFViewer != null)
  277. {
  278. PDFViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  279. PDFViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  280. }
  281. }
  282. //选中和非选中注释,右键菜单
  283. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  284. {
  285. if (e != null)
  286. {
  287. var annot = e.AnnotItemsList[0];
  288. if (annot != null)
  289. {
  290. if (e.AnnotItemsList.Count == 1)
  291. {
  292. //IsAnnotCreateReset:是否为创建注释的状态
  293. if (e.IsAnnotCreateReset == false)
  294. {
  295. GetSelectedAnnots(e);
  296. //记录这次选中的注释,之后创建注释会跟随上次选中注释的属性值
  297. PDFViewer.SetToolParam(annot);
  298. }
  299. else
  300. {
  301. //TODO: 设计已重新调整为(仅限高亮注释):修改注释后,会作用到之后添加的注释中。因此先把此逻辑“创建注释后,会自动回到默认值”注释掉
  302. if (annot.EventType != AnnotArgsType.AnnotStrikeout &&
  303. annot.EventType != AnnotArgsType.AnnotUnderline &&
  304. annot.EventType != AnnotArgsType.AnnotHighlight &&
  305. annot.EventType != AnnotArgsType.AnnotSquiggly &&
  306. annot.EventType != AnnotArgsType.AnnotLink &&
  307. annot.EventType != AnnotArgsType.AnnotFreehand &&
  308. annot.EventType != AnnotArgsType.AnnotSticky
  309. )
  310. {
  311. if (ToolExpandDict.ContainsValue(e.AnnotItemsList[0].EventType))
  312. {
  313. foreach (var item in ToolExpandDict)
  314. {
  315. if (item.Value == e.AnnotItemsList[0].EventType)
  316. {
  317. annot = null;//新建注释时,回到默认值
  318. FindAnnotTypeKey(item.Key, ref annot);
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. //else
  325. PDFViewer.SetToolParam(annot);
  326. //设计重新调整,阅读页空白处,右键菜单,添加链接,和pro mac一样的效果,不显示属性栏
  327. if (isRightMenuAddAnnot)
  328. {
  329. ShowPropertyPanel(false);
  330. }
  331. else
  332. {
  333. ShowPropertyPanel();
  334. }
  335. }
  336. }
  337. else
  338. {
  339. bool isDifferentAnnotTyle = false;
  340. var lastAnnot = annot;
  341. foreach (var item in e.AnnotItemsList)
  342. {
  343. if (lastAnnot.EventType != item.EventType)
  344. {
  345. if (isShapAnnot(annot) == true && isShapAnnot(item) == true)
  346. {
  347. lastAnnot = item;
  348. continue;
  349. }
  350. lastAnnot = item;
  351. isDifferentAnnotTyle = true;
  352. break;
  353. }
  354. }
  355. if (isDifferentAnnotTyle)
  356. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  357. else
  358. GetSelectedAnnots(e);
  359. }
  360. }
  361. }
  362. else
  363. {
  364. viewContentViewModel.SelectedPrpoertyPanel("PropertyPanelContent", null);
  365. }
  366. }
  367. private bool isShapAnnot(AnnotHandlerEventArgs annot)
  368. {
  369. if (annot.EventType == AnnotArgsType.AnnotCircle ||
  370. annot.EventType == AnnotArgsType.AnnotSquare ||
  371. annot.EventType == AnnotArgsType.AnnotLine
  372. )
  373. {
  374. return true;
  375. }
  376. else
  377. {
  378. return false;
  379. }
  380. }
  381. private void GetSelectedAnnots(AnnotAttribEvent e)
  382. {
  383. var annot = e.AnnotItemsList[0];
  384. switch (annot.EventType)
  385. {
  386. case AnnotArgsType.AnnotFreeText:
  387. GetFreetext(e.AnnotItemsList);
  388. break;
  389. }
  390. }
  391. private void ShowPropertyPanel(bool show = true)
  392. {
  393. viewContentViewModel.IsPropertyOpen = show;
  394. }
  395. #region Navigation
  396. public bool IsNavigationTarget(NavigationContext navigationContext)
  397. {
  398. return true;
  399. }
  400. public void OnNavigatedFrom(NavigationContext navigationContext)
  401. {
  402. }
  403. public void OnNavigatedTo(NavigationContext navigationContext)
  404. {
  405. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  406. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  407. BindingPDFViewerHandler();
  408. }
  409. #endregion
  410. }
  411. }