WatermarkTemplateListFileContentViewModel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFWatermark;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Office.CustomControl;
  6. using PDF_Office.EventAggregators;
  7. using PDF_Office.Helper;
  8. using PDF_Office.Model;
  9. using PDF_Office.Model.EditTools.Background;
  10. using PDF_Office.Model.EditTools.Watermark;
  11. using PDF_Office.Properties;
  12. using PDF_Office.ViewModels.Tools;
  13. using PDFSettings;
  14. using Prism.Commands;
  15. using Prism.Events;
  16. using Prism.Mvvm;
  17. using Prism.Regions;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Collections.ObjectModel;
  21. using System.Diagnostics;
  22. using System.Drawing;
  23. using System.Drawing.Printing;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Threading.Tasks;
  27. using System.Windows;
  28. using System.Windows.Controls;
  29. using System.Windows.Media;
  30. using System.Windows.Media.Imaging;
  31. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  32. namespace PDF_Office.ViewModels.EditTools.Watermark
  33. {
  34. public class WatermarkTemplateListFileContentViewModel : BindableBase, INavigationAware
  35. {
  36. private readonly IEventAggregator eventAggregator;
  37. private CPDFViewer CurrentPDFViewer;
  38. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  39. public WatermarkItem WatermarkItem = new WatermarkItem();
  40. public int TemplateIndex = 0;
  41. public ObservableCollection<WatermarkItem> watermarkModFileCollection = new ObservableCollection<WatermarkItem>();
  42. public ObservableCollection<WatermarkItem> WatermarkModFileCollection
  43. {
  44. get { return watermarkModFileCollection; }
  45. set
  46. {
  47. watermarkModFileCollection = value;
  48. RaisePropertyChanged();
  49. }
  50. }
  51. private Visibility _createTemplateVisible;
  52. public Visibility CreateTemplateVisible
  53. {
  54. get { return _createTemplateVisible; }
  55. set { SetProperty(ref _createTemplateVisible, value); }
  56. }
  57. private ImageSource _imageSource;
  58. public ImageSource ImageSource
  59. {
  60. get { return _imageSource; }
  61. set { SetProperty(ref _imageSource, value); }
  62. }
  63. public DelegateCommand AddTemplateCommand { get; set; }
  64. public DelegateCommand<object> DeleteTemplateItemCommand { get; set; }
  65. public DelegateCommand<object> EditTemplateItemCommand { get; set; }
  66. public DelegateCommand DeleteAllTemplateItemCommand { get; set; }
  67. public DelegateCommand<object> SelectTemplateItemCommand { get; set; }
  68. public string Unicode = null;
  69. public WatermarkTemplateListFileContentViewModel(IEventAggregator eventAggregator)
  70. {
  71. this.eventAggregator = eventAggregator;
  72. Unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  73. AddTemplateCommand = new DelegateCommand(AddTemplate);
  74. DeleteTemplateItemCommand = new DelegateCommand<object>(DeleteTemplateItem);
  75. EditTemplateItemCommand = new DelegateCommand<object>(RequestEditTemplateItem);
  76. DeleteAllTemplateItemCommand = new DelegateCommand(DeleteAllTemplateItem);
  77. SelectTemplateItemCommand = new DelegateCommand<object>(SelectTemplateItem);
  78. eventAggregator.GetEvent<CurrentWatermarkPDFViewerEvent>().Subscribe(CurrentWatermarkPDFViewer, e => e.Unicode == Unicode);
  79. eventAggregator.GetEvent<SaveEditedWatermarkTemplateItemEvent>().Subscribe(SaveEditedWatermarkTemplateItem, e => e.Unicode == Unicode);
  80. }
  81. public void CurrentWatermarkPDFViewer(CPDFViewerUnicode cPDFViewerunicode)
  82. {
  83. CurrentPDFViewer = cPDFViewerunicode.Status;
  84. }
  85. private void CheckTemplateListIsEmpty(List<WatermarkItem> watermarkTemplateList)
  86. {
  87. if (watermarkTemplateList.Count() == 0)
  88. {
  89. CreateTemplateVisible = Visibility.Visible;
  90. }
  91. else
  92. {
  93. CreateTemplateVisible = Visibility.Collapsed;
  94. }
  95. }
  96. private void GetWatermarkSource()
  97. {
  98. List<WatermarkItem> watermarkModTextTemplateList = new List<WatermarkItem>();
  99. for (int temp = 0; temp < Settings.Default.WatermarkTemplateList.Count; temp++)
  100. {
  101. if (Settings.Default.WatermarkTemplateList[temp].type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG)
  102. {
  103. watermarkModTextTemplateList.Add(Settings.Default.WatermarkTemplateList[temp]);
  104. }
  105. }
  106. WatermarkModFileCollection = new ObservableCollection<WatermarkItem>(watermarkModTextTemplateList);
  107. CheckTemplateListIsEmpty(watermarkModTextTemplateList);
  108. }
  109. private void InitWatermarkTemplateList()
  110. {
  111. if (Settings.Default.WatermarkTemplateList == null)
  112. {
  113. Settings.Default.WatermarkTemplateList = new WatermarkTemplateList();
  114. }
  115. GetWatermarkSource();
  116. }
  117. public void AddTemplate()
  118. {
  119. this.eventAggregator.GetEvent<EnterTemplateListOrCreateEvent>().Publish(new EnumTemplateListOrCreateUnicode
  120. {
  121. Unicode = Unicode,
  122. Status = EnumTemplateListOrCreate.StatusCreate
  123. });
  124. }
  125. public void DeleteTemplateItem(object e)
  126. {
  127. var btn = e as System.Windows.Controls.Control;
  128. if (btn == null)
  129. {
  130. return;
  131. }
  132. var template = btn.DataContext as WatermarkItem;
  133. if (template == null)
  134. {
  135. return;
  136. }
  137. Settings.Default.WatermarkTemplateList.Remove(template);
  138. Settings.Default.Save();
  139. WatermarkModFileCollection.Remove(template);
  140. GetWatermarkSource();
  141. }
  142. public void DeleteAllTemplateItem()
  143. {
  144. while (Settings.Default.WatermarkTemplateList.Count != 0)
  145. {
  146. int temp = 0;
  147. var template = Settings.Default.WatermarkTemplateList[temp];
  148. if (template.type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG)
  149. {
  150. Settings.Default.WatermarkTemplateList.Remove(template);
  151. Settings.Default.Save();
  152. WatermarkModFileCollection.Remove(template);
  153. }
  154. }
  155. GetWatermarkSource();
  156. }
  157. public void SaveEditedWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  158. {
  159. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  160. Settings.Default.WatermarkTemplateList[TemplateIndex] = watermarkItem;
  161. Settings.Default.Save();
  162. GetWatermarkSource();
  163. }
  164. public void RequestEditTemplateItem(object e)
  165. {
  166. var control = e as Control;
  167. if (control == null)
  168. {
  169. return;
  170. }
  171. var template = control.DataContext as WatermarkItem;
  172. if (template == null)
  173. {
  174. return;
  175. }
  176. TemplateIndex = Settings.Default.WatermarkTemplateList.IndexOf(template);
  177. template.listIndex = TemplateIndex;
  178. this.eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Publish(new WatermarkItemUnicode
  179. {
  180. Unicode = Unicode,
  181. Status = template
  182. });
  183. }
  184. public void SelectTemplateItem(object e)
  185. {
  186. AlertsMessage alertsMessage = new AlertsMessage();
  187. if (CurrentPDFViewer.Document.GetWatermarkCount() > 0)
  188. {
  189. alertsMessage.ShowDialog("文件中已添加水印,您要替换现有的水印吗?", "", "取消", "确定");
  190. if (alertsMessage.result == ContentResult.Ok)
  191. {
  192. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  193. {
  194. Unicode = Unicode,
  195. Status = EnumDelete.StatusDeleteInitial
  196. });
  197. }
  198. else
  199. {
  200. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  201. {
  202. Unicode = Unicode,
  203. Status = EnumDelete.StatusCreate
  204. });
  205. }
  206. }
  207. var listBox = e as ListBox;
  208. WatermarkItem WatermarkItem = listBox.SelectedItem as WatermarkItem;
  209. SendTemplateItemToDocument(WatermarkItem);
  210. }
  211. public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
  212. {
  213. if (watermarkItem != null)
  214. {
  215. watermarkInfo.WatermarkType = watermarkItem.type;
  216. FileInfo file = new FileInfo(watermarkItem.imagepath);
  217. if (file.Exists)
  218. {
  219. if (file.Extension == ".pdf")
  220. {
  221. GetBitmapFromDocment(watermarkItem.imagepath);
  222. }
  223. else
  224. {
  225. EditToolsHelper.ChooseFile(watermarkItem.imagepath, ref WatermarkInfo);
  226. }
  227. }
  228. else
  229. {
  230. watermarkInfo.WatermarkType = ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_UNKWON;
  231. MessageBoxEx.Show("原路径为:" + watermarkItem.imagepath + ",该文件不存在");
  232. }
  233. watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
  234. watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
  235. watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
  236. watermarkInfo.Isfull = watermarkItem.isFullScreen;
  237. watermarkInfo.IsFront = watermarkItem.isfront;
  238. watermarkInfo.ImageSize = watermarkItem.imageSize;
  239. watermarkInfo.Opacity = watermarkItem.opacity;
  240. watermarkInfo.Rotation = watermarkItem.rotation;
  241. watermarkInfo.VertOffset = watermarkItem.vertOffset;
  242. watermarkInfo.HorizOffset = watermarkItem.horizOffset;
  243. watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
  244. watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
  245. watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
  246. EditToolsHelper.GetPageRange(watermarkItem.pageRangeIndex, CurrentPDFViewer.Document, ref WatermarkInfo.PageRange, watermarkItem.pageRange);
  247. }
  248. }
  249. public async void GetBitmapFromDocment(string filePath)
  250. {
  251. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  252. CPDFPage page = document.PageAtIndex(0);
  253. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  254. await Task.Run(delegate
  255. {
  256. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  257. });
  258. WatermarkInfo.ImageArray = bmp_data;
  259. WatermarkInfo.ImageWidth = (int)page.PageSize.Width;
  260. WatermarkInfo.ImageHeight = (int)page.PageSize.Height;
  261. document.ReleasePages();
  262. document.Release();
  263. }
  264. public void SendTemplateItemToDocument(WatermarkItem WatermarkItem)
  265. {
  266. ConvertItemToInfo(WatermarkItem, ref WatermarkInfo);
  267. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  268. {
  269. Unicode = Unicode,
  270. Status = WatermarkInfo
  271. });
  272. }
  273. public bool IsNavigationTarget(NavigationContext navigationContext)
  274. {
  275. return true;
  276. }
  277. public void OnNavigatedFrom(NavigationContext navigationContext)
  278. {
  279. }
  280. public void OnNavigatedTo(NavigationContext navigationContext)
  281. {
  282. InitWatermarkTemplateList();
  283. }
  284. }
  285. }