WatermarkCreateFileContentViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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.Watermark;
  10. using PDF_Office.Model.PageEdit;
  11. using Prism.Commands;
  12. using Prism.Events;
  13. using Prism.Mvvm;
  14. using Prism.Regions;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Drawing.Imaging;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Threading.Tasks;
  22. using System.Windows;
  23. namespace PDF_Office.ViewModels.EditTools.Watermark
  24. {
  25. public class WatermarkCreateFileContentViewModel : BindableBase, INavigationAware
  26. {
  27. public WatermarkInfo WatermarkInfo = new WatermarkInfo();
  28. private CPDFViewer PDFViewer;
  29. IEventAggregator eventAggregator;
  30. private List<string> _opacityList = new List<string>();
  31. public List<string> OpacityList
  32. {
  33. get { return _opacityList; }
  34. set
  35. {
  36. SetProperty(ref _opacityList, value);
  37. }
  38. }
  39. private void InitOpacityList()
  40. {
  41. OpacityList.Clear();
  42. for (int temp = 0; temp <= 100; temp += 10)
  43. {
  44. OpacityList.Add(temp.ToString() + " %");
  45. }
  46. }
  47. private List<string> _rotationList = new List<string>();
  48. public List<string> RotationList
  49. {
  50. get { return _rotationList; }
  51. set
  52. {
  53. SetProperty(ref _rotationList, value);
  54. }
  55. }
  56. private void InitRotationList()
  57. {
  58. RotationList.Clear();
  59. for (int temp = -45; temp <= 45; temp += 15)
  60. {
  61. RotationList.Add(temp.ToString());
  62. }
  63. }
  64. private List<string> _scaleList = new List<string>();
  65. public List<string> ScaleList
  66. {
  67. get { return _scaleList; }
  68. set
  69. {
  70. SetProperty(ref _scaleList, value);
  71. }
  72. }
  73. private void InitScaleList()
  74. {
  75. ScaleList.Clear();
  76. for (int temp = 0; temp <= 100; temp += 10)
  77. {
  78. ScaleList.Add(temp.ToString() + " %");
  79. }
  80. }
  81. private List<string> _isFrontList = new List<string>();
  82. public List<string> IsFrontList
  83. {
  84. get { return _isFrontList; }
  85. set
  86. {
  87. SetProperty(ref _isFrontList, value);
  88. }
  89. }
  90. private void InitIsFrontList()
  91. {
  92. IsFrontList.Clear();
  93. IsFrontList.Add("位于页面上方");
  94. IsFrontList.Add("位于页面下方");
  95. }
  96. private int _rotationValue = 0;
  97. public int RotationValue
  98. {
  99. get { return _rotationValue; }
  100. set
  101. {
  102. SetProperty(ref _rotationValue, value);
  103. WatermarkInfo.Rotation = (float)RotationValue;
  104. }
  105. }
  106. private int _opacityValue = 100;
  107. public int OpacityValue
  108. {
  109. get { return _opacityValue; }
  110. set
  111. {
  112. SetProperty(ref _opacityValue, value);
  113. WatermarkInfo.Opacity = (byte)(((float)OpacityValue / 100) * 225);
  114. }
  115. }
  116. private int _relativeScaleValue = 50;
  117. public int RelativeScaleValue
  118. {
  119. get { return _relativeScaleValue; }
  120. set
  121. {
  122. SetProperty(ref _relativeScaleValue, value);
  123. }
  124. }
  125. private string _vertOffsetValue = "0";
  126. public string VertOffsetValue
  127. {
  128. get { return _vertOffsetValue; }
  129. set
  130. {
  131. SetProperty(ref _vertOffsetValue, value);
  132. WatermarkInfo.VertOffset = float.Parse(VertOffsetValue);
  133. }
  134. }
  135. private string _horizOffsetValue = "0";
  136. public string HorizOffsetValue
  137. {
  138. get { return _horizOffsetValue; }
  139. set
  140. {
  141. SetProperty(ref _horizOffsetValue, value);
  142. WatermarkInfo.HorizOffset = float.Parse(HorizOffsetValue);
  143. }
  144. }
  145. private string _verticalSpacingValue = "6";
  146. public string VerticalSpacingValue
  147. {
  148. get { return _verticalSpacingValue; }
  149. set
  150. {
  151. SetProperty(ref _verticalSpacingValue, value);
  152. WatermarkInfo.VerticalSpacing = float.Parse(VerticalSpacingValue);
  153. }
  154. }
  155. private string _horizontalSpacingValue = "6";
  156. public string HorizontalSpacingValue
  157. {
  158. get { return _horizontalSpacingValue; }
  159. set
  160. {
  161. SetProperty(ref _horizontalSpacingValue, value);
  162. WatermarkInfo.HorizontalSpacing = float.Parse(HorizontalSpacingValue);
  163. }
  164. }
  165. public string PageRangeText { get; set; } = "0";
  166. private int _pageRangeSelectIndex = 0;
  167. public int PageRangeSelectIndex
  168. {
  169. get { return _pageRangeSelectIndex; }
  170. set
  171. {
  172. SetProperty(ref _pageRangeSelectIndex, value);
  173. EditToolsHelper.GetPageRange(PageRangeSelectIndex, PDFViewer.Document, ref WatermarkInfo.PageRange, PageRangeText);
  174. }
  175. }
  176. private int _isFrontSelectedIndex = 0;
  177. public int IsFrontSelectedIndex
  178. {
  179. get { return _isFrontSelectedIndex; }
  180. set
  181. {
  182. SetProperty(ref _isFrontSelectedIndex, value);
  183. SetIsFront(IsFrontSelectedIndex);
  184. }
  185. }
  186. private string _fileNameText = "";
  187. public string FileNameText
  188. {
  189. get { return _fileNameText; }
  190. set
  191. {
  192. SetProperty(ref _fileNameText, value);
  193. }
  194. }
  195. private Visibility _createFileVisible = Visibility.Collapsed;
  196. public Visibility CreateFileVisible
  197. {
  198. get { return _createFileVisible; }
  199. set { SetProperty(ref _createFileVisible, value); }
  200. }
  201. private ObservableDictionary<string, bool> _getLocationFromNumber = new ObservableDictionary<string, bool>();
  202. public ObservableDictionary<string, bool> GetLocationFromNumber
  203. {
  204. get { return _getLocationFromNumber; }
  205. set { _getLocationFromNumber = value; }
  206. }
  207. public DelegateCommand<object> ChangeLocationCommand { get; set; }
  208. public DelegateCommand OpenFileCommand { get; set; }
  209. public WatermarkCreateFileContentViewModel(IEventAggregator eventAggregator)
  210. {
  211. this.eventAggregator = eventAggregator;
  212. WatermarkInfo.WatermarkType=C_Watermark_Type.WATERMARK_TYPE_IMG;
  213. ChangeLocationCommand = new DelegateCommand<object>(ChangeLocation);
  214. OpenFileCommand = new DelegateCommand(OpenFile);
  215. InitList();
  216. WatermarkInfo.WatermarkHorizalign = C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER;
  217. WatermarkInfo.WatermarkVertalign = C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER;
  218. InitLocationButtonMatrix();
  219. eventAggregator.GetEvent<ConfirmEditToolsWatermarkEvent>().Subscribe(ConfirmEditToolsWatermark);
  220. }
  221. private void SetIsFront(int Index)
  222. {
  223. if (Index == 0)
  224. {
  225. WatermarkInfo.IsFront = false;
  226. }
  227. if (Index == 1)
  228. {
  229. WatermarkInfo.IsFront = true;
  230. }
  231. }
  232. private void InitList()
  233. {
  234. InitOpacityList();
  235. InitIsFrontList();
  236. InitRotationList();
  237. InitScaleList();
  238. }
  239. private void InitLocationButtonMatrix()
  240. {
  241. GetLocationFromNumber.Clear();
  242. for (var temp = 0; temp <= 22; temp++)
  243. {
  244. GetLocationFromNumber.Add(temp.ToString(), true);
  245. if (temp % 10 == 2)
  246. {
  247. temp += 7;
  248. }
  249. }
  250. int Num = (int)WatermarkInfo.WatermarkVertalign * 10 + (int)WatermarkInfo.WatermarkHorizalign;
  251. GetLocationFromNumber[Num.ToString()] = false;
  252. }
  253. public void ChangeLocation(object e)
  254. {
  255. string args = e as string;
  256. if (args != null)
  257. {
  258. WatermarkInfo.WatermarkVertalign = (C_Watermark_Vertalign)(int.Parse(args) / 10);
  259. WatermarkInfo.WatermarkHorizalign = (C_Watermark_Horizalign)(int.Parse(args) % 10);
  260. InitLocationButtonMatrix();
  261. }
  262. }
  263. public void ConfirmEditToolsWatermark(EnumTextOrFile enumTextOrFile)
  264. {
  265. if (enumTextOrFile == EnumTextOrFile.StatusFile)
  266. {
  267. eventAggregator.GetEvent<SetWatermarkEvent>().Publish(WatermarkInfo);
  268. }
  269. }
  270. public void OpenFile()
  271. {
  272. System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
  273. dlg.Multiselect = false;
  274. dlg.Filter = "PDF|*.png;*.jpg;*.pdf";
  275. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  276. {
  277. FileNameText = dlg.SafeFileName;
  278. FileInfo file = new FileInfo(dlg.FileName);
  279. if (file.Extension == ".pdf")
  280. {
  281. GetBitmapFromDocment(dlg.FileName);
  282. }
  283. else
  284. {
  285. EditToolsHelper.ChooseFile(dlg.FileName, ref WatermarkInfo);
  286. }
  287. CreateFileVisible=Visibility.Visible;
  288. }
  289. }
  290. public async void GetBitmapFromDocment(string filePath)
  291. {
  292. CPDFDocument document = CPDFDocument.InitWithFilePath(filePath);
  293. CPDFPage page = document.PageAtIndex(0);
  294. byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
  295. await Task.Run(delegate
  296. {
  297. page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
  298. });
  299. WatermarkInfo.ImageArray= bmp_data;
  300. WatermarkInfo.ImageWidth = (int)page.PageSize.Width;
  301. WatermarkInfo.ImageHeight = (int)page.PageSize.Height;
  302. document.ReleasePages();
  303. document.Release();
  304. }
  305. public bool IsNavigationTarget(NavigationContext navigationContext)
  306. {
  307. return true;
  308. }
  309. public void OnNavigatedFrom(NavigationContext navigationContext)
  310. {
  311. }
  312. public void OnNavigatedTo(NavigationContext navigationContext)
  313. {
  314. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  315. }
  316. }
  317. }