WatermarkContentViewModel.cs 12 KB

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