RedactionContentViewModel.cs 17 KB

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