WatermarkTemplateListFileContentViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKit.PDFPage;
  3. using ComPDFKit.PDFWatermark;
  4. using ComPDFKitViewer.PdfViewer;
  5. using PDF_Master.CustomControl;
  6. using PDF_Master.EventAggregators;
  7. using PDF_Master.Helper;
  8. using PDF_Master.Model;
  9. using PDF_Master.Model.EditTools.Background;
  10. using PDF_Master.Model.EditTools.Watermark;
  11. using PDF_Master.Properties;
  12. using PDF_Master.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_Master.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. List<WatermarkItem> removewatermarkItems = new List<WatermarkItem>();
  145. for (int i = 0; i < Settings.Default.WatermarkTemplateList.Count; i++)
  146. {
  147. var template = Settings.Default.WatermarkTemplateList[i];
  148. if (template.type == ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_IMG)
  149. {
  150. removewatermarkItems.Add(template);
  151. }
  152. }
  153. foreach (var removetemplate in removewatermarkItems)
  154. {
  155. Settings.Default.WatermarkTemplateList.Remove(removetemplate);
  156. Settings.Default.Save();
  157. WatermarkModFileCollection.Remove(removetemplate);
  158. }
  159. GetWatermarkSource();
  160. }
  161. public void SaveEditedWatermarkTemplateItem(WatermarkItemUnicode watermarkItemunicode)
  162. {
  163. WatermarkItem watermarkItem = watermarkItemunicode.Status;
  164. Settings.Default.WatermarkTemplateList[TemplateIndex] = watermarkItem;
  165. Settings.Default.Save();
  166. GetWatermarkSource();
  167. }
  168. public void RequestEditTemplateItem(object e)
  169. {
  170. var control = e as Control;
  171. if (control == null)
  172. {
  173. return;
  174. }
  175. var template = control.DataContext as WatermarkItem;
  176. if (template == null)
  177. {
  178. return;
  179. }
  180. TemplateIndex = Settings.Default.WatermarkTemplateList.IndexOf(template);
  181. template.listIndex = TemplateIndex;
  182. this.eventAggregator.GetEvent<EditWatermarkTemplateItemEvent>().Publish(new WatermarkItemUnicode
  183. {
  184. Unicode = Unicode,
  185. Status = template
  186. });
  187. }
  188. public void SelectTemplateItem(object e)
  189. {
  190. AlertsMessage alertsMessage = new AlertsMessage();
  191. if (CurrentPDFViewer.Document.GetWatermarkCount() > 0)
  192. {
  193. alertsMessage.ShowDialog("文件中已添加水印,您要替换现有的水印吗?", "", "取消", "确定");
  194. if (alertsMessage.result == ContentResult.Ok)
  195. {
  196. this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  197. {
  198. Unicode = Unicode,
  199. Status = EnumDelete.StatusDeleteInitial
  200. });
  201. }
  202. else
  203. {
  204. //this.eventAggregator.GetEvent<DeleteWatermarkEvent>().Publish(new EnumDeleteUnicode
  205. //{
  206. // Unicode = Unicode,
  207. // Status = EnumDelete.StatusCreate
  208. //});
  209. return;
  210. }
  211. }
  212. var listBox = e as ListBox;
  213. WatermarkItem WatermarkItem = listBox.SelectedItem as WatermarkItem;
  214. SendTemplateItemToDocument(WatermarkItem);
  215. }
  216. public void ConvertItemToInfo(WatermarkItem watermarkItem, ref WatermarkInfo watermarkInfo)
  217. {
  218. if (watermarkItem != null)
  219. {
  220. watermarkInfo.WatermarkType = watermarkItem.type;
  221. FileInfo file = new FileInfo(watermarkItem.imagepath);
  222. if (file.Exists)
  223. {
  224. if (file.Extension == ".pdf")
  225. {
  226. GetBitmapFromDocment(watermarkItem.imagepath);
  227. }
  228. else
  229. {
  230. EditToolsHelper.ChooseFile(watermarkItem.imagepath, ref WatermarkInfo);
  231. }
  232. }
  233. else
  234. {
  235. watermarkInfo.WatermarkType = ComPDFKit.PDFWatermark.C_Watermark_Type.WATERMARK_TYPE_UNKWON;
  236. MessageBoxEx.Show("原路径为:" + watermarkItem.imagepath + ",该文件不存在");
  237. }
  238. watermarkInfo.WatermarkHorizalign = watermarkItem.horizalign;
  239. watermarkInfo.WatermarkVertalign = watermarkItem.vertalign;
  240. watermarkInfo.IsRelativeScale = watermarkItem.isRelativeScale;
  241. watermarkInfo.Isfull = watermarkItem.isFullScreen;
  242. watermarkInfo.IsFront = watermarkItem.isfront;
  243. watermarkInfo.ImageSize = watermarkItem.imageSize;
  244. watermarkInfo.Opacity = watermarkItem.opacity;
  245. watermarkInfo.Rotation = watermarkItem.rotation;
  246. watermarkInfo.VertOffset = watermarkItem.vertOffset;
  247. watermarkInfo.HorizOffset = watermarkItem.horizOffset;
  248. watermarkInfo.VerticalSpacing = watermarkItem.verticalSpacing;
  249. watermarkInfo.HorizontalSpacing = watermarkItem.horizontalSpacing;
  250. watermarkInfo.PageRangeIndex = watermarkItem.pageRangeIndex;
  251. watermarkInfo.PageRange = watermarkItem.pageRange;
  252. }
  253. }
  254. public async void GetBitmapFromDocment(string filePath)
  255. {
  256. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  257. CPDFPage page = document.PageAtIndex(0);
  258. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  259. await Task.Run(delegate
  260. {
  261. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  262. });
  263. WatermarkInfo.ImageArray = bmp_data;
  264. WatermarkInfo.ImageWidth = (int)page.PageSize.Width;
  265. WatermarkInfo.ImageHeight = (int)page.PageSize.Height;
  266. document.ReleasePages();
  267. document.Release();
  268. }
  269. public void SendTemplateItemToDocument(WatermarkItem WatermarkItem)
  270. {
  271. ConvertItemToInfo(WatermarkItem, ref WatermarkInfo);
  272. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(new WatermarkInfoUnicode
  273. {
  274. Unicode = Unicode,
  275. Status = WatermarkInfo
  276. });
  277. }
  278. public bool IsNavigationTarget(NavigationContext navigationContext)
  279. {
  280. return true;
  281. }
  282. public void OnNavigatedFrom(NavigationContext navigationContext)
  283. {
  284. }
  285. public void OnNavigatedTo(NavigationContext navigationContext)
  286. {
  287. InitWatermarkTemplateList();
  288. }
  289. }
  290. }