FillAndSignContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. namespace PDF_Office.ViewModels.FillAndSign
  21. {
  22. public class FillAndSignContentViewModel : BindableBase, INavigationAware
  23. {
  24. #region
  25. private CPDFViewer PDFViewer;
  26. private AnnotPropertyPanel propertyPanel = new AnnotPropertyPanel();
  27. private ViewContentViewModel viewContentViewModel;
  28. private IRegionManager regions { get; set; }
  29. #endregion
  30. #region Command
  31. public DelegateCommand<RoutedEventArgs> CheckCommand { get; set; }
  32. /// <summary>
  33. /// 按钮名称和属性面板映射字典
  34. /// </summary>
  35. public Dictionary<string, string> btnToProperty = new Dictionary<string, string>();
  36. #endregion
  37. public FillAndSignContentViewModel(IRegionManager regionManager)
  38. {
  39. regions = regionManager;
  40. CheckCommand = new DelegateCommand<RoutedEventArgs>(CheckedEvent);
  41. InitDictionary();
  42. }
  43. private void CheckedEvent(RoutedEventArgs e)
  44. {
  45. var control = e.OriginalSource as Control;
  46. NavigateToProperty(control.Name);
  47. if(control.Name== "RbtnSign") { AnnotHandlerEventArgs annotArgs = null; FindAnnotTypeKey("Signature", ref annotArgs); }
  48. }
  49. //注释工具
  50. private void FindAnnotTypeKey(string tag, ref AnnotHandlerEventArgs annotArgs, bool isRightMenuAdd = false)
  51. {
  52. switch (tag)
  53. {
  54. case "Freetext"://文本
  55. annotArgs = GetFreetext();
  56. break;
  57. case "Stamp"://图章
  58. annotArgs = GetStamp();
  59. break;
  60. case "Signature"://签名
  61. annotArgs = GetSignature();
  62. PDFViewer.SetMouseMode(MouseModes.PanTool);//清空其他注释
  63. break;
  64. }
  65. }
  66. /// <summary>
  67. /// 签名
  68. /// </summary>
  69. /// <returns></returns>
  70. private AnnotHandlerEventArgs GetSignature()
  71. {
  72. AddToPropertyPanel("SignatureAnnotProperty");
  73. return null;
  74. }
  75. /// <summary>
  76. /// 图章
  77. /// </summary>
  78. /// <returns></returns>
  79. private AnnotHandlerEventArgs GetStamp()
  80. {
  81. StampAnnotArgs stampAnnotArgs = new StampAnnotArgs();
  82. stampAnnotArgs.Opacity = 1;
  83. stampAnnotArgs.StampText = "APPROVED";
  84. stampAnnotArgs.Type = StampType.STANDARD_STAMP;
  85. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  86. annotAttribsList[AnnotAttrib.Transparency] = stampAnnotArgs.Opacity;
  87. List<AnnotHandlerEventArgs> stampAnnotArgsList = new List<AnnotHandlerEventArgs>();
  88. if (stampAnnotArgs != null)
  89. stampAnnotArgsList.Add(stampAnnotArgs);
  90. AddToPropertyPanel("StampAnnotProperty", null, stampAnnotArgsList, annotAttribsList);
  91. return stampAnnotArgs;
  92. }
  93. /// <summary>
  94. /// 文本
  95. /// </summary>
  96. /// <param name="selectedfreetextArgs"></param>
  97. /// <returns></returns>
  98. private AnnotHandlerEventArgs GetFreetext(List<AnnotHandlerEventArgs> selectedArgs = null)
  99. {
  100. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  101. FreeTextAnnotArgs freetextArgs = null;
  102. TextAlignment textAlignment;
  103. if (selectedArgs == null || selectedArgs.Count == 0)
  104. {
  105. freetextArgs = new FreeTextAnnotArgs();
  106. freetextArgs.Align = TextAlignment.Left;
  107. freetextArgs.BgColor = Colors.Transparent;
  108. freetextArgs.FontFamily = new FontFamily(Settings.Default.AppProperties.Annotate.TextFontFamaily);
  109. freetextArgs.FontColor = Colors.Black;
  110. freetextArgs.FontSize = 14;
  111. freetextArgs.Transparency = 1;
  112. freetextArgs.LineColor = Colors.Black;
  113. freetextArgs.LineWidth = 0;
  114. freetextArgs.TextContent = string.Empty;
  115. DefaultAnnotProperty annotProperty = SettingHelper.GetAnnotDefaultProperty(AnnotArgsType.AnnotFreeText);
  116. if (annotProperty != null)
  117. {
  118. freetextArgs.Align = annotProperty.TextAlign;
  119. freetextArgs.BgColor = annotProperty.BackgroundColor;
  120. freetextArgs.FontFamily = new FontFamily(annotProperty.FontFamily);
  121. freetextArgs.FontColor = annotProperty.ForgoundColor;
  122. freetextArgs.FontSize = annotProperty.FontSize;
  123. freetextArgs.Transparency = annotProperty.Opacity;
  124. freetextArgs.LineColor = annotProperty.BorderColor;
  125. freetextArgs.LineWidth = annotProperty.Thickness;
  126. freetextArgs.TextContent = annotProperty.NoteText;
  127. freetextArgs.FontWeight = annotProperty.FontWeight;
  128. freetextArgs.FontStyle = annotProperty.FontStyle;
  129. }
  130. int align = (int)Settings.Default.AppProperties.Annotate.TextAlign;
  131. if (align == 0)
  132. textAlignment = TextAlignment.Left;
  133. else if (align == 1)
  134. textAlignment = TextAlignment.Center;
  135. else
  136. textAlignment = TextAlignment.Right;
  137. if (freetextArgs != null)
  138. {
  139. selectedArgs = new List<AnnotHandlerEventArgs>();
  140. selectedArgs.Add(freetextArgs);
  141. }
  142. }
  143. else
  144. {
  145. freetextArgs = selectedArgs[0] as FreeTextAnnotArgs;
  146. textAlignment = freetextArgs.Align;
  147. }
  148. annotAttribsList[AnnotAttrib.Color] = freetextArgs.LineColor;
  149. annotAttribsList[AnnotAttrib.FillColor] = freetextArgs.BgColor;
  150. annotAttribsList[AnnotAttrib.Thickness] = freetextArgs.LineWidth;
  151. annotAttribsList[AnnotAttrib.Transparency] = freetextArgs.Transparency;
  152. annotAttribsList[AnnotAttrib.FontColor] = freetextArgs.FontColor;
  153. annotAttribsList[AnnotAttrib.FontSize] = freetextArgs.FontSize;
  154. annotAttribsList[AnnotAttrib.FontFamily] = freetextArgs.FontFamily;
  155. annotAttribsList[AnnotAttrib.FontStyle] = freetextArgs.FontStyle;
  156. annotAttribsList[AnnotAttrib.FontWeight] = freetextArgs.FontWeight;
  157. annotAttribsList[AnnotAttrib.TextAlign] = textAlignment;
  158. annotAttribsList[AnnotAttrib.NoteText] = freetextArgs.TextContent;
  159. AddToPropertyPanel("FreetextAnnotProperty", "Freetext", selectedArgs, annotAttribsList);
  160. return freetextArgs;
  161. }
  162. /// <summary>
  163. /// 导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线
  164. /// </summary>
  165. /// <param name="viewContent">对应的注释面板</param>
  166. /// <param name="toolTag">导航到同一个注释xaml时,需要区分某个注释;比如高亮、删除线、下划线</param>
  167. /// <param name="annot">注释</param>
  168. /// <param name="annotAttribsList">更改注释属性的键值对,更改值后会自动记录undoRedo容器里</param>
  169. private void AddToPropertyPanel(string viewContent, string toolTag = null, List<AnnotHandlerEventArgs> annots = null, Dictionary<AnnotAttrib, object> annotAttribsList = null, AnnotAttribEvent annotAttribEvent = null)
  170. {
  171. if (annots != null)
  172. {
  173. propertyPanel.annotlists = annots;
  174. propertyPanel.annot = annots[0];
  175. }
  176. else
  177. {
  178. propertyPanel.annotlists = null;
  179. propertyPanel.annot = null;
  180. }
  181. if (annotAttribsList != null)
  182. {
  183. if (annots.Count > 1)
  184. {
  185. if (propertyPanel.AnnotEvents == null)
  186. propertyPanel.AnnotEvents = new List<AnnotAttribEvent>();
  187. propertyPanel.AnnotEvents.Clear();
  188. foreach (var itemAnnot in annots)
  189. {
  190. var eventitem = AnnotAttribEvent.GetAnnotAttribEvent(itemAnnot, annotAttribsList);
  191. propertyPanel.AnnotEvents.Add(eventitem);
  192. }
  193. }
  194. propertyPanel.AnnotEvent = AnnotAttribEvent.GetAnnotAttribEvent(annots[0], annotAttribsList);
  195. if (annots[0] is LinkAnnotArgs && annotAttribEvent != null)
  196. {
  197. propertyPanel.AnnotEvent = annotAttribEvent;
  198. }
  199. }
  200. if (string.IsNullOrEmpty(viewContent) == false)
  201. {
  202. viewContentViewModel.SelectedPrpoertyPanel(viewContent, propertyPanel);
  203. }
  204. }
  205. private void NavigateToProperty(string btnName)
  206. {
  207. NavigationParameters values = new NavigationParameters();
  208. values.Add(ParameterNames.PDFViewer, PDFViewer);
  209. regions.RequestNavigate(RegionNames.PropertyRegionName,btnToProperty[btnName]);
  210. viewContentViewModel.IsPropertyOpen = true;
  211. }
  212. /// <summary>
  213. /// 初始化按钮名称-属性面板字典
  214. /// </summary>
  215. private void InitDictionary()
  216. {
  217. btnToProperty["RbtnText"] = "TextFillProperty";
  218. btnToProperty["RbtnTick"] = "ShapFillProperty";
  219. btnToProperty["RbtnFork"] = "ShapFillProperty";
  220. btnToProperty["RbtnRectangle"] = "ShapFillProperty";
  221. btnToProperty["RbtnLine"] = "ShapFillProperty";
  222. btnToProperty["RbtnPoint"] = "ShapFillProperty";
  223. btnToProperty["RbtnDate"] = "DateFillProperty";
  224. btnToProperty["RbtnSign"] = "";
  225. }
  226. #region Navigation
  227. public bool IsNavigationTarget(NavigationContext navigationContext)
  228. {
  229. return true;
  230. }
  231. public void OnNavigatedFrom(NavigationContext navigationContext)
  232. {
  233. }
  234. public void OnNavigatedTo(NavigationContext navigationContext)
  235. {
  236. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  237. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  238. }
  239. #endregion
  240. }
  241. }