RedactionContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.Media;
  21. namespace PDF_Office.ViewModels.EditTools.Redaction
  22. {
  23. public class RedactionContentViewModel : BindableBase,INavigationAware
  24. {
  25. public IEventAggregator eventAggregator;
  26. public IRegionManager redactionRegion;
  27. public IDialogService dialogs;
  28. private CPDFViewer PDFViewer;
  29. private ViewContentViewModel viewContentViewModel;
  30. private RedactionAnnotArgs redactionArgs = new RedactionAnnotArgs();
  31. public string RedactionDocumentRegionName { get; set; }
  32. public string RedactionBottomBarRegionName { get; set; }
  33. public string RedactionDocumentName = "RedactionDocumentContent";
  34. public string Unicode = null;
  35. public DelegateCommand CloseEditToolCommand { get; set; }
  36. public DelegateCommand ApplyCommmand { get; set; }
  37. public DelegateCommand RemoveCommand { get; set; }
  38. public DelegateCommand PageRedactionCommand { get; set; }
  39. private int currentPage = 0;
  40. /// <summary>
  41. /// 是否因为本身传递值 防止循环调用
  42. /// </summary>
  43. private bool isFromSelf = false;
  44. public int CurrentPage
  45. {
  46. get { return currentPage; }
  47. set
  48. {
  49. SetProperty(ref currentPage, value);
  50. if(value>=1&&value<=PDFViewer.Document.PageCount&&!isFromSelf)
  51. {
  52. PDFViewer.GoToPage(value-1);
  53. }
  54. }
  55. }
  56. private int pageCount = 0;
  57. public int PageCount
  58. {
  59. get { return pageCount; }
  60. set
  61. {
  62. SetProperty(ref pageCount, value);
  63. }
  64. }
  65. public RedactionContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator,IDialogService dialogService)
  66. {
  67. this.redactionRegion = regionManager;
  68. this.eventAggregator = eventAggregator;
  69. this.dialogs = dialogService;
  70. RedactionDocumentRegionName = Guid.NewGuid().ToString();
  71. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  72. CloseEditToolCommand = new DelegateCommand(CloseEditTool);
  73. ApplyCommmand = new DelegateCommand(apply);
  74. RemoveCommand = new DelegateCommand(remove);
  75. PageRedactionCommand = new DelegateCommand(pageMark);
  76. }
  77. /// <summary>
  78. /// 应用
  79. /// </summary>
  80. private void apply()
  81. {
  82. AlertsMessage alertsMessage = new AlertsMessage();
  83. alertsMessage.ShowDialog("Apply Redactions", "Use blank blocks to remove selected sensitive content."+
  84. "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");
  85. if(alertsMessage.result== ContentResult.Ok)
  86. {
  87. viewContentViewModel.saveAsFile(true);
  88. }
  89. }
  90. /// <summary>
  91. /// 擦除
  92. /// </summary>
  93. private void remove()
  94. {
  95. }
  96. private void pageMark()
  97. {
  98. DialogParameters valuePairs = new DialogParameters();
  99. valuePairs.Add(ParameterNames.PageCount,PDFViewer.Document.PageCount);
  100. valuePairs.Add(ParameterNames.CurrentPageIndex,PDFViewer.CurrentIndex);
  101. dialogs.ShowDialog(DialogNames.PageMarkDialog,valuePairs,e=> {
  102. if(e.Result == ButtonResult.OK)
  103. {
  104. var pagelist = e.Parameters.GetValue<List<int>>(ParameterNames.PageList);
  105. addMarkToPages(pagelist);
  106. }
  107. });
  108. }
  109. private void addMarkToPages(List<int> list)
  110. {
  111. foreach (var page in list)
  112. {
  113. //根据页面大小,创建标记密文注释
  114. CPDFPage docPage = PDFViewer.Document.PageAtIndex(page);
  115. CPDFRedactAnnotation redactionAnnot = docPage.CreateAnnot(C_ANNOTATION_TYPE.C_ANNOTATION_REDACT) as CPDFRedactAnnotation;
  116. if (redactionAnnot == null)
  117. {
  118. //TODO 操作失败
  119. return;
  120. }
  121. redactionAnnot.SetQuardRects(new List<CRect>() { new CRect(0, (float)docPage.PageSize.Height, (float)docPage.PageSize.Width,0)});
  122. if (redactionArgs == null)
  123. return;
  124. byte[] lineColor = { redactionArgs.LineColor.R, redactionArgs.LineColor.G, redactionArgs.LineColor.B };
  125. redactionAnnot.SetOutlineColor(lineColor);
  126. if (redactionArgs.BgColor != Colors.Transparent)
  127. {
  128. byte[] bgColor = { redactionArgs.BgColor.R, redactionArgs.BgColor.G, redactionArgs.BgColor.B };
  129. redactionAnnot.SetFillColor(bgColor);
  130. }
  131. else
  132. {
  133. // redactionAnnot.ClearBgColor();
  134. }
  135. CTextAttribute textAttr = new CTextAttribute();
  136. byte[] fontColor = { redactionArgs.FontColor.R, redactionArgs.FontColor.G, redactionArgs.FontColor.B };
  137. textAttr.FontColor = fontColor;
  138. textAttr.FontSize = (float)redactionArgs.FontSize;
  139. redactionAnnot.SetTextAttribute(textAttr);
  140. switch (redactionArgs.Align)
  141. {
  142. case TextAlignment.Left:
  143. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  144. break;
  145. case TextAlignment.Center:
  146. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_CENTER);
  147. break;
  148. case TextAlignment.Right:
  149. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_RIGHT);
  150. break;
  151. default:
  152. redactionAnnot.SetTextAlignment(C_TEXT_ALIGNMENT.ALIGNMENT_LEFT);
  153. break;
  154. }
  155. //byte transparency = (byte)Math.Round(redactionArgs.Transparency * 255);
  156. //redactionAnnot.SetTransparency(transparency);
  157. redactionAnnot.SetBorderWidth(1);
  158. //redactionAnnot.SetRect(new CRect(Left, Bottom, Right, Top));
  159. //redactionAnnot.SetCreationDate(Helpers.GetCurrentPdfTime());
  160. if (string.IsNullOrEmpty(Settings.Default.AppProperties.Description.Author))
  161. {
  162. redactionAnnot.SetAuthor(Settings.Default.AppProperties.Description.Author);
  163. }
  164. if (redactionArgs.Content != null && redactionArgs.Content != string.Empty)
  165. {
  166. redactionAnnot.SetOverlayText(redactionArgs.Content);
  167. }
  168. if (redactionAnnot.GetIsLocked() != redactionArgs.Locked)
  169. {
  170. redactionAnnot.SetIsLocked(redactionArgs.Locked);
  171. }
  172. redactionAnnot.UpdateAp();
  173. redactionAnnot.ReleaseAnnot();
  174. }
  175. }
  176. public void CloseEditTool()
  177. {
  178. bool isClose = true;
  179. if (CheckHasRedactionAnnote())
  180. {
  181. AlertsMessage alertsMessage = new AlertsMessage();
  182. alertsMessage.ShowDialog("", "There are unapplied redactions in this file. Exit will not save redaction.", "Cancel", "Exit");
  183. if (alertsMessage.result != ContentResult.Ok)
  184. {
  185. isClose = false;
  186. }
  187. }
  188. if(isClose)
  189. {
  190. PDFViewer.SetMouseMode(MouseModes.Default);
  191. redactionRegion.Regions[RegionNames.ViwerRegionName].Remove(PDFViewer);
  192. redactionRegion.Regions[RedactionDocumentRegionName].Remove(PDFViewer);
  193. this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = this.Unicode, Status = EnumCloseMode.StatusCancel });
  194. }
  195. }
  196. /// <summary>
  197. /// 检查是否有未应用的标记密文
  198. /// </summary>
  199. private bool CheckHasRedactionAnnote()
  200. {
  201. for (int i = 0; i < PDFViewer.Document.PageCount; i++)
  202. {
  203. var items = PDFViewer.GetAnnotCommentList(i, PDFViewer.Document);
  204. for (int j = 0; j < items.Count; j++)
  205. {
  206. if (items[j].EventType == AnnotArgsType.AnnotRedaction)
  207. {
  208. return true;
  209. }
  210. }
  211. }
  212. return false;
  213. }
  214. #region Navigation
  215. public bool IsNavigationTarget(NavigationContext navigationContext)
  216. {
  217. return true;
  218. }
  219. public void OnNavigatedFrom(NavigationContext navigationContext)
  220. {
  221. }
  222. public void OnNavigatedTo(NavigationContext navigationContext)
  223. {
  224. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  225. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  226. PDFViewer.InfoChanged += PDFViewer_InfoChanged;
  227. CurrentPage = PDFViewer.CurrentIndex + 1;
  228. PageCount = PDFViewer.Document.PageCount;
  229. if (!redactionRegion.Regions[RedactionDocumentRegionName].Views.Contains(PDFViewer))
  230. {
  231. redactionArgs = new RedactionAnnotArgs();
  232. AnnotHandlerEventArgs annotArgs = null;
  233. redactionArgs.LineColor = Settings.Default.RedactionsSettings.LineColor;
  234. redactionArgs.BgColor = Settings.Default.RedactionsSettings.BgColor;
  235. redactionArgs.FontColor = Settings.Default.RedactionsSettings.FontColor;
  236. redactionArgs.Align = Settings.Default.RedactionsSettings.Align;
  237. redactionArgs.FontSize = Settings.Default.RedactionsSettings.FontSize;
  238. redactionArgs.Content = Settings.Default.RedactionsSettings.Content;
  239. if (!Settings.Default.RedactionsSettings.isUseText)
  240. {
  241. redactionArgs.Content = "";
  242. }
  243. annotArgs = redactionArgs;
  244. if (annotArgs != null)
  245. {
  246. //设置注释作者
  247. annotArgs.Author = Settings.Default.AppProperties.Description.Author;
  248. PDFViewer.SetMouseMode(MouseModes.AnnotCreate);
  249. PDFViewer.SetToolParam(annotArgs);
  250. }
  251. redactionRegion.AddToRegion(RedactionDocumentRegionName, PDFViewer);
  252. }
  253. }
  254. private void PDFViewer_InfoChanged(object sender, KeyValuePair<string, object> e)
  255. {
  256. isFromSelf = true;
  257. if(e.Key=="PageNum")
  258. {
  259. var data = e.Value as RenderData;
  260. CurrentPage = data.PageIndex;
  261. }
  262. isFromSelf = false;
  263. }
  264. #endregion
  265. }
  266. }