RedactionContentViewModel.cs 15 KB

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