WatermarkTemplateListTextContentViewModel.cs 11 KB

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