WatermarkTemplateListTextContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using PDFSettings;
  2. using Prism.Commands;
  3. using Prism.Mvvm;
  4. using Prism.Regions;
  5. using System.Windows;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using PDF_Master.Properties;
  11. using Prism.Events;
  12. using PDF_Master.EventAggregators;
  13. using PDF_Master.Model.EditTools.Watermark;
  14. using System.Windows.Controls;
  15. using ComPDFKitViewer.PdfViewer;
  16. using PDF_Master.CustomControl;
  17. using PDF_Master.Model;
  18. using PDF_Master.Helper;
  19. using PDF_Master.Model.EditTools.Background;
  20. using PDF_Master.ViewModels.Tools;
  21. using Microsoft.Office.Interop.Word;
  22. namespace PDF_Master.ViewModels.EditTools.Watermark
  23. {
  24. public class WatermarkTemplateListTextContentViewModel : BindableBase, INavigationAware
  25. {
  26. private readonly IEventAggregator eventAggregator;
  27. private CPDFViewer CurrentPDFViewer;
  28. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  29. public WatermarkItem WatermarkItem = new WatermarkItem();
  30. public int TemplateIndex = 0;
  31. public ObservableCollection<WatermarkItem> watermarkModTextCollection = new ObservableCollection<WatermarkItem>();
  32. public ObservableCollection<WatermarkItem> WatermarkModTextCollection
  33. {
  34. get { return watermarkModTextCollection; }
  35. set
  36. {
  37. watermarkModTextCollection = value;
  38. RaisePropertyChanged();
  39. }
  40. }
  41. private Visibility _createTemplateVisible;
  42. public Visibility CreateTemplateVisible
  43. {
  44. get { return _createTemplateVisible; }
  45. set { SetProperty(ref _createTemplateVisible, value); }
  46. }
  47. public DelegateCommand AddTemplateCommand { get; set; }
  48. public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
  49. public DelegateCommand<object> EditTemplateItemCommand { get; set; }
  50. public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
  51. public DelegateCommand<object> SelectTemplateItemCommand { get; set; }
  52. public string Unicode = null;
  53. public WatermarkTemplateListTextContentViewModel(IEventAggregator eventAggregator)
  54. {
  55. this.eventAggregator = eventAggregator;
  56. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  57. AddTemplateCommand = new DelegateCommand(AddTemplate);
  58. DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
  59. DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
  60. EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
  61. SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
  62. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
  63. eventAggregator.GetEvent<SaveEditedWatermarkTemplateItemEvent>().Subscribe(SaveEditedWatermarkTemplateItem, e => e.Unicode == Unicode);
  64. }
  65. public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicode)
  66. {
  67. CurrentPDFViewer = cPDFViewerunicode.Status;
  68. }
  69. private void CheckTemplateListIsEmpty(List<WatermarkItem> watermarkTemplateList)
  70. {
  71. if (watermarkTemplateList.Count() == 0)
  72. {
  73. CreateTemplateVisible = Visibility.Visible;
  74. }
  75. else
  76. {
  77. CreateTemplateVisible = Visibility.Collapsed;
  78. }
  79. }
  80. private void GetWatermarkSource()
  81. {
  82. List<WatermarkItem> watermarkModTextTemplateList = new List<WatermarkItem>();
  83. for (int temp = 0; temp < Settings.Default.WatermarkTemplateList.Count; temp++)
  84. {
  85. if (Settings.Default.WatermarkTemplateList[temp].type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
  86. {
  87. watermarkModTextTemplateList.Add(Settings.Default.WatermarkTemplateList[temp]);
  88. }
  89. }
  90. WatermarkModTextCollection = new ObservableCollection<WatermarkItem>(watermarkModTextTemplateList);
  91. CheckTemplateListIsEmpty(watermarkModTextTemplateList);
  92. }
  93. private void InitWatermarkTemplateList()
  94. {
  95. if (Settings.Default.WatermarkTemplateList == null)
  96. {
  97. Settings.Default.WatermarkTemplateList = new WatermarkTemplateList();
  98. }
  99. GetWatermarkSource();
  100. }
  101. public void AddTemplate()
  102. {
  103. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  104. {
  105. Unicode = Unicode,
  106. Status = EnumTemplateListOrCreate.StatusCreate
  107. });
  108. }
  109. public void DeleteTemplateItem(object e)
  110. {
  111. var btn = e as System.Windows.Controls.Control;
  112. if (btn == null)
  113. {
  114. return;
  115. }
  116. var template = btn.DataContext as WatermarkItem;
  117. if (template == null)
  118. {
  119. return;
  120. }
  121. Settings.Default.WatermarkTemplateList.Remove(template);
  122. Settings.Default.Save();
  123. WatermarkModTextCollection.Remove(template);
  124. GetWatermarkSource();
  125. }
  126. public void DeleteAllTemplateItem()
  127. {
  128. List<WatermarkItem> removewatermarkItems=new List<WatermarkItem>();
  129. for (int i=0; i<Settings.Default.WatermarkTemplateList.Count;i++)
  130. {
  131. var template = Settings.Default.WatermarkTemplateList[i];
  132. if (template.type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_TEXT)
  133. {
  134. removewatermarkItems.Add(template);
  135. }
  136. }
  137. foreach (var removetemplate in removewatermarkItems)
  138. {
  139. Settings.Default.WatermarkTemplateList.Remove(removetemplate);
  140. Settings.Default.Save();
  141. WatermarkModTextCollection.Remove(removetemplate);
  142. }
  143. GetWatermarkSource();
  144. }
  145. public void RequestEditTemplateItem(object e)
  146. {
  147. var control = e as Control;
  148. if (control == null)
  149. {
  150. return;
  151. }
  152. var template = control.DataContext as WatermarkItem;
  153. if (template == null)
  154. {
  155. return;
  156. }
  157. TemplateIndex = Settings.Default.WatermarkTemplateList.IndexOf(template);
  158. template.listIndex = TemplateIndex;
  159. this.eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Publish(new WatermarkItemUnicode
  160. {
  161. Unicode = Unicode,
  162. Status = template
  163. });
  164. }
  165. public void SelectTemplateItem(object e)
  166. {
  167. AlertsMessage alertsMessage = new AlertsMessage();
  168. if (CurrentPDFViewer.Document.GetWatermarkCount() > 0)
  169. {
  170. alertsMessage.ShowDialog("文件中已添加水印,您要替换现有的水印吗?", "", "取消", "确定");
  171. if (alertsMessage.result == ContentResult.Ok)
  172. {
  173. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  174. {
  175. Unicode = Unicode,
  176. Status = EnumDelete.StatusDeleteInitial
  177. });
  178. }
  179. else
  180. {
  181. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  182. //{
  183. // Unicode = Unicode,
  184. // Status = EnumDelete.StatusCreate
  185. //});
  186. return;
  187. }
  188. }
  189. var listBox = e as ListBox;
  190. WatermarkItem WatermarkItem = listBox.SelectedItem as WatermarkItem;
  191. SendTemplateItemToDocument(WatermarkItem);
  192. }
  193. public void SaveEditedWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  194. {
  195. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  196. Settings.Default.WatermarkTemplateList[TemplateIndex] = watermarkItem;
  197. Settings.Default.Save();
  198. GetWatermarkSource();
  199. }
  200. public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
  201. {
  202. if (watermarkItem != null)
  203. {
  204. watermarkInfo.WatermarkType = watermarkItem.type;
  205. watermarkInfo.Text = watermarkItem.text;
  206. watermarkInfo.TextSize = watermarkItem.fontSize;
  207. watermarkInfo.FontName = watermarkItem.fontName;
  208. watermarkInfo.TextColor = watermarkItem.textcolor;
  209. watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
  210. watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
  211. watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
  212. watermarkInfo.Isfull = watermarkItem.isFullScreen;
  213. watermarkInfo.IsFront = watermarkItem.isfront;
  214. watermarkInfo.Opacity = watermarkItem.opacity;
  215. watermarkInfo.Rotation = watermarkItem.rotation;
  216. watermarkInfo.VertOffset = watermarkItem.vertOffset;
  217. watermarkInfo.HorizOffset = watermarkItem.horizOffset;
  218. watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
  219. watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
  220. watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
  221. watermarkInfo.PageRange = watermarkItem.pageRange;
  222. }
  223. }
  224. public void SendTemplateItemToDocument(WatermarkItem WatermarkItem)
  225. {
  226. ConvertItemToInfo(WatermarkItem, ref WatermarkInfo);
  227. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  228. {
  229. Unicode = Unicode,
  230. Status = WatermarkInfo
  231. });
  232. }
  233. public bool IsNavigationTarget(NavigationContext navigationContext)
  234. {
  235. return true;
  236. }
  237. public void OnNavigatedFrom(NavigationContext navigationContext)
  238. {
  239. }
  240. public void OnNavigatedTo(NavigationContext navigationContext)
  241. {
  242. InitWatermarkTemplateList();
  243. }
  244. }
  245. }