WatermarkContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using ComPDFKitViewer.PdfViewer;
  2. using PDF_Office.CustomControl;
  3. using PDF_Office.EventAggregators;
  4. using PDF_Office.Model;
  5. using PDF_Office.Model.EditTools.Background;
  6. using PDF_Office.Model.EditTools.Watermark;
  7. using PDF_Office.ViewModels.Tools;
  8. using PDFSettings;
  9. using Prism.Commands;
  10. using Prism.Events;
  11. using Prism.Mvvm;
  12. using Prism.Regions;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Linq;
  16. using System.Windows;
  17. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  18. namespace PDF_Office.ViewModels.EditTools.Watermark
  19. {
  20. public class WatermarkContentViewModel : BindableBase, INavigationAware
  21. {
  22. public IEventAggregator eventAggregator;
  23. public IRegionManager watermarkRegion;
  24. private CPDFViewer PDFViewer;
  25. private CPDFViewer CurrentPDFViewer;
  26. private ViewContentViewModel viewContentViewModel;
  27. public string TemplateListName = "WatermarkTemplateListBaseContent";
  28. public string CreateBaseName = "WatermarkCreateBaseContent";
  29. public string CreateModTextName = "WatermarkCreateTextContent";
  30. public string CreateModFileName = "WatermarkCreateFileContent";
  31. public string TemplateListModTextName = "WatermarkTemplateListTextContent";
  32. public string TemplateListModFileName = "WatermarkTemplateListFileContent";
  33. public string WatermarkDocumentName = "WatermarkDocumentContent";
  34. private string _watermarkSettingsRegionName;
  35. public string WatermarkSettingsRegionName
  36. {
  37. get { return _watermarkSettingsRegionName; }
  38. set { _watermarkSettingsRegionName = value; }
  39. }
  40. private Visibility _watermarkSettingsVisible = Visibility.Collapsed;
  41. public Visibility WatermarkSettingsVisible
  42. {
  43. get { return _watermarkSettingsVisible; }
  44. set { _watermarkSettingsVisible = value; }
  45. }
  46. private string _watermarkDocumentRegionName;
  47. /// <summary>
  48. /// 持有Document的Region,负责渲染和保存
  49. /// </summary>
  50. public string WatermarkDocumentRegionName
  51. {
  52. get { return _watermarkDocumentRegionName; }
  53. set { _watermarkDocumentRegionName = value; }
  54. }
  55. private Visibility _watermarkDocumentVisible = Visibility.Visible;
  56. /// <summary>
  57. /// 持有Document的Region可见
  58. /// </summary>
  59. public Visibility WatermarkDocumentVisible
  60. {
  61. get { return _watermarkDocumentVisible; }
  62. set { _watermarkDocumentVisible = value; }
  63. }
  64. private EnumTextOrFile _currentCreateMod;
  65. public EnumTextOrFile CurrentCreateMod
  66. {
  67. get { return _currentCreateMod; }
  68. set { _currentCreateMod = value; }
  69. }
  70. private EnumTextOrFile _currentTemplateListMod;
  71. public EnumTextOrFile CurrentTemplateListMod
  72. {
  73. get { return _currentTemplateListMod; }
  74. set { _currentTemplateListMod = value; }
  75. }
  76. public DelegateCommand CloseEditToolCommand { get; set; }
  77. public DelegateCommand DeleteWatermarkCommand { get; set; }
  78. public DelegateCommand ConfirmEditToolCommand { get; set; }
  79. public DelegateCommand<string> EnterSelectedContentCommand { get; set; }
  80. public string Unicode = null;
  81. public WatermarkContentViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)
  82. {
  83. this.eventAggregator = eventAggregator;
  84. this.watermarkRegion = regionManager;
  85. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  86. WatermarkSettingsVisible = Visibility.Visible;
  87. WatermarkSettingsRegionName = Guid.NewGuid().ToString();
  88. WatermarkDocumentRegionName = Guid.NewGuid().ToString();
  89. CloseEditToolCommand = new DelegateCommand(CloseEditTool);
  90. ConfirmEditToolCommand = new DelegateCommand(ConfirmEditTool);
  91. EnterSelectedContentCommand = new DelegateCommand<string>(EnterSelectedContent);
  92. DeleteWatermarkCommand = new DelegateCommand(DeleteWatermark);
  93. eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Subscribe(EnterTemplateListOrCreate, e => e.Unicode == Unicode);
  94. eventAggregator.GetEvent<SetCurrentCreateModEvent>().Subscribe(SetCurrentCreateMod, e => e.Unicode == Unicode);
  95. eventAggregator.GetEvent<SetCurrentTemplateListModEvent>().Subscribe(SetCurrentTemplateListMod, e => e.Unicode == Unicode);
  96. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
  97. eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Subscribe(EditWatermarkTemplateItem, e => e.Unicode == Unicode);
  98. }
  99. public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicod)
  100. {
  101. CurrentPDFViewer = cPDFViewerunicod.Status;
  102. }
  103. public void CloseEditTool()
  104. {
  105. this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = Unicode, Status = EnumCloseMode.StatusCancel });
  106. }
  107. public void ConfirmEditTool()
  108. {
  109. this.eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Publish();
  110. this.eventAggregator.GetEvent<CloseEditToolEvent>().Publish(new EnumCloseModeUnicode { Unicode = Unicode, Status = EnumCloseMode.StatusConfirm });
  111. }
  112. public void EnterTemplateListOrCreate(EnumTemplateListOrCreateUnicode enumTemplateListOrCreateunicode)
  113. {
  114. EnumTemplateListOrCreate enumTemplateListOrCreate = enumTemplateListOrCreateunicode.Status;
  115. if (enumTemplateListOrCreate == EnumTemplateListOrCreate.StatusTemplate)
  116. {
  117. EnterSelectedContent(TemplateListName);
  118. }
  119. else
  120. {
  121. EnterSelectedContent(CreateBaseName);
  122. }
  123. }
  124. public void EditWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  125. {
  126. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  127. NavigationParameters param = new NavigationParameters();
  128. param.Add(ParameterNames.PDFViewer, PDFViewer);
  129. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  130. param.Add("WatermarkItem", watermarkItem);
  131. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, CreateBaseName, param);
  132. WatermarkSettingsVisible = Visibility.Visible;
  133. }
  134. public void SetCurrentCreateMod(stringUnicode currentCreateModNameunicode)
  135. {
  136. string currentCreateModName = currentCreateModNameunicode.Status;
  137. if (currentCreateModName == CreateModTextName)
  138. {
  139. CurrentCreateMod = EnumTextOrFile.StatusText;
  140. }
  141. else if (currentCreateModName == CreateModFileName)
  142. {
  143. CurrentCreateMod = EnumTextOrFile.StatusFile;
  144. }
  145. }
  146. public void SetCurrentTemplateListMod(stringUnicode currentTemplateListModNameunicode)
  147. {
  148. string currentTemplateListModName = currentTemplateListModNameunicode.Status;
  149. if (currentTemplateListModName == TemplateListModTextName)
  150. {
  151. CurrentTemplateListMod = EnumTextOrFile.StatusText;
  152. }
  153. else if (currentTemplateListModName == TemplateListModFileName)
  154. {
  155. CurrentTemplateListMod = EnumTextOrFile.StatusFile;
  156. }
  157. }
  158. public void EnterSelectedContent(string SelectedContentName)
  159. {
  160. NavigationParameters param = new NavigationParameters();
  161. param.Add(ParameterNames.PDFViewer, PDFViewer);
  162. if (SelectedContentName == TemplateListName)
  163. {
  164. param.Add("CurrentCreateModName", CurrentCreateMod);
  165. }
  166. else if (SelectedContentName == CreateBaseName)
  167. {
  168. param.Add("CurrentTemplateListModName", CurrentTemplateListMod);
  169. }
  170. watermarkRegion.RequestNavigate(WatermarkSettingsRegionName, SelectedContentName, param);
  171. WatermarkSettingsVisible = Visibility.Visible;
  172. }
  173. public void EnterDocumentContent()
  174. {
  175. NavigationParameters param = new NavigationParameters();
  176. param.Add(ParameterNames.PDFViewer, PDFViewer);
  177. param.Add(ParameterNames.ViewContentViewModel, viewContentViewModel);
  178. watermarkRegion.RequestNavigate(WatermarkDocumentRegionName, WatermarkDocumentName, param);
  179. WatermarkDocumentVisible = Visibility.Visible;
  180. }
  181. private void DeleteWatermark() {
  182. AlertsMessage alertsMessage = new AlertsMessage();
  183. if (CurrentPDFViewer.Document.GetWatermarkCount()> 0)
  184. {
  185. alertsMessage.ShowDialog("确定要删除文件水印吗?", "", "取消", "删除");
  186. if (alertsMessage.result == ContentResult.Ok)
  187. {
  188. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode {
  189. Unicode = Unicode, Status = EnumDelete.StatusDeleteAll});
  190. }
  191. else
  192. {
  193. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode {
  194. Unicode = Unicode, Status = EnumDelete.StatusCreate});
  195. }
  196. }
  197. else {
  198. alertsMessage.ShowDialog("无法在本文件中找到可删除的水印。如果您看到水印,其不是使用PDF Office添加的,因此无法被检测到。", "", "取消", "确定");
  199. }
  200. }
  201. public void OnNavigatedTo(NavigationContext navigationContext)
  202. {
  203. navigationContext.Parameters.TryGetValue<ViewContentViewModel>(ParameterNames.ViewContentViewModel, out viewContentViewModel);
  204. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  205. EnterSelectedContent(TemplateListName);
  206. EnterDocumentContent();
  207. }
  208. public bool IsNavigationTarget(NavigationContext navigationContext)
  209. {
  210. return true;
  211. }
  212. public void OnNavigatedFrom(NavigationContext navigationContext)
  213. {
  214. }
  215. }
  216. }