RedactionContentViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFPage;
  4. using ComPDFKitViewer;
  5. using ComPDFKitViewer.AnnotEvent;
  6. using ComPDFKitViewer.PdfViewer;
  7. using PDF_Office.CustomControl;
  8. using PDF_Office.EventAggregators;
  9. using PDF_Office.Model;
  10. using PDF_Office.Properties;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using Prism.Services.Dialogs;
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. namespace PDF_Office.ViewModels.EditTools.Redaction
  24. {
  25. public class RedactionContentViewModel : BindableBase, INavigationAware
  26. {
  27. public IEventAggregator eventAggregator;
  28. public IRegionManager redactionRegion;
  29. public IDialogService dialogs;
  30. private CPDFViewer PDFViewer;
  31. private ViewContentViewModel viewContentViewModel;
  32. private RedactionAnnotArgs redactionArgs = new RedactionAnnotArgs();
  33. public string RedactionDocumentRegionName { get; set; }
  34. public string RedactionBottomBarRegionName { get; set; }
  35. public string RedactionDocumentName = "RedactionDocumentContent";
  36. public string Unicode = null;
  37. public DelegateCommand CloseEditToolCommand { get; set; }
  38. public DelegateCommand ApplyCommmand { get; set; }
  39. public DelegateCommand PageRedactionCommand { get; set; }
  40. public DelegateCommand EraseCommand { get; set; }
  41. public RedactionCommandEventArgs TempArgs { get; set; }
  42. private int currentPage = 0;
  43. /// <summary>
  44. /// 是否因为本身传递值 防止循环调用
  45. /// </summary>
  46. private bool isFromSelf = false;
  47. public int CurrentPage
  48. {
  49. get { return currentPage; }
  50. set
  51. {
  52. SetProperty(ref currentPage, value);
  53. if (value >= 1 && value <= PDFViewer.Document.PageCount && !isFromSelf)
  54. {
  55. PDFViewer.GoToPage(value - 1);
  56. }
  57. }
  58. }
  59. private int pageCount = 0;
  60. public int PageCount
  61. {
  62. get { return pageCount; }
  63. set
  64. {
  65. SetProperty(ref pageCount, value);
  66. }
  67. }
  68. public RedactionContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
  69. {
  70. this.redactionRegion = regionManager;
  71. this.eventAggregator = eventAggregator;
  72. this.dialogs = dialogService;
  73. RedactionDocumentRegionName = Guid.NewGuid().ToString();
  74. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  75. CloseEditToolCommand = new DelegateCommand(CloseEditTool);
  76. ApplyCommmand = new DelegateCommand(apply);
  77. EraseCommand = new DelegateCommand(erase);
  78. PageRedactionCommand = new DelegateCommand(pageMark);
  79. eventAggregator.GetEvent<RedactionCommandEvent>().Subscribe(SetContextMenu, e => e.UniCode == Unicode);
  80. }
  81. private void SetContextMenu(RedactionCommandEventArgs obj)
  82. {
  83. var contextmenu = App.Current.FindResource("RedactionContextMenu") as ContextMenu;
  84. obj.args.PopupMenu = contextmenu;
  85. for(int i=0;i<contextmenu.Items.Count;i++)
  86. {
  87. if(contextmenu.Items[i] is MenuItem)
  88. {
  89. var item = contextmenu.Items[i] as MenuItem;
  90. switch (item.Name)
  91. {
  92. case "MenuSetDeufalt":
  93. item.Command = new DelegateCommand<RedactionCommandEventArgs>(SetDefualtProperty);
  94. item.CommandParameter = obj;
  95. break;
  96. case "MenuProperties":
  97. item.Command = new DelegateCommand<RedactionCommandEventArgs>(ShowProperty);
  98. item.CommandParameter = obj;
  99. break;
  100. case "MenuRepeat":
  101. item.Command = new DelegateCommand<RedactionCommandEventArgs>(RepreatMark);
  102. item.CommandParameter = obj;
  103. break;
  104. case "MenuApply":
  105. item.Command = this.ApplyCommmand;
  106. break;
  107. case "MenuErase":
  108. item.Command = this.EraseCommand;
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// 设置当前为默认属性
  118. /// </summary>
  119. /// <param name="args"></param>
  120. private void SetDefualtProperty(RedactionCommandEventArgs args)
  121. {
  122. var annoteargs = args.args.AnnotEventArgsList[0] as RedactionAnnotArgs;
  123. DialogParameters keyValues = new DialogParameters();
  124. keyValues.Add(ParameterNames.DataModel,args.args.AnnotEventArgsList[0] as RedactionAnnotArgs);
  125. Settings.Default.RedactionsSettings.SetDefualtValue(annoteargs.Content, annoteargs.LineColor,annoteargs.BgColor,annoteargs.FontColor, (int)annoteargs.FontSize,string.IsNullOrEmpty(annoteargs.Content) ? false : true, annoteargs.Align, annoteargs.FontFamily.ToString(),FontWeights.Regular);
  126. }
  127. /// <summary>
  128. /// 显示属性弹窗
  129. /// </summary>
  130. /// <param name="args"></param>
  131. private void ShowProperty(RedactionCommandEventArgs args)
  132. {
  133. DialogParameters keyValues = new DialogParameters();
  134. keyValues.Add(ParameterNames.DataModel, args.args.AnnotEventArgsList[0] as RedactionAnnotArgs);
  135. dialogs.ShowDialog(DialogNames.MarkSettingDialog,keyValues,null);
  136. }
  137. /// <summary>
  138. /// 显示跨页标记弹窗
  139. /// </summary>
  140. /// <param name="args"></param>
  141. private void RepreatMark(RedactionCommandEventArgs args)
  142. {
  143. DialogParameters keyValues = new DialogParameters();
  144. keyValues.Add(ParameterNames.PageCount,PDFViewer.Document.PageCount);
  145. dialogs.ShowDialog(DialogNames.RepeatMarkDialog, keyValues, e=> {
  146. if (e.Result == ButtonResult.OK)
  147. {
  148. PDFViewer.AddRedaction(e.Parameters.GetValue<List<int>>(ParameterNames.PageList), args.args);
  149. }
  150. });
  151. }
  152. /// <summary>
  153. /// 应用
  154. /// </summary>
  155. private void apply()
  156. {
  157. AlertsMessage alertsMessage = new AlertsMessage();
  158. alertsMessage.ShowDialog("Apply Redactions", "Use blank blocks to remove selected sensitive content."+
  159. "This action will permanently delete the marked ciphertext information from this document and you will not be able to retrieve the marked ciphertext information.","Cancel","Apply & Save As");
  160. if(alertsMessage.result== ContentResult.Ok)
  161. {
  162. viewContentViewModel.saveAsFile(true);
  163. }
  164. }
  165. /// <summary>
  166. /// 擦除
  167. /// </summary>
  168. private void erase()
  169. {
  170. }
  171. private void pageMark()
  172. {
  173. DialogParameters valuePairs = new DialogParameters();
  174. valuePairs.Add(ParameterNames.PageCount,PDFViewer.Document.PageCount);
  175. valuePairs.Add(ParameterNames.CurrentPageIndex,PDFViewer.CurrentIndex);
  176. dialogs.ShowDialog(DialogNames.PageMarkDialog,valuePairs,e=> {
  177. if(e.Result == ButtonResult.OK)
  178. {
  179. var pagelist = e.Parameters.GetValue<List<int>>(ParameterNames.PageList);
  180. addMarkToPages(pagelist);
  181. }
  182. });
  183. }
  184. private void addMarkToPages(List<int> list)
  185. {
  186. foreach (var page in list)
  187. {
  188. //根据页面大小,创建标记密文注释
  189. CPDFPage docPage = PDFViewer.Document.PageAtIndex(page);
  190. CPDFRedactAnnotation redactionAnnot = docPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  191. if (redactionAnnot == null)
  192. {
  193. //TODO 操作失败
  194. return;
  195. }
  196. redactionAnnot.SetQuardRects(new List<CRect>() { new CRect(0, (float)docPage.PageSize.Height, (float)docPage.PageSize.Width,0)});
  197. if (redactionArgs == null)
  198. return;
  199. byte[] lineColor = { redactionArgs.LineColor.R, redactionArgs.LineColor.G, redactionArgs.LineColor.B };
  200. redactionAnnot.SetOutlineColor(lineColor);
  201. if (redactionArgs.BgColor != Colors.Transparent)
  202. {
  203. byte[] bgColor = { redactionArgs.BgColor.R, redactionArgs.BgColor.G, redactionArgs.BgColor.B };
  204. redactionAnnot.SetFillColor(bgColor);
  205. }
  206. else
  207. {
  208. // redactionAnnot.ClearBgColor();
  209. }
  210. CTextAttribute textAttr = new CTextAttribute();
  211. byte[] fontColor = { redactionArgs.FontColor.R, redactionArgs.FontColor.G, redactionArgs.FontColor.B };
  212. textAttr.FontColor = fontColor;
  213. textAttr.FontSize = (float)redactionArgs.FontSize;
  214. redactionAnnot.SetTextAttribute(textAttr);
  215. switch (redactionArgs.Align)
  216. {
  217. case TextAlignment.Left:
  218. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  219. break;
  220. case TextAlignment.Center:
  221. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  222. break;
  223. case TextAlignment.Right:
  224. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  225. break;
  226. default:
  227. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  228. break;
  229. }
  230. //byte transparency = (byte)Math.Round(redactionArgs.Transparency * 255);
  231. //redactionAnnot.SetTransparency(transparency);
  232. redactionAnnot.SetBorderWidth(1);
  233. //redactionAnnot.SetRect(new CRect(Left, Bottom, Right, Top));
  234. //redactionAnnot.SetCreationDate(Helpers.GetCurrentPdfTime());
  235. if (string.IsNullOrEmpty(Settings.Default.AppProperties.Description.Author))
  236. {
  237. redactionAnnot.SetAuthor(Settings.Default.AppProperties.Description.Author);
  238. }
  239. if (redactionArgs.Content != null && redactionArgs.Content != string.Empty)
  240. {
  241. redactionAnnot.SetOverlayText(redactionArgs.Content);
  242. }
  243. if (redactionAnnot.GetIsLocked() != redactionArgs.Locked)
  244. {
  245. redactionAnnot.SetIsLocked(redactionArgs.Locked);
  246. }
  247. redactionAnnot.UpdateAp();
  248. redactionAnnot.ReleaseAnnot();
  249. }
  250. }
  251. public void CloseEditTool()
  252. {
  253. bool isClose = true;
  254. if (CheckHasRedactionAnnote())
  255. {
  256. AlertsMessage alertsMessage = new AlertsMessage();
  257. alertsMessage.ShowDialog("", "There are unapplied redactions in this file. Exit will not save redaction.", "Cancel", "Exit");
  258. if (alertsMessage.result != ContentResult.Ok)
  259. {
  260. isClose = false;
  261. }
  262. }
  263. if(isClose)
  264. {
  265. PDFViewer.SetMouseMode(MouseModes.Default);
  266. redactionRegion.Regions[RegionNames.ViwerRegionName].Remove(PDFViewer);
  267. redactionRegion.Regions[RedactionDocumentRegionName].Remove(PDFViewer);
  268. this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = this.Unicode, Status = EnumCloseMode.StatusCancel });
  269. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  270. }
  271. }
  272. /// <summary>
  273. /// 检查是否有未应用的标记密文
  274. /// </summary>
  275. private bool CheckHasRedactionAnnote()
  276. {
  277. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  278. {
  279. var items = PDFViewer.GetAnnotCommentList(i, PDFViewer.Document);
  280. for (int j = 0; j < items.Count; j++)
  281. {
  282. if (items[j].EventType == AnnotArgsType.AnnotRedaction)
  283. {
  284. return true;
  285. }
  286. }
  287. }
  288. return false;
  289. }
  290. #region Navigation
  291. public bool IsNavigationTarget(NavigationContext navigationContext)
  292. {
  293. return true;
  294. }
  295. public void OnNavigatedFrom(NavigationContext navigationContext)
  296. {
  297. }
  298. public void OnNavigatedTo(NavigationContext navigationContext)
  299. {
  300. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  301. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  302. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  303. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  304. CurrentPage = PDFViewer.CurrentIndex + 1;
  305. PageCount = PDFViewer.Document.PageCount;
  306. if (!redactionRegion.Regions[RedactionDocumentRegionName].Views.Contains(PDFViewer))
  307. {
  308. redactionArgs = new RedactionAnnotArgs();
  309. AnnotHandlerEventArgs annotArgs = null;
  310. redactionArgs.LineColor = Settings.Default.RedactionsSettings.LineColor;
  311. redactionArgs.BgColor = Settings.Default.RedactionsSettings.BgColor;
  312. redactionArgs.FontColor = Settings.Default.RedactionsSettings.FontColor;
  313. redactionArgs.Align = Settings.Default.RedactionsSettings.Align;
  314. redactionArgs.FontSize = Settings.Default.RedactionsSettings.FontSize;
  315. redactionArgs.Content = Settings.Default.RedactionsSettings.Content;
  316. if (!Settings.Default.RedactionsSettings.isUseText)
  317. {
  318. redactionArgs.Content = "";
  319. }
  320. annotArgs = redactionArgs;
  321. if (annotArgs != null)
  322. {
  323. //设置注释作者
  324. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  325. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  326. PDFViewer.SetToolParam(annotArgs);
  327. }
  328. redactionRegion.AddToRegion(RedactionDocumentRegionName, PDFViewer);
  329. }
  330. }
  331. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  332. {
  333. isFromSelf = true;
  334. if(e.Key=="PageNum")
  335. {
  336. var data = e.Value as RenderData;
  337. CurrentPage = data.PageIndex;
  338. }
  339. isFromSelf = false;
  340. }
  341. #endregion
  342. }
  343. }