RedactionContentViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. private bool isEnable = false;
  69. /// <summary>
  70. /// 按钮是否启用
  71. /// </summary>
  72. public bool IsEnable
  73. {
  74. get { return isEnable; }
  75. set
  76. {
  77. SetProperty(ref isEnable, value);
  78. }
  79. }
  80. public RedactionContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IDialogService dialogService)
  81. {
  82. this.redactionRegion = regionManager;
  83. this.eventAggregator = eventAggregator;
  84. this.dialogs = dialogService;
  85. RedactionDocumentRegionName = Guid.NewGuid().ToString();
  86. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  87. CloseEditToolCommand = new DelegateCommand(CloseEditTool);
  88. ApplyCommmand = new DelegateCommand(apply,CanExcute).ObservesProperty(() => IsEnable);
  89. EraseCommand = new DelegateCommand(erase, CanExcute).ObservesProperty(() => IsEnable);
  90. PageRedactionCommand = new DelegateCommand(pageMark);
  91. eventAggregator.GetEvent<RedactionCommandEvent>().Subscribe(SetContextMenu, e => e.UniCode == Unicode);
  92. }
  93. private void SetContextMenu(RedactionCommandEventArgs obj)
  94. {
  95. var contextmenu = App.Current.FindResource("RedactionContextMenu") as ContextMenu;
  96. obj.args.PopupMenu = contextmenu;
  97. for(int i=0;i<contextmenu.Items.Count;i++)
  98. {
  99. if(contextmenu.Items[i] is MenuItem)
  100. {
  101. var item = contextmenu.Items[i] as MenuItem;
  102. switch (item.Name)
  103. {
  104. case "MenuDelete":
  105. item.Command = ApplicationCommands.Delete;
  106. item.CommandParameter = (UIElement)obj.Sender;
  107. break;
  108. case "MenuSetDeufalt":
  109. item.Command = new DelegateCommand<RedactionCommandEventArgs>(SetDefualtProperty);
  110. item.CommandParameter = obj;
  111. break;
  112. case "MenuProperties":
  113. item.Command = new DelegateCommand<RedactionCommandEventArgs>(ShowProperty);
  114. item.CommandParameter = obj;
  115. break;
  116. case "MenuRepeat":
  117. item.Command = new DelegateCommand<RedactionCommandEventArgs>(RepreatMark);
  118. item.CommandParameter = obj;
  119. break;
  120. case "MenuApply":
  121. item.Command = this.ApplyCommmand;
  122. break;
  123. case "MenuErase":
  124. item.Command = this.EraseCommand;
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// 设置当前为默认属性
  134. /// </summary>
  135. /// <param name="args"></param>
  136. private void SetDefualtProperty(RedactionCommandEventArgs args)
  137. {
  138. var annoteargs = args.args.AnnotEventArgsList[0] as RedactionAnnotArgs;
  139. DialogParameters keyValues = new DialogParameters();
  140. keyValues.Add(ParameterNames.DataModel,args.args.AnnotEventArgsList[0] as RedactionAnnotArgs);
  141. 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(),annoteargs.FontWeight);
  142. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  143. PDFViewer.SetToolParam(annoteargs);
  144. }
  145. /// <summary>
  146. /// 显示属性弹窗
  147. /// </summary>
  148. /// <param name="args"></param>
  149. private void ShowProperty(RedactionCommandEventArgs args)
  150. {
  151. DialogParameters keyValues = new DialogParameters();
  152. keyValues.Add(ParameterNames.DataModel, args.args.AnnotEventArgsList[0] as RedactionAnnotArgs);
  153. dialogs.ShowDialog(DialogNames.MarkSettingDialog,keyValues,null);
  154. }
  155. /// <summary>
  156. /// 显示跨页标记弹窗
  157. /// </summary>
  158. /// <param name="args"></param>
  159. private void RepreatMark(RedactionCommandEventArgs args)
  160. {
  161. DialogParameters keyValues = new DialogParameters();
  162. keyValues.Add(ParameterNames.PageCount,PDFViewer.Document.PageCount);
  163. dialogs.ShowDialog(DialogNames.RepeatMarkDialog, keyValues, e=> {
  164. if (e.Result == ButtonResult.OK)
  165. {
  166. PDFViewer.AddRedaction(e.Parameters.GetValue<List<int>>(ParameterNames.PageList), args.args);
  167. }
  168. });
  169. }
  170. /// <summary>
  171. /// 应用
  172. /// </summary>
  173. private void apply()
  174. {
  175. AlertsMessage alertsMessage = new AlertsMessage();
  176. alertsMessage.ShowDialog("Apply Redactions", "Use blank blocks to remove selected sensitive content."+
  177. "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");
  178. if(alertsMessage.result== ContentResult.Ok)
  179. {
  180. viewContentViewModel.saveAsFile(true);
  181. }
  182. }
  183. /// <summary>
  184. /// 擦除
  185. /// </summary>
  186. private void erase()
  187. {
  188. AlertsMessage alertsMessage = new AlertsMessage();
  189. alertsMessage.ShowDialog("","Replace selected sensitive content with color blocks."+
  190. "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");
  191. if (alertsMessage.result == ContentResult.Ok)
  192. {
  193. viewContentViewModel.saveAsFile(false,true);
  194. }
  195. }
  196. private bool CanExcute()
  197. {
  198. return IsEnable;
  199. }
  200. private void pageMark()
  201. {
  202. DialogParameters valuePairs = new DialogParameters();
  203. valuePairs.Add(ParameterNames.PageCount,PDFViewer.Document.PageCount);
  204. valuePairs.Add(ParameterNames.CurrentPageIndex,PDFViewer.CurrentIndex);
  205. dialogs.ShowDialog(DialogNames.PageMarkDialog,valuePairs,e=> {
  206. if(e.Result == ButtonResult.OK)
  207. {
  208. var pagelist = e.Parameters.GetValue<List<int>>(ParameterNames.PageList);
  209. addMarkToPages(pagelist);
  210. }
  211. });
  212. }
  213. private void addMarkToPages(List<int> list)
  214. {
  215. foreach (var page in list)
  216. {
  217. //根据页面大小,创建标记密文注释
  218. CPDFPage docPage = PDFViewer.Document.PageAtIndex(page);
  219. CPDFRedactAnnotation redactionAnnot = docPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  220. if (redactionAnnot == null)
  221. {
  222. //TODO 操作失败
  223. return;
  224. }
  225. redactionAnnot.SetQuardRects(new List<CRect>() { new CRect(0, (float)docPage.PageSize.Height, (float)docPage.PageSize.Width,0)});
  226. if (redactionArgs == null)
  227. return;
  228. byte[] lineColor = { redactionArgs.LineColor.R, redactionArgs.LineColor.G, redactionArgs.LineColor.B };
  229. redactionAnnot.SetOutlineColor(lineColor);
  230. if (redactionArgs.BgColor != Colors.Transparent)
  231. {
  232. byte[] bgColor = { redactionArgs.BgColor.R, redactionArgs.BgColor.G, redactionArgs.BgColor.B };
  233. redactionAnnot.SetFillColor(bgColor);
  234. }
  235. else
  236. {
  237. // redactionAnnot.ClearBgColor();
  238. }
  239. CTextAttribute textAttr = new CTextAttribute();
  240. byte[] fontColor = { redactionArgs.FontColor.R, redactionArgs.FontColor.G, redactionArgs.FontColor.B };
  241. textAttr.FontColor = fontColor;
  242. textAttr.FontSize = (float)redactionArgs.FontSize;
  243. redactionAnnot.SetTextAttribute(textAttr);
  244. switch (redactionArgs.Align)
  245. {
  246. case TextAlignment.Left:
  247. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  248. break;
  249. case TextAlignment.Center:
  250. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  251. break;
  252. case TextAlignment.Right:
  253. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  254. break;
  255. default:
  256. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  257. break;
  258. }
  259. //byte transparency = (byte)Math.Round(redactionArgs.Transparency * 255);
  260. //redactionAnnot.SetTransparency(transparency);
  261. redactionAnnot.SetBorderWidth(1);
  262. //redactionAnnot.SetRect(new CRect(Left, Bottom, Right, Top));
  263. //redactionAnnot.SetCreationDate(Helpers.GetCurrentPdfTime());
  264. if (string.IsNullOrEmpty(Settings.Default.AppProperties.Description.Author))
  265. {
  266. redactionAnnot.SetAuthor(Settings.Default.AppProperties.Description.Author);
  267. }
  268. if (redactionArgs.Content != null && redactionArgs.Content != string.Empty)
  269. {
  270. redactionAnnot.SetOverlayText(redactionArgs.Content);
  271. }
  272. if (redactionAnnot.GetIsLocked() != redactionArgs.Locked)
  273. {
  274. redactionAnnot.SetIsLocked(redactionArgs.Locked);
  275. }
  276. redactionAnnot.UpdateAp();
  277. redactionAnnot.ReleaseAnnot();
  278. }
  279. }
  280. public void CloseEditTool()
  281. {
  282. bool isClose = true;
  283. if (CheckHasRedactionAnnote())
  284. {
  285. AlertsMessage alertsMessage = new AlertsMessage();
  286. alertsMessage.ShowDialog("", "There are unapplied redactions in this file. Exit will not save redaction.", "Cancel", "Exit");
  287. if (alertsMessage.result != ContentResult.Ok)
  288. {
  289. isClose = false;
  290. }
  291. }
  292. if(isClose)
  293. {
  294. PDFViewer.SetMouseMode(MouseModes.Default);
  295. redactionRegion.Regions[RegionNames.ViwerRegionName].Remove(PDFViewer);
  296. redactionRegion.Regions[RedactionDocumentRegionName].Remove(PDFViewer);
  297. this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = this.Unicode, Status = EnumCloseMode.StatusCancel });
  298. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = false;
  299. }
  300. }
  301. /// <summary>
  302. /// 检查是否有未应用的标记密文
  303. /// </summary>
  304. private bool CheckHasRedactionAnnote()
  305. {
  306. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  307. {
  308. var items = PDFViewer.GetAnnotCommentList(i, PDFViewer.Document);
  309. for (int j = 0; j < items.Count; j++)
  310. {
  311. if (items[j].EventType == AnnotArgsType.AnnotRedaction)
  312. {
  313. return true;
  314. }
  315. }
  316. }
  317. return false;
  318. }
  319. #region Navigation
  320. public bool IsNavigationTarget(NavigationContext navigationContext)
  321. {
  322. return true;
  323. }
  324. public void OnNavigatedFrom(NavigationContext navigationContext)
  325. {
  326. }
  327. public void OnNavigatedTo(NavigationContext navigationContext)
  328. {
  329. App.mainWindowViewModel.SelectedItem.IsInReadctonMode = true;
  330. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  331. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  332. PDFViewer.InfoChanged -= PDFViewer_InfoChanged;
  333. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  334. PDFViewer.AnnotEditHandler -= PDFViewer_AnnotEditHandler;
  335. PDFViewer.AnnotEditHandler += PDFViewer_AnnotEditHandler;
  336. CurrentPage = PDFViewer.CurrentIndex + 1;
  337. PageCount = PDFViewer.Document.PageCount;
  338. if (!redactionRegion.Regions[RedactionDocumentRegionName].Views.Contains(PDFViewer))
  339. {
  340. redactionArgs = new RedactionAnnotArgs();
  341. AnnotHandlerEventArgs annotArgs = null;
  342. redactionArgs.LineColor = Settings.Default.RedactionsSettings.LineColor;
  343. redactionArgs.BgColor = Settings.Default.RedactionsSettings.BgColor;
  344. redactionArgs.FontColor = Settings.Default.RedactionsSettings.FontColor;
  345. redactionArgs.Align = Settings.Default.RedactionsSettings.Align;
  346. redactionArgs.FontSize = Settings.Default.RedactionsSettings.FontSize;
  347. redactionArgs.Content = Settings.Default.RedactionsSettings.Content;
  348. if (!Settings.Default.RedactionsSettings.isUseText)
  349. {
  350. redactionArgs.Content = "";
  351. }
  352. annotArgs = redactionArgs;
  353. if (annotArgs != null)
  354. {
  355. //设置注释作者
  356. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  357. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  358. PDFViewer.SetToolParam(annotArgs);
  359. }
  360. redactionRegion.AddToRegion(RedactionDocumentRegionName, PDFViewer);
  361. }
  362. }
  363. private void PDFViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  364. {
  365. //添加注释后检测是否有未应用的标记密文注释
  366. IsEnable = false;
  367. for(int i=0;i<PDFViewer.Document.PageCount;i++)
  368. {
  369. var annotes = PDFViewer.GetAnnotCommentList(i,PDFViewer.Document);
  370. for(int j=0;j<annotes.Count;j++)
  371. {
  372. if(annotes[j].EventType == AnnotArgsType.AnnotRedaction)
  373. {
  374. IsEnable = true;
  375. return;
  376. }
  377. }
  378. }
  379. }
  380. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  381. {
  382. isFromSelf = true;
  383. if(e.Key=="PageNum")
  384. {
  385. var data = e.Value as RenderData;
  386. CurrentPage = data.PageIndex;
  387. }
  388. isFromSelf = false;
  389. }
  390. #endregion
  391. }
  392. }